How to test SMS verification codes in automated tests

Last updated 2026-12-03 · By the Humanless team at Ninety Nine Oranges · Markdown version

Short answer: For most tests, stub or bypass SMS in your app (provider test credentials, or fictional test numbers with fixed codes) so they are fast and free. For the few tests that must prove a real SMS arrives, give each test a real phone number that your code can read: either rent a number from Twilio and read its inbound messages through their API, or use a service with dedicated test numbers and a blocking wait call, such as Humanless (channels: ["sms"], then waitForCode). Run as many SMS tests in parallel as you have numbers.

Use policy: Test numbers are for verifying phone flows in apps you own or are authorized to test. Humanless numbers are receive-only and must not be used to register on or get past verification on third-party services. See the Terms.

Pick the right layer

ApproachWhat it provesCostParallelism
Mock your SMS client in unit and integration testsYour code calls the provider correctlyFreeUnlimited
Provider test mode or fictional numbers (e.g. Firebase Auth test phone numbers with fixed codes)The UI flow works with a known codeFreeUnlimited
Your own Twilio number and reading inbound messagesA real SMS reaches a real numberNumber rental plus per-message fees on Twilio's pricing pageOne test per number at a time
Hosted test numbers with a wait API (Humanless, Mailosaur, MailSlurp)A real SMS arrives, and the code is extracted for youHumanless: $5 per number per 30 days plus $0.10 per SMS, or Builder $49/mo with 1 number and 500 SMSOne test per number at a time

A healthy suite has many tests at the first two layers and a handful of real-SMS tests (sign-up, login, phone change) at the last two.

Option 1: Your own Twilio number

If you already use Twilio, buy a number that can receive SMS and read what it receives. The simplest version polls the Messages API for messages sent to that number after the test started:

twilio-otp.ts
import twilio from "twilio";

const client = twilio(process.env.TWILIO_ACCOUNT_SID, process.env.TWILIO_AUTH_TOKEN);

export async function latestSmsCode(to: string, after: Date, timeoutMs = 60_000): Promise<string> {
  const deadline = Date.now() + timeoutMs;
  while (Date.now() < deadline) {
    const [msg] = await client.messages.list({ to, dateSentAfter: after, limit: 1 });
    const code = msg?.body.match(/\b(\d{6})\b/)?.[1];
    if (code) return code;
    await new Promise((r) => setTimeout(r, 2_000));
  }
  throw new Error(`No SMS to ${to} within ${timeoutMs} ms`);
}

This works, but you own the polling interval, the regex, the number inventory and the parallelism problem. Two tests sharing a number will read each other's codes. For push instead of polling, configure the number's incoming-message webhook and forward messages to your test runner, which is more infrastructure.

Option 2: Dedicated test numbers with a wait call (the Humanless way)

On a paid plan or with pay-as-you-go credits, your account gets dedicated US numbers. They are never shared with other customers, and they are quarantined before they are ever reassigned. An SMS identity leases one of your numbers for the test. waitForCode holds the request open and returns the code as soon as the SMS lands:

phone-verify.spec.ts
import { expect, test } from "@playwright/test";
import { Humanless, withIdentity } from "./humanless"; // TypeScript SDK, one file: usehumanless.com/sdk/humanless.ts

const hl = new Humanless({ apiKey: process.env.HUMANLESS_API_KEY! });

test("verify phone number by SMS", async ({ page }) => {
  await withIdentity(hl, { channels: ["sms"] }, async ({ identity_id, phone }) => {
    // phone is E.164, e.g. "+14155550192"
    await page.goto("http://localhost:3000/settings/phone");
    await page.getByLabel("Phone number").fill(phone!);
    await page.getByRole("button", { name: "Send code" }).click();
    const { code } = await hl.waitForCode({ identityId: identity_id, channel: "sms", timeout: 25 });
    await page.getByLabel("Code").fill(code);
    await page.getByRole("button", { name: "Verify" }).click();
    await expect(page.getByText("Phone verified")).toBeVisible();
  });
});

You can combine channels in one identity (channels: ["email", "sms"]) when sign-up asks for both. Then wait on each channel separately with channel: "email" and channel: "sms".

Over plain HTTP it's the same two calls as email:

curl
curl -s -X POST https://api.usehumanless.com/v1/identities \
  -H "Authorization: Bearer $HUMANLESS_API_KEY" -H "Content-Type: application/json" \
  -d '{"channels":["sms"]}' | tee identity.json
# -> {"identity_id":"...","phone":"+1415...","channels":["sms"], ...}
ID=$(jq -r .identity_id identity.json)

curl "https://api.usehumanless.com/v1/identities/$ID/wait?channel=sms&timeout=60" \
  -H "Authorization: Bearer $HUMANLESS_API_KEY"
# -> {"type":"otp","code":"700215","channel":"sms","sender":"+1...", ...}

Parallelism = number of numbers

Each SMS identity holds one of your numbers until it is deleted or expires (default 1 hour). After that the number cools down for 30 seconds before the next identity can use it. So:

Pitfalls

FAQ

How do I test SMS OTP in Playwright?

Get a phone number your test can read, enter it in the form, trigger the SMS, wait for the message without a fixed sleep, extract the code and fill it. With Humanless: createIdentity({ channels: ["sms"] }) → fill phone → click send → waitForCode({ identityId, channel: "sms" }) → fill code.

Can I test SMS verification without paying for SMS?

Yes, at the UI-flow level. Mock your SMS client, or use your auth provider's test numbers with fixed codes. You only need real numbers to prove that a real SMS is delivered.

Can I use a free temporary phone number website for tests?

Don't. Public "receive SMS online" numbers are shared with everyone, so your codes are visible to strangers. They are frequently blocked and can't be automated reliably. Use numbers dedicated to your account.

Can Humanless send SMS or receive calls?

No. Numbers are receive-only SMS. Humanless never sends SMS.

How many SMS tests can run in parallel?

One per dedicated number at a time. Builder includes 1 number, Pro includes 5, and you can add more at $5 per 30 days each. Delete identities as soon as the code arrives to free the number for the next test.

What happens to my numbers if I downgrade?

A number your account gives up is quarantined for 30 days before anyone else can get it, so late messages never reach another customer. Remove released numbers from any test accounts that depend on them.

Humanless: disposable inboxes, dedicated US SMS numbers and TOTP for testing apps you own. Email and TOTP are free.

Get an API key