Set OTPMOCK_URL=https://api.otpmock.com in your test environment, point the Bandwidth client at it, and use your otpmock API key as the Bandwidth credential. Your application code doesn't change, and production keeps sending real SMS through Bandwidth.
Setup
The SDK's basePath option is ignored for sending messages, because each operation has its own server URL. Override that URL from bandwidth-sdk/dist/base once at startup in the test environment.
import { Configuration, MessagesApi } from "bandwidth-sdk";
import { operationServerMap } from "bandwidth-sdk/dist/base";
if (process.env.OTPMOCK_URL) {
operationServerMap["MessagesApi.createMessage"][0].url = `${process.env.OTPMOCK_URL}/api/v2`;
}
export const bandwidth = new MessagesApi(
new Configuration({
username: process.env.BANDWIDTH_USERNAME,
password: process.env.BANDWIDTH_PASSWORD, // otpmock API key in tests
}),
);Credentials. In the test environment, use your otpmock API key as the Bandwidth password (Basic auth). Don't configure OAuth client credentials in tests.
What otpmock emulates
| Endpoint | What it does |
|---|---|
POST /api/v2/users/{accountId}/messages | Send a message (applicationId, to[], from, text), 202 |
Responses and errors follow Bandwidth's own format, so the SDK parses them as usual. Authentication failures and an exhausted monthly allowance also come back in Bandwidth'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 Bandwidth
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 Bandwidth?
No. In the test environment your app's Bandwidth requests go to otpmock, which stores the message for 10 minutes and never contacts Bandwidth or any carrier. Production keeps using Bandwidth because otpmock is only enabled when OTPMOCK_URL is set.
Is the official bandwidth-sdk SDK supported?
Yes. otpmock's Bandwidth endpoints are tested end to end with the official bandwidth-sdk package for Node.js. Other languages work too if their Bandwidth client lets you change the API host.
Which Bandwidth APIs does otpmock support?
POST /api/v2/users/{accountId}/messages (Send a message (applicationId, to[], from, text), 202).
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