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` }),
});import os
from telnyx import Telnyx
otpmock = os.environ.get("OTPMOCK_URL")
telnyx = Telnyx(
api_key=os.environ["TELNYX_API_KEY"], # otpmock API key in tests
base_url=f"{otpmock}/v2" if otpmock else None,
)import com.telnyx.sdk.client.TelnyxClient;
import com.telnyx.sdk.client.okhttp.TelnyxOkHttpClient;
String otpmock = System.getenv("OTPMOCK_URL");
TelnyxOkHttpClient.Builder builder = TelnyxOkHttpClient.builder()
.apiKey(System.getenv("TELNYX_API_KEY")); // otpmock API key in tests
if (otpmock != null) builder.baseUrl(otpmock + "/v2"); // keep the /v2 suffix
TelnyxClient telnyx = builder.build();
// Messaging: telnyx.messages().send(...); Verify: telnyx.verifications().triggerSms(...)using Telnyx;
TelnyxConfiguration.SetApiKey(Environment.GetEnvironmentVariable("TELNYX_API_KEY")); // otpmock API key in tests
var otpmock = Environment.GetEnvironmentVariable("OTPMOCK_URL");
if (otpmock is not null) TelnyxConfiguration.SetApiBase(otpmock + "/v2");
var messages = new MessageService();use Telnyx\Client;
$otpmock = getenv('OTPMOCK_URL');
$telnyx = new Client(
apiKey: getenv('TELNYX_API_KEY'), // otpmock API key in tests
baseUrl: $otpmock ? rtrim($otpmock, '/') . '/v2' : null,
);require "telnyx"
telnyx = Telnyx::Client.new(
api_key: ENV["TELNYX_API_KEY"], # otpmock API key in tests
**(ENV["OTPMOCK_URL"] ? { base_url: "#{ENV["OTPMOCK_URL"]}/v2" } : {})
)opts := []option.RequestOption{option.WithAPIKey(os.Getenv("TELNYX_API_KEY"))} // otpmock API key in tests
if base := os.Getenv("OTPMOCK_URL"); base != "" {
opts = append(opts, option.WithBaseURL(base+"/v2/"))
}
client := telnyx.NewClient(opts...)Each language is tested in CI against otpmock with the provider's official SDK, or its plain HTTP API where no SDK can be pointed at another host.
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).
How to tell your app uses Telnyx
Look for any of these in your codebase to confirm where the SMS is sent, and therefore where the otpmock option has to be set:
telnyxin package.jsonclient.messages.send(client.verifications.triggerSMS(
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 300 messages a month. Using an AI coding assistant? Point it at otpmock.com/llms-full.txt.
Get a free API key