Forward SMS text messages to code and tests via Webhooks or HTTP

Create phone numbers that handle inbound SMS messages and direct them to your application or test suites

Services like Twilio and MailSlurp offer phone number and SMS APIs that can be used to build SMS routing functionality for automation and testing.

Create phone numbers

Create a phone number for a given region in the MailSlurp dashboard.

phone number api

You can then access and control the phone number using the MailSlurp API client.

const mailslurp = new MailSlurp({ apiKey });
// fetch a phone number (must create phone numbers in the dashboard first)
const {
  content: [phoneNumber],
} = await mailslurp.phoneController.getPhoneNumbers({
  size: 1,
  phoneCountry: GetPhoneNumbersPhoneCountryEnum.US,
});
expect(phoneNumber.phoneNumber).toEqual('+19108074451');

Redirect SMS with webhooks

Create a webhook for your phone number to route incoming SMS messages to your endpoint of choice, be that a server, slack, or lambda function.

// create a webhook for a phone number to have new SMS sent to your server
const webhook =
  await mailslurp.webhookController.createWebhookForPhoneNumber({
    phoneNumberId: phoneNumber.id,
    createWebhookOptions: {
      eventName: CreateWebhookOptionsEventNameEnum.NEW_SMS,
      url: 'https://myserver.com',
      name: 'Process SMS',
    },
  });

Wait for arrival of SMS

In tests or automation logic you can hold a connection open until an expected SMS txt message is received:

// wait for the latest unread sms
const [sms] = await mailslurp.waitController.waitForSms({
  waitForSmsConditions: {
    count: 1,
    unreadOnly: true,
    phoneNumberId: phoneNumber.id,
    timeout: 30_000,
  },
});
// extract a code from body with regex
expect(sms.body).toContain('Your code: 123');
const [, code] = /.+:\s([0-9]{3})/.exec(sms.body);
expect(code).toEqual('123');

Reading text messages in code

Extract SMS message content like so:

// fetch a message
const txtMessage = await mailslurp.smsController.getSmsMessage({
  smsId: sms.id,
});
expect(txtMessage.read).toEqual(true);
expect(txtMessage.fromNumber).toEqual('+13252527014');
// delete all messages in phone number
await mailslurp.smsController.deleteSmsMessages({
  phoneNumberId: sms.phoneNumber,
});

Related content

Test SMS phone numbers

Read text messages in code and webhooks to test applications and authentication end-to-end. Real phone numbers for developers and QA testers.

Best SMS apps for android

Best SMS apps for android

How to Schedule a Text Message

How to Schedule a Text Message

The Way to Ship a Text via Email (And an email through text): A Simple Guide

The Way to Ship a Text via Email (And an email through text): A Simple Guide

Instant messaging platforms compared

Use SMS/TXT messaging platforms to send and receive text at scale

User Authentication: Getting the Most Out of SMS OTP

User Authentication: Getting the Most Out of SMS OTP

Text Enabled Toll-Free Numbers: What all Businesses Should Know about Toll-Free SMS

Text Enabled Toll-Free Numbers: What all Businesses Should Know about Toll-Free SMS

What is an SMS message segment?

Text messages are split into SMS segments when sent.

What is SMS Authentication and is it Secure?

What is SMS Authentication and is it Secure?

Forward SMS text messages to code and tests via Webhooks or HTTP

Create phone numbers that handle inbound SMS messages and direct them to your application or test suites

How to test 2FA OTP login using SMS codes with Playwright

The ultimate guide to testing OAuth one-time-password flows with real SMS MFA. Use Playwright to automate authentication tests with programmable TXT message APIs.

Testing SMS based authentication (2FA/OTP)

How to test applications that use two-factor authentication and one-time TXT message passwords.

Receive inbound SMS TXT messages with API and webhooks

How to create phone numbers and read text messages in code and tests

Ready to dive in?Start building email applications today.