Twilio

@crvouga/mockingbird-service-twilio

Stateful emulator of Twilio Verify, Lookup v2, Messaging and Recordings on one port, with signed inbound SMS and voice webhooks (Mockingbird service contract).

Parity: Verify, Lookup, Messaging, and voiceCommunication80 of 80 operationsIn-process fetchNode servermockingbird-twilio
$
npm install -D @crvouga/mockingbird-service-twilio

Playground

Requests go to the real emulator running in this tab. State persists across requests, and the journal shows what it received. A green dot marks operations whose sample succeeds as-is; others need ids from earlier responses.

Loads the real @crvouga/mockingbird-service-twilio into this tab on first send
https://api.twilio.com

AccountSid (or an SK API key sid) as the username, AuthToken (or key secret) as the password.

Send a request to see the emulator's response.

Operations

80 of 80 operations in the vendored contract are emulated. Trimmed from Twilio's public API reference (twilio-oai) to the operations our consumer calls (twilio.service.ts, twilio-conversation-sms.adapter.ts, notification-dispatcher channels/sms.ts, twilio-recording-http.adapter.ts); Lookup v2 shapes verified against the live API.

MethodPathOperationStatus
GET/lookups/v2/PhoneNumbers/{PhoneNumber}FetchPhoneNumber
POST/verify/v2/Services/{ServiceSid}/VerificationsCreateVerification
GET/verify/v2/Services/{ServiceSid}/Verifications/{Sid}FetchVerification
POST/verify/v2/Services/{ServiceSid}/Verifications/{Sid}UpdateVerification
POST/verify/v2/Services/{ServiceSid}/VerificationCheckCreateVerificationCheck
GET/api/2010-04-01/Accounts/{AccountSid}/Messages.jsonListMessages
POST/api/2010-04-01/Accounts/{AccountSid}/Messages.jsonCreateMessage
GET/api/2010-04-01/Accounts/{AccountSid}/Messages/{Sid}.jsonFetchMessage
POST/api/2010-04-01/Accounts/{AccountSid}/Messages/{Sid}.jsonPOST /api/2010-04-01/Accounts/{accountSid}/Messages/{messageSid}.jsonPOST /2010-04-01/Accounts/:accountSid/Messages/:messageSid.json
DELETE/api/2010-04-01/Accounts/{AccountSid}/Messages/{Sid}.jsonDELETE /api/2010-04-01/Accounts/{accountSid}/Messages/{messageSid}.jsonDELETE /2010-04-01/Accounts/:accountSid/Messages/:messageSid.json
GET/api/2010-04-01/Accounts/{AccountSid}/Recordings/{Sid}.wavFetchRecordingMedia
GET/api/2010-04-01/Accounts/{AccountSid}/Recordings/{Sid}.jsonFetchRecording
DELETE/api/2010-04-01/Accounts/{AccountSid}/Recordings/{Sid}.jsonDeleteRecording
GET/2010-04-01/Accounts/{accountSid}/Calls/{callSid}.jsonGET /2010-04-01/Accounts/:accountSid/Calls/:callSid.jsonGET /2010-04-01/Accounts/:accountSid/Calls/:callSid.json
POST/2010-04-01/Accounts/{accountSid}/Calls/{callSid}.jsonPOST /2010-04-01/Accounts/:accountSid/Calls/:callSid.jsonPOST /2010-04-01/Accounts/:accountSid/Calls/:callSid.json
DELETE/2010-04-01/Accounts/{accountSid}/Calls/{callSid}.jsonDELETE /2010-04-01/Accounts/:accountSid/Calls/:callSid.jsonDELETE /2010-04-01/Accounts/:accountSid/Calls/:callSid.json
GET/2010-04-01/Accounts/{accountSid}/IncomingPhoneNumbers/{sid}.jsonGET /2010-04-01/Accounts/:accountSid/IncomingPhoneNumbers/:sid.jsonGET /2010-04-01/Accounts/:accountSid/IncomingPhoneNumbers/:sid.json
POST/2010-04-01/Accounts/{accountSid}/IncomingPhoneNumbers/{sid}.jsonPOST /2010-04-01/Accounts/:accountSid/IncomingPhoneNumbers/:sid.jsonPOST /2010-04-01/Accounts/:accountSid/IncomingPhoneNumbers/:sid.json
DELETE/2010-04-01/Accounts/{accountSid}/IncomingPhoneNumbers/{sid}.jsonDELETE /2010-04-01/Accounts/:accountSid/IncomingPhoneNumbers/:sid.jsonDELETE /2010-04-01/Accounts/:accountSid/IncomingPhoneNumbers/:sid.json
GET/2010-04-01/Accounts/{accountSid}/Messages/{messageSid}.jsonGET /2010-04-01/Accounts/:accountSid/Messages/:messageSid.jsonGET /2010-04-01/Accounts/:accountSid/Messages/:messageSid.json
POST/2010-04-01/Accounts/{accountSid}/Messages/{messageSid}.jsonPOST /2010-04-01/Accounts/:accountSid/Messages/:messageSid.jsonPOST /2010-04-01/Accounts/:accountSid/Messages/:messageSid.json
DELETE/2010-04-01/Accounts/{accountSid}/Messages/{messageSid}.jsonDELETE /2010-04-01/Accounts/:accountSid/Messages/:messageSid.jsonDELETE /2010-04-01/Accounts/:accountSid/Messages/:messageSid.json
GET/conversations/v1/Services/{serviceSid}/Conversations/{conversationSid}GET /conversations/v1/Services/:serviceSid/Conversations/:conversationSidGET /conversations/v1/Services/:serviceSid/Conversations/:conversationSid
POST/conversations/v1/Services/{serviceSid}/Conversations/{conversationSid}POST /conversations/v1/Services/:serviceSid/Conversations/:conversationSidPOST /conversations/v1/Services/:serviceSid/Conversations/:conversationSid
DELETE/conversations/v1/Services/{serviceSid}/Conversations/{conversationSid}DELETE /conversations/v1/Services/:serviceSid/Conversations/:conversationSidDELETE /conversations/v1/Services/:serviceSid/Conversations/:conversationSid
GET/messaging/v1/Services/{serviceSid}GET /messaging/v1/Services/:serviceSidGET /messaging/v1/Services/:serviceSid
POST/messaging/v1/Services/{serviceSid}POST /messaging/v1/Services/:serviceSidPOST /messaging/v1/Services/:serviceSid
DELETE/messaging/v1/Services/{serviceSid}DELETE /messaging/v1/Services/:serviceSidDELETE /messaging/v1/Services/:serviceSid
DELETE/messaging/v1/Services/{serviceSid}/PhoneNumbers/{sid}DELETE /messaging/v1/Services/:serviceSid/PhoneNumbers/:sidDELETE /messaging/v1/Services/:serviceSid/PhoneNumbers/:sid
GET/verify/v2/Services/{serviceSid}GET /verify/v2/Services/:serviceSidGET /verify/v2/Services/:serviceSid
POST/verify/v2/Services/{serviceSid}POST /verify/v2/Services/:serviceSidPOST /verify/v2/Services/:serviceSid
DELETE/verify/v2/Services/{serviceSid}DELETE /verify/v2/Services/:serviceSidDELETE /verify/v2/Services/:serviceSid
GET/GET /GET /
GET/2010-04-01/Accounts.jsonGET /2010-04-01/Accounts.jsonGET /2010-04-01/Accounts.json
GET/2010-04-01/Accounts/{accountSid}.jsonGET /2010-04-01/Accounts/:accountSid.jsonGET /2010-04-01/Accounts/:accountSid.json
POST/2010-04-01/Accounts/{accountSid}.jsonPOST /2010-04-01/Accounts/:accountSid.jsonPOST /2010-04-01/Accounts/:accountSid.json
GET/2010-04-01/Accounts/{accountSid}/Calls.jsonGET /2010-04-01/Accounts/:accountSid/Calls.jsonGET /2010-04-01/Accounts/:accountSid/Calls.json
POST/2010-04-01/Accounts/{accountSid}/Calls.jsonPOST /2010-04-01/Accounts/:accountSid/Calls.jsonPOST /2010-04-01/Accounts/:accountSid/Calls.json
GET/2010-04-01/Accounts/{accountSid}/IncomingPhoneNumbers.jsonGET /2010-04-01/Accounts/:accountSid/IncomingPhoneNumbers.jsonGET /2010-04-01/Accounts/:accountSid/IncomingPhoneNumbers.json
POST/2010-04-01/Accounts/{accountSid}/IncomingPhoneNumbers.jsonPOST /2010-04-01/Accounts/:accountSid/IncomingPhoneNumbers.jsonPOST /2010-04-01/Accounts/:accountSid/IncomingPhoneNumbers.json
GET/2010-04-01/Accounts/{accountSid}/Messages.jsonGET /2010-04-01/Accounts/:accountSid/Messages.jsonGET /2010-04-01/Accounts/:accountSid/Messages.json
POST/2010-04-01/Accounts/{accountSid}/Messages.jsonPOST /2010-04-01/Accounts/:accountSid/Messages.jsonPOST /2010-04-01/Accounts/:accountSid/Messages.json
GET/2010-04-01/Accounts/{accountSid}/Messages/{messageSid}/Media.jsonGET /2010-04-01/Accounts/:accountSid/Messages/:messageSid/Media.jsonGET /2010-04-01/Accounts/:accountSid/Messages/:messageSid/Media.json
GET/2010-04-01/Accounts/{accountSid}/Messages/{messageSid}/Media/{mediaSid}.jsonGET /2010-04-01/Accounts/:accountSid/Messages/:messageSid/Media/:mediaSid.jsonGET /2010-04-01/Accounts/:accountSid/Messages/:messageSid/Media/:mediaSid.json
GET/_twilio/simulate/verification-codeGET /_twilio/simulate/verification-codeGET /_twilio/simulate/verification-code
GET/conversations/v1/ServicesGET /conversations/v1/ServicesGET /conversations/v1/Services
POST/conversations/v1/ServicesPOST /conversations/v1/ServicesPOST /conversations/v1/Services
GET/conversations/v1/Services/{serviceSid}GET /conversations/v1/Services/:serviceSidGET /conversations/v1/Services/:serviceSid
GET/conversations/v1/Services/{serviceSid}/ConversationsGET /conversations/v1/Services/:serviceSid/ConversationsGET /conversations/v1/Services/:serviceSid/Conversations
POST/conversations/v1/Services/{serviceSid}/ConversationsPOST /conversations/v1/Services/:serviceSid/ConversationsPOST /conversations/v1/Services/:serviceSid/Conversations
GET/conversations/v1/Services/{serviceSid}/Conversations/{conversationSid}/MessagesGET /conversations/v1/Services/:serviceSid/Conversations/:conversationSid/MessagesGET /conversations/v1/Services/:serviceSid/Conversations/:conversationSid/Messages
POST/conversations/v1/Services/{serviceSid}/Conversations/{conversationSid}/MessagesPOST /conversations/v1/Services/:serviceSid/Conversations/:conversationSid/MessagesPOST /conversations/v1/Services/:serviceSid/Conversations/:conversationSid/Messages
GET/conversations/v1/Services/{serviceSid}/Conversations/{conversationSid}/ParticipantsGET /conversations/v1/Services/:serviceSid/Conversations/:conversationSid/ParticipantsGET /conversations/v1/Services/:serviceSid/Conversations/:conversationSid/Participants
POST/conversations/v1/Services/{serviceSid}/Conversations/{conversationSid}/ParticipantsPOST /conversations/v1/Services/:serviceSid/Conversations/:conversationSid/ParticipantsPOST /conversations/v1/Services/:serviceSid/Conversations/:conversationSid/Participants
GET/messaging/v1/ServicesGET /messaging/v1/ServicesGET /messaging/v1/Services
POST/messaging/v1/ServicesPOST /messaging/v1/ServicesPOST /messaging/v1/Services
GET/messaging/v1/Services/{serviceSid}/PhoneNumbersGET /messaging/v1/Services/:serviceSid/PhoneNumbersGET /messaging/v1/Services/:serviceSid/PhoneNumbers
POST/messaging/v1/Services/{serviceSid}/PhoneNumbersPOST /messaging/v1/Services/:serviceSid/PhoneNumbersPOST /messaging/v1/Services/:serviceSid/PhoneNumbers
GET/verify/v2/ServicesGET /verify/v2/ServicesGET /verify/v2/Services
POST/verify/v2/ServicesPOST /verify/v2/ServicesPOST /verify/v2/Services
POST/_twilio/simulate/call-statusPOST /_twilio/simulate/call-statusPOST /_twilio/simulate/call-status
POST/_twilio/simulate/inbound-callPOST /_twilio/simulate/inbound-callPOST /_twilio/simulate/inbound-call
POST/_twilio/simulate/inbound-messagePOST /_twilio/simulate/inbound-messagePOST /_twilio/simulate/inbound-message
POST/_twilio/simulate/message-statusPOST /_twilio/simulate/message-statusPOST /_twilio/simulate/message-status
POST/_twilio/simulate/verification-statusPOST /_twilio/simulate/verification-statusPOST /_twilio/simulate/verification-status
GET/api/2010-04-01/Accounts/{accountSid}/Calls/{callSid}.jsonGET /api/2010-04-01/Accounts/{accountSid}/Calls/{callSid}.jsonGET /2010-04-01/Accounts/:accountSid/Calls/:callSid.json
POST/api/2010-04-01/Accounts/{accountSid}/Calls/{callSid}.jsonPOST /api/2010-04-01/Accounts/{accountSid}/Calls/{callSid}.jsonPOST /2010-04-01/Accounts/:accountSid/Calls/:callSid.json
DELETE/api/2010-04-01/Accounts/{accountSid}/Calls/{callSid}.jsonDELETE /api/2010-04-01/Accounts/{accountSid}/Calls/{callSid}.jsonDELETE /2010-04-01/Accounts/:accountSid/Calls/:callSid.json
GET/api/2010-04-01/Accounts/{accountSid}/IncomingPhoneNumbers/{sid}.jsonGET /api/2010-04-01/Accounts/{accountSid}/IncomingPhoneNumbers/{sid}.jsonGET /2010-04-01/Accounts/:accountSid/IncomingPhoneNumbers/:sid.json
POST/api/2010-04-01/Accounts/{accountSid}/IncomingPhoneNumbers/{sid}.jsonPOST /api/2010-04-01/Accounts/{accountSid}/IncomingPhoneNumbers/{sid}.jsonPOST /2010-04-01/Accounts/:accountSid/IncomingPhoneNumbers/:sid.json
DELETE/api/2010-04-01/Accounts/{accountSid}/IncomingPhoneNumbers/{sid}.jsonDELETE /api/2010-04-01/Accounts/{accountSid}/IncomingPhoneNumbers/{sid}.jsonDELETE /2010-04-01/Accounts/:accountSid/IncomingPhoneNumbers/:sid.json
GET/api/2010-04-01/Accounts.jsonGET /api/2010-04-01/Accounts.jsonGET /2010-04-01/Accounts.json
GET/api/2010-04-01/Accounts/{accountSid}.jsonGET /api/2010-04-01/Accounts/{accountSid}.jsonGET /2010-04-01/Accounts/:accountSid.json
POST/api/2010-04-01/Accounts/{accountSid}.jsonPOST /api/2010-04-01/Accounts/{accountSid}.jsonPOST /2010-04-01/Accounts/:accountSid.json
GET/api/2010-04-01/Accounts/{accountSid}/Calls.jsonGET /api/2010-04-01/Accounts/{accountSid}/Calls.jsonGET /2010-04-01/Accounts/:accountSid/Calls.json
POST/api/2010-04-01/Accounts/{accountSid}/Calls.jsonPOST /api/2010-04-01/Accounts/{accountSid}/Calls.jsonPOST /2010-04-01/Accounts/:accountSid/Calls.json
GET/api/2010-04-01/Accounts/{accountSid}/IncomingPhoneNumbers.jsonGET /api/2010-04-01/Accounts/{accountSid}/IncomingPhoneNumbers.jsonGET /2010-04-01/Accounts/:accountSid/IncomingPhoneNumbers.json
POST/api/2010-04-01/Accounts/{accountSid}/IncomingPhoneNumbers.jsonPOST /api/2010-04-01/Accounts/{accountSid}/IncomingPhoneNumbers.jsonPOST /2010-04-01/Accounts/:accountSid/IncomingPhoneNumbers.json
GET/api/2010-04-01/Accounts/{accountSid}/Messages/{messageSid}/Media.jsonGET /api/2010-04-01/Accounts/{accountSid}/Messages/{messageSid}/Media.jsonGET /2010-04-01/Accounts/:accountSid/Messages/:messageSid/Media.json
GET/api/2010-04-01/Accounts/{accountSid}/Messages/{messageSid}/Media/{mediaSid}.jsonGET /api/2010-04-01/Accounts/{accountSid}/Messages/{messageSid}/Media/{mediaSid}.jsonGET /2010-04-01/Accounts/:accountSid/Messages/:messageSid/Media/:mediaSid.json

