Message

View as MarkdownOpen in Claude

The Message class represents a single SMS/MMS message in the RELAY messaging namespace. It tracks the lifecycle of a sent or received message through state events. Outbound messages progress through queued, initiated, sent, and then reach a terminal state (delivered, undelivered, or failed). Inbound messages arrive fully formed with state received.

Obtain a Message instance from RelayClient.sendMessage() or from the client.onMessage() handler for inbound messages.

1import { RelayClient } from '@signalwire/sdk';
2
3const client = new RelayClient({
4 project: process.env.SIGNALWIRE_PROJECT_ID!,
5 token: process.env.SIGNALWIRE_TOKEN!,
6 contexts: ['default']
7});
8
9await client.connect();
10
11const message = await client.sendMessage({
12 to: '+15551234567',
13 from: '+15559876543',
14 body: 'Hello from SignalWire!',
15});
16
17// Wait for delivery confirmation
18const result = await message.wait(30_000);
19console.log(`Final state: ${message.state}`);
20
21await client.disconnect();

Properties

messageId
string

Unique identifier for this message, assigned by SignalWire.

context
string

The messaging context this message belongs to.

direction
string

Message direction. Valid values:

  • "inbound" — incoming message
  • "outbound" — outgoing message
fromNumber
string

Sender phone number in E.164 format.

toNumber
string

Recipient phone number in E.164 format.

body
string

Text content of the message.

media
string[]

List of media URLs for MMS messages. Empty list for SMS-only messages.

segments
number

Number of SMS segments required for this message.

state
string

Current message state. See Message Constants for valid values.

  • "queued" — message has been accepted and is waiting to be processed
  • "initiated" — message processing has started
  • "sent" — message has been dispatched to the carrier
  • "delivered" — message was successfully delivered to the recipient
  • "undelivered" — carrier was unable to deliver the message
  • "failed" — message could not be sent
  • "received" — inbound message received from the network
reason
string

Failure reason when the message reaches an error state. Empty string if no failure has occurred.

tags
string[]

Optional tags associated with this message.

isDone
boolean

true if the message has reached a terminal state (delivered, undelivered, or failed). Read-only property.

result
RelayEvent | null

The terminal RelayEvent that resolved this message, or null if the message has not yet completed.

Events

Events are emitted during the lifecycle of a message. Register handlers using message.on() to react to state changes on outbound messages.

See the Events reference for the full list of messaging events, their parameters, and typed event classes.


Methods

Examples

Listening for state changes

1import { RelayClient } from '@signalwire/sdk';
2
3const client = new RelayClient({
4 project: process.env.SIGNALWIRE_PROJECT_ID!,
5 token: process.env.SIGNALWIRE_TOKEN!,
6 contexts: ['default']
7});
8
9await client.connect();
10
11const message = await client.sendMessage({
12 to: '+15551234567',
13 from: '+15559876543',
14 body: 'Order confirmed',
15});
16
17const onStateChange = (event) => {
18 console.log(`Message ${message.messageId} -> ${message.state}`);
19};
20
21message.on(onStateChange);
22await message.wait();
23
24await client.disconnect();

Waiting for delivery with timeout

1import { RelayClient } from '@signalwire/sdk';
2
3const client = new RelayClient({
4 project: process.env.SIGNALWIRE_PROJECT_ID!,
5 token: process.env.SIGNALWIRE_TOKEN!,
6 contexts: ['default']
7});
8
9await client.connect();
10
11const message = await client.sendMessage({
12 to: '+15551234567',
13 from: '+15559876543',
14 body: 'Your verification code is 123456',
15});
16
17try {
18 const event = await message.wait(30_000);
19 if (message.state === 'delivered') {
20 console.log('Message delivered successfully');
21 } else {
22 console.log(`Message failed: ${message.reason}`);
23 }
24} catch (err) {
25 console.log('Timed out waiting for delivery confirmation');
26}
27
28await client.disconnect();