How to test Twilio Verify and OTP flows end to end

· 5 min read · QuickInbox team

  • Twilio
  • OTP
  • SMS

If your app sends codes with Twilio Verify or your own OTP code on top of Twilio, you probably have unit tests. They're useful, but they never see a real text.

Two layers of testing

Unit and integration tests check your code: it calls the right API with the right number, handles errors, and accepts or refuses codes correctly. Twilio's test credentials and mocks are perfect here: fast, free and predictable.

End-to-end tests check the whole chain in a real environment: your config, your Twilio account, the template, the carrier, and the code your backend checks. Only a real text to a real number can test that.

What end-to-end tests catch

  • Wrong or expired API credentials in staging.
  • A Verify service or messaging service set up for the wrong country or channel.
  • A template change that breaks the code format your app expects.
  • Numbers saved in the wrong format (missing +1), so texts never send.
  • Rate limits or fraud settings that block your own test traffic.

Set up the end-to-end test

  1. Claim the US number included with a paid QuickInbox plan.
  2. Use it as the phone number of a staging test user.
  3. In your test, record the texts that already exist, trigger the code, wait for a new text and enter its code.
// sms.js - read texts sent to your QuickInbox test number (Node 18+)
const BASE = 'https://business.quickinbox.net';
const auth = { headers: { 'X-API-Key': process.env.QI_KEY } };

export async function getNumber() {
  const res = await fetch(`${BASE}/api/business/sms/numbers`, auth);
  const { numbers } = await res.json();
  if (!numbers?.length) throw new Error('No SMS number on this account yet');
  return numbers[0]; // { id, phone_number, ... }
}

async function listMessages(numberId) {
  const res = await fetch(`${BASE}/api/business/sms/numbers/${numberId}/messages`, auth);
  if (!res.ok) throw new Error(`SMS API ${res.status}`);
  return (await res.json()).messages; // newest first
}

// Remember which texts already exist, so the test only reads new ones.
export async function snapshot(numberId) {
  return new Set((await listMessages(numberId)).map((m) => m.id));
}

export async function waitForCode(numberId, seen, { timeoutMs = 90_000, pattern = /\b(\d{4,8})\b/ } = {}) {
  const deadline = Date.now() + timeoutMs;
  while (Date.now() < deadline) {
    const fresh = (await listMessages(numberId)).filter((m) => !seen.has(m.id));
    const hit = fresh.find((m) => pattern.test(m.body));
    if (hit) return hit.body.match(pattern)[1];
    await new Promise((r) => setTimeout(r, 3000));
  }
  throw new Error('No new SMS code arrived in time');
}

Then your test calls snapshot, clicks "Send code" and calls waitForCode. Full examples: Playwright, Cypress, Selenium.

Keep costs and noise down

  • Run the SMS end-to-end test on merges to main or nightly, and mocks on every commit.
  • Use one test user per number so codes never mix.
  • Allow-list your test number in any fraud or rate-limit rules in staging.

Related guides

Get a test number: every paid QuickInbox plan includes a real US phone number and a monthly SMS allowance, with plans from $9 a month. Extra packs start at $5 a month. See SMS testing and pricing.

← All articles