Documentation

The package README, the same file that ships in the npm tarball.Edit on GitHub

Local emulators. Real API contracts. Part of Mockingbird.

Stateful emulator of Twilio for test suites: Lookup v2 phone validation, Verify v2 phone OTP (with the real state machine: wrong codes, attempt limits, 10-minute expiry on the emulator clock), Programmable Messaging with an outbox, and call Recordings. It also signs and posts Twilio's inbound SMS and voice webhooks to your app. Every product is served on one port. A suite reads the OTP from the admin plane instead of bypassing verification (E2E_OTP_BYPASS_*).

  • Operation coverage: SUPPORT.md
  • The contract (openapi.yaml) is trimmed from Twilio's API reference to what our consumer calls. Lookup v2 responses are checked byte for byte against the live API (scripts/parity.ts, recorded in test/fixtures/lookups.live.json).

Install

Shell
npm install -D @crvouga/mockingbird-service-twilio

ESM only. Node >= 22 or Bun >= 1.2. Lookup validation uses libphonenumber-js (Twilio's Lookup is libphonenumber). Serve it with npx mockingbird-twilio serve, with createServer from ./server (Node), or with createRuntime in any Fetch server.

Usage

Shell
npx mockingbird-twilio serve --port 8798 \
  --app-url http://127.0.0.1:3000 \
  --public-base-url "$TWILIO_VOICE_WEBHOOK_BASE_URL" \
  --account-sid "$TWILIO_ACCOUNT_SID" --auth-token "$TWILIO_AUTH_TOKEN" \
  --caller-id "$TWILIO_VOICE_CALLER_ID"

Pointing the app at it (G-T1)

twilio-node builds a host for each product (api.twilio.com, verify.twilio.com, lookups.twilio.com). The emulator takes that host as the first path segment: {emulator}/api/2010-04-01/…, {emulator}/verify/v2/…, {emulator}/lookups/v2/…. Pass every new Twilio(...) an httpClient that rewrites the URL. twilioMockUrl does the rewrite:

TypeScript
import twilio from "twilio"
import { twilioMockUrl } from "@crvouga/mockingbird-service-twilio"
import { createServer } from "@crvouga/mockingbird-service-twilio/server"

const server = await createServer() // or process.env.TWILIO_API_BASE_URL, e.g. http://127.0.0.1:8798

type RequestOptions = Parameters<twilio.RequestClient["request"]>[0]

/** https://verify.twilio.com/v2/Services/VA…/Verifications → {base}/verify/v2/Services/VA…/Verifications */
class MockRequestClient extends twilio.RequestClient {
  override request(opts: RequestOptions): Promise<never> {
    const uri = twilioMockUrl(opts.uri, server.url)
    return super.request({ ...opts, uri }) as Promise<never>
  }
}

// The app's own Twilio client. Only its HTTP client changes.
const client = twilio("AC00000000000000000000000000000000", "fixture-token", {
  httpClient: new MockRequestClient(),
})
const lookup = await client.lookups.v2.phoneNumbers("+12025550123").fetch()
// lookup.valid === true, lookup.nationalFormat === "(202) 555-0123"
await server.close()

The example type-checks with twilio 5.13.1 or later; 5.10.0 runs it the same way, but its exports map hides the typings from moduleResolution: nodenext.

The emulator also routes by the Host header. A request that reaches it as lookups.twilio.com (through a DNS override or a proxy) and has no prefix is served as /lookups/…. The recording adapter's raw fetch of https://api.twilio.com/2010-04-01/… becomes twilioMockUrl(url, TWILIO_API_BASE_URL). RecordingUrl in webhooks keeps the canonical https://api.twilio.com form that the adapter checks.

Reading the OTP in a test

TypeScript
import twilio from "twilio"
import { twilioMockUrl } from "@crvouga/mockingbird-service-twilio"
import { createServer } from "@crvouga/mockingbird-service-twilio/server"

// Test setup: the emulator, and the app's Twilio client pointed at it as above.
const server = await createServer()
type RequestOptions = Parameters<twilio.RequestClient["request"]>[0]
class MockRequestClient extends twilio.RequestClient {
  override request(opts: RequestOptions): Promise<never> {
    const uri = twilioMockUrl(opts.uri, server.url)
    return super.request({ ...opts, uri }) as Promise<never>
  }
}
const client = twilio("AC00000000000000000000000000000000", "fixture-token", {
  httpClient: new MockRequestClient(),
})
const verify = client.verify.v2.services("VA00000000000000000000000000000000")

// The app sends the code.
const verification = await verify.verifications.create({ to: "+12025550123", channel: "sms" })

// The test reads it from the admin plane, the way a user reads the SMS.
const latest = await fetch(`${server.url}/__admin/verify/+12025550123/latest`)
const { code } = (await latest.json()) as { code: string; sid: string; status: string }

// The app checks it.
const check = await verify.verificationChecks.create({ verificationSid: verification.sid, code })
// check.status === "approved"
await server.close()

Routes

Route Behaviour
GET /lookups/v2/PhoneNumbers/{PhoneNumber} Validates the number the way the live API does (libphonenumber): valid, validation_errors (TOO_SHORT, TOO_LONG, INVALID_BUT_POSSIBLE, INVALID_COUNTRY_CODE, INVALID_LENGTH, NOT_A_NUMBER), phone_number, national_format, calling_country_code, country_code, url. Every data package is null. A national number with no CountryCode must be a valid US number, otherwise the answer is INVALID_COUNTRY_CODE. Fictional 555-01xx numbers are valid.
POST /verify/v2/Services/{VA}/Verifications To (valid E.164, or an email for Channel=email), Channel, optional CustomCode. Creates a VE… verification with a 6-digit code (or fixedCode). Sending again to the same pending verification keeps its sid and code and adds a send_code_attempts entry. The 6th send to one number within 10 minutes is 429 60203. A bad To is 400 60200. An unknown service sid (not VA + 32 hex) is 404 20404.
POST /verify/v2/Services/{VA}/VerificationCheck Code plus VerificationSid or To. The right code returns 200 approved, valid: true. A wrong code returns 200 pending and counts an attempt. The 6th check is 429 60202. A missing, approved, canceled or expired (10 minutes on the emulator clock) verification is 404 20404.
GET / POST /verify/v2/Services/{VA}/Verifications/{VE} Fetches a verification, or sets Status=canceled|approved on one.
POST /api/2010-04-01/Accounts/{AC}/Messages.json To, Body or MediaUrl, and From or MessagingServiceSid. Returns 201 with an SM… sid (MM… with media) and RFC 2822 dates. MessagingServiceSid alone is status accepted, from: null, num_segments: "0" (a sender is not chosen yet). From alone is status queued. Both are kept and the status is queued (Twilio queues on a specific sender). There is no idempotency key: the same body creates another sid. Errors: 21604 (no To), 21602 (no Body), 21603 (no From), 21211 (invalid To), 21617 (over 1600 characters). The message is recorded in the outbox.
GET /api/2010-04-01/Accounts/{AC}/Messages/{SM}.json Reads a message back.
GET /api/2010-04-01/Accounts/{AC}/Messages.json Newest-first Message resources for that account. PageSize defaults to 50 (1–1000), PageToken follows next_page_uri/previous_page_uri; Page is client state. Links keep the vendor path without /api, so apply the normal product-prefix rewrite. To and From filter stored resources.
GET /api/2010-04-01/Accounts/{AC}/Recordings/{RE}.wav The recording as a RIFF WAV. With RequestedChannels=2 you get both channels. Otherwise a dual-channel recording is mixed down to mono, as Twilio does.
GET / DELETE /api/2010-04-01/Accounts/{AC}/Recordings/{RE}.json Metadata, or a delete (204; a second delete is 404, which our consumer tolerates).

Errors use Twilio's body, {code, message, more_info, status}, with X-Twilio-Error-Code. Authentication is HTTP Basic: an AC… (or SK…) username and a non-empty password. With accounts (or --account-sid plus --auth-token) only those pairs are accepted. A missing or wrong credential is 401 20003. For a wrong token the message matches the live one: authentication failed, auth token is not valid for account AC….

Webhooks (S8.4)

The emulator signs inbound webhooks with X-Twilio-Signature. The signature is base64 HMAC-SHA1(auth token, public URL + sorted key+value params), the same computation as twilio.validateRequest. It signs against the public base URL the app is configured with (--public-base-url, i.e. TWILIO_VOICE_WEBHOOK_BASE_URL), not the localhost address it posts to. Twilio does not retry these webhooks, so the emulator sends each one once.

Trigger Posts to Payload
POST /__admin/inbound/sms {from, body, to?, media?, messageSid?, params?} /messaging/inbound/sms MessageSid/SmsSid (SM…, or MM… with media), AccountSid, From, To (default: --caller-id), Body, NumMedia, MediaUrl{i}, MediaContentType{i}, SmsStatus=received, …
POST /__admin/voice/twiml /admin/messaging/voice/twiml CallSid, From=client:<uuid>, To, CallStatus=ringing plus the body's fields (e.g. conversationId)
POST /__admin/voice/disclosure /admin/messaging/voice/disclosure CallSid, CallStatus=in-progress
POST /__admin/voice/status /admin/messaging/voice/status CallSid, CallStatus=completed, CallDuration
POST /__admin/voice/recording /admin/messaging/voice/recording RecordingSid, canonical RecordingUrl, RecordingStatus=completed, RecordingDuration, RecordingChannels=2. It also stores a dual-channel WAV so the download works.

Every body field overrides a default. The admin answer lists each delivery with the app's status and response body (e.g. <Response/>). Event types for PUT /__admin/webhook-endpoints filters are sms.inbound and voice.{twiml,disclosure,status,recording}.

Admin (beyond the standard contract)

Route Effect
GET /__admin/verify/:e164/latest {code, sid, status, to, channel, serviceSid, attempts, sendAttempts, createdAt, expiresAt} for the newest verification to that number. This is how a test reads the OTP.
GET / PUT /__admin/verify {fixedCode: "000000" | null, ttlSeconds, maxCheckAttempts, maxSendAttempts, sendWindowSeconds} for the calling namespace.
PUT /__admin/lookups/:e164 {valid, validationErrors?} overrides Lookup for that number (Verify and Messages use it too). DELETE removes the override.
PUT /__admin/recordings/:RE Uploads a recording: a raw WAV body (content-type: audio/wav), {"wavBase64": …}, or {channels?, seconds?} for a synthesised 8 kHz 16-bit tone.
GET /__admin/outbox?to=&since=&kind=sms|verify Everything "sent": SMS ({to, from, messagingServiceSid, body, sid}) and every Verify code delivery ({code, body: "Your verification code is: …"}).
GET /__admin/messages Message resources in the namespace.

Fault presets (POST /__admin/faults {"preset": "<name>", "count"?: n, "latencyMs"?: n}; list them with GET /__admin/faults/presets, and read hits / remaining from GET /__admin/faults). A preset applies to the calling namespace. verify_5xx (start and check answer 500 20500), sms_drop_before_accept (Messages.json drops the socket before a Message exists; still registered as sms_socket_drop), sms_accepted_then_socket_drop (the Message and outbox entry are stored, then the socket closes before any response bytes), sms_4xx (400 21211), sms_429 (429 20429 with Retry-After: 1), sms_5xx (500 20500, no Retry-After), sms_missing_sid (201 whose body omits sid; the outbox still has the message), lookup_5xx (503 20503; the EMR fails open), webhook_duplicate, webhook_drop. A 503 on Messages.json is the same envelope with code 20503 and Retry-After (a standard fault, not a separate preset). None of the status faults store a message.

Namespaces

The Twilio SDK cannot add headers, so a suite picks a namespace by AccountSid: PUT /__admin/credentials {"credentials": {"<TWILIO_ACCOUNT_SID>": "<namespace>"}}. A namespace can also come from the x-mockingbird-namespace header or a /__admin/ns/<name> prefix on the base URL (twilioMockUrl keeps the prefix).

Deliberately not modelled

  • Delivery: messages stay queued/accepted, no status callbacks are sent (our consumer sets no statusCallback), and no voice calls, conferences or media streams happen.
  • Message collection DateSent filters (messages are never sent by the emulator).
  • Paid Lookup data packages (Fields=line_type_intelligence, caller_name, …) are always null.
  • Verify channels other than SMS are accepted and recorded, but nothing is delivered. Verify Service configuration (code length, friendly name, rate-limit buckets) is global, set with PUT /__admin/verify.
  • Accounts: any AC… sid is its own account, and the path's {AccountSid} is not cross-checked against the credential.
  • libphonenumber-js drops "local only" lengths from its metadata. The emulator restores them for NANP (7) and GB (4–6, 8), which is enough for the fictional numbers tests use. Other countries may answer TOO_SHORT where Twilio answers INVALID_BUT_POSSIBLE.

API

Export Kind Description
TwilioAPI class The in-process emulator: fetch(request), reset(), latestVerification(to), resolveLookup(raw, country?), setLookup(e164, override), putRecording(sid, input), outbox(), messages(). Options: sqlite, now, namespace, verify, accounts.
createRuntime function The emulator with the full service contract (health, admin, namespaces, credentials, presets, outbox, webhooks), plus host routing. Options: app: {url, publicBaseUrl?, authToken, accountSid?, callerId?, messagingServiceSid?}, verify, accounts, retryDelaysMs, fetch, clock, seed, adminKey, onLog. The returned runtime also has inboundSms(input) and voiceWebhook(kind, params).
TWILIO_PRESETS object Every named fault preset.
TWILIO_WEBHOOK_EVENTS object Webhook event type → the app path it posts to.
twilioMockUrl function Rewrites an upstream https://<product>.twilio.com/… URL onto an emulator base URL (for the G-T1 httpClient).
TWILIO_PRODUCTS array The product prefixes served: api, verify, lookups.
lookup function Lookup v2 validation of a raw input (optionally in a CountryCode region).
e164Key function + and digits: the key that admin overrides use.
twilioError function A Twilio error response, {code, message, more_info, status}.
DEFAULT_VERIFY_SETTINGS object Verify defaults: 10-minute expiry, 5 checks, 5 sends per 10 minutes, random codes.
DEFAULT_ACCOUNT_SID string The account used when the credential is an API key.
TWILIO_NAMESPACE string The service name, "twilio".
synthesizeWav, readWav, mixDownToMono functions Make a PCM WAV, read its format, mix a multi-channel WAV down to mono.
document, operationIds, supportedOperationIds values The vendored OpenAPI contract and its operation ids.
createServer, serveTarget, DEFAULT_PORT (./server) Node Serve over node:http; the serve CLI target; port 8798.

Part of mockingbird.

↑↓ to navigate↵ to open