Skip to content

Use inbound TXT/SMS

MailSlurp supports creation and management of real mobile phone numbers in multiple regions. Phone numbers can receive inbound SMS text messages that can be read and received in code, tests, or online.

sms-basic

Creating phone numbers

First add a phone plan to your account subscription. Then provision a phone number for the plan in the MailSlurp dashboard.

Viewing numbers

Use the PhoneControllerApi to manage phone numbers.

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')

List and view SMS messages

Use the SmsControllerApi to view and download SMS messages

// 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
})

Wait for new SMS messages

Use the WaitForControllerApi waitForSms methods to hold a connection open until an expected count of SMS messages is present in a phone number

// 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')

Use SMS webhooks

Have SMS messages sent via HTTP POST to your server with TXT Webhooks

// 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',
    }
})