Client

Messaging.Client

You can use instances of this class to send or receive messages. Please see MessagingClientApiEvents for the full list of events you can subscribe to.

Example

const client = new Messaging.Client({
  project: "<project-id>",
  token: "<api-token>",
  contexts: ['office']
})

client.on('message.received', (message) => {
  console.log('message.received', message)
})

await client.send({
  context: 'office',
  from: '+1xxx',
  to: '+1yyy',
  body: 'Hello World!'
})

Constructors

constructor

new Client(opts)

Parameters

NameTypeDescription
optsObject-
opts.contextsstring[]

SignalWire contexts, e.g. 'home', 'office'…

opts.projectstring

SignalWire Project ID, e.g. a10d8a9f-2166-4e82-56ff-118bc3a4840f

opts.tokenstring

SignalWire API token, e.g. PT9e5660c101cd140a1c93a0197640a369cf5f16975a0079c9

Methods

disconnect

disconnect(): void

Disconnects this client. The client will stop receiving events and you will need to create a new instance if you want to use it again.

Returns

void

Example

client.disconnect()

off

off<T>(event, fn?): EmitterContract<MessagingClientApiEvents>

Type parameters

NameType
Textends keyof MessagingClientApiEvents

Parameters

NameType
eventT
fn?(...args: ArgumentMap<MessagingClientApiEvents>[Extract<T, keyof MessagingClientApiEvents>]) => void

Returns

EmitterContract<MessagingClientApiEvents>


on

on<T>(event, fn): EmitterContract<MessagingClientApiEvents>

Type parameters

NameType
Textends keyof MessagingClientApiEvents

Parameters

NameType
eventT
fn(...args: ArgumentMap<MessagingClientApiEvents>[Extract<T, keyof MessagingClientApiEvents>]) => void

Returns

EmitterContract<MessagingClientApiEvents>


once

once<T>(event, fn): EmitterContract<MessagingClientApiEvents>

Type parameters

NameType
Textends keyof MessagingClientApiEvents

Parameters

NameType
eventT
fn(...args: ArgumentMap<MessagingClientApiEvents>[Extract<T, keyof MessagingClientApiEvents>]) => void

Returns

EmitterContract<MessagingClientApiEvents>


removeAllListeners

removeAllListeners<T>(event?): EmitterContract<MessagingClientApiEvents>

Type parameters

NameType
Textends keyof MessagingClientApiEvents

Parameters

NameType
event?T

Returns

EmitterContract<MessagingClientApiEvents>


send

send(params): Promise<MessagingSendResult>

Send an outbound SMS or MMS message.

Parameters

NameTypeDescription
paramsObject-
params.body?string

The content of the message. Optional if media is present.

params.context?string

Inbound events for the message will be received on this context. If not specified, a default context will be used.

params.fromstring

The phone number to place the message from. Must be a SignalWire phone number or short code that you own.

params.media?string[]

Array of URLs to send in the message. Optional if body is present.

params.region?string

Region of the world to originate the message from. A default value is picked based on account preferences or device location.

params.tags?string[]

Array of strings to tag the message with for searching in the UI.

params.tostring

The phone number to send to.

Returns

Promise<MessagingSendResult>

Asynchronously returns a result object.

Example

Send a message.

try {
  const sendResult = await client.send({
    from: '+1xxx',
    to: '+1yyy',
    body: 'Hello World!'
  })
  console.log('Message ID: ', sendResult.messageId)
} catch (e) {
  console.error(e.message)
}