Set OTPMOCK_URL=https://api.otpmock.com in your test environment, point the Telnyx client at it, and use your otpmock API key as the Telnyx credential. Your application code doesn't change, and production keeps sending real SMS through Telnyx.
Setup
The SDK has a documented baseURL option (and reads TELNYX_BASE_URL). Keep the /v2 suffix.
import Telnyx from "telnyx";
export const telnyx = new Telnyx({
apiKey: process.env.TELNYX_API_KEY, // otpmock API key in tests
...(process.env.OTPMOCK_URL && { baseURL: `${process.env.OTPMOCK_URL}/v2` }),
});Credentials. In the test environment, set your Telnyx API key variable to your otpmock API key.
What otpmock emulates
| Endpoint | What it does |
|---|---|
POST /v2/messages | Send an SMS |
POST /v2/verifications/sms | Verify: send a code (otpmock generates it; custom_code is honoured) |
POST /v2/verifications/by_phone_number/{phone}/actions/verify | Verify: check a code (response_code accepted / rejected) |
Responses and errors follow Telnyx's own format, so the SDK parses them as usual. Authentication failures and an exhausted monthly allowance also come back in Telnyx's error shape.
Phone numbers
Send in E.164 (+15550142).
Read the code in your tests
const phone = otp.randomPhone();
const since = Date.now() - 5_000;
await page.getByRole("button", { name: "Send code" }).click(); // your app calls Telnyx
const { code } = await otp.waitForCode(phone, { since });The otp helper is a single file: get it from the docs. Full walkthroughs: Playwright, Cypress.
FAQ
Does otpmock send real SMS through Telnyx?
No. In the test environment your app's Telnyx requests go to otpmock, which stores the message for 10 minutes and never contacts Telnyx or any carrier. Production keeps using Telnyx because otpmock is only enabled when OTPMOCK_URL is set.
Is the official telnyx SDK supported?
Yes. otpmock's Telnyx endpoints are tested end to end with the official telnyx package for Node.js. Other languages work too if their Telnyx client lets you change the API host.
Which Telnyx APIs does otpmock support?
POST /v2/messages (Send an SMS); POST /v2/verifications/sms (Verify: send a code (otpmock generates it; custom_code is honoured)); POST /v2/verifications/by_phone_number/{phone}/actions/verify (Verify: check a code (response_code accepted / rejected)).
The free plan includes 100 messages a month. Using an AI coding assistant? Point it at otpmock.com/llms-full.txt.
Get a free API key