Prerequisites: A SMS number must be provisioned by Momentic.

SMS functionality is surfaced through the JavaScript step. Within Momentic’s JavaScript sandbox, your code can access the sms object. This object contains the following utility functions:

Sending SMS

The send utility function allows you to send an SMS message to a phone number. The phone number provided in the from argument must be issued by Momentic.

async function send(params: {
  body: string;
  to: string;
  from: string;
}): Promise<void>;

Fetching the latest SMS message

Make sure the function timeout is shorter than the JavaScript lambda timeout. The default timeout for both is 10 seconds.

The fetchLatest utility function retrieves the most recent SMS message sent to your phone number. You can filter messages based on the sender as well as the receipt date. This function will automatically retry until a matching message is found or the timeout is exhausted.

If multiple new SMS messages are received, only the most recently received message will be returned. To avoid race conditions, we recommend structuring your tests so that only a single SMS flow is active at any given time for each phone number allocated.

If you do not filter by receipt date, fetchLatest can return messages that were received before your test started (i.e. data generated from previous test runs).

fetchLatest will throw an error if no matching SMS message is received within the timeout.

async function fetchLatest(params: {
  to: string;
  from?: string;
  beforeDate?: Date;
  afterDate?: Date;
  timeout?: number; // milliseconds
}): Promise<{ body: string }>;