Set OTPMOCK_URL=https://api.otpmock.com in your test environment, point the Infobip client at it, and use your otpmock API key as the Infobip credential. Your application code doesn't change, and production keeps sending real SMS through Infobip.
Setup
Infobip gives every account its own base URL, so baseUrl is already a required option.
import { Infobip, AuthType } from "@infobip-api/sdk";
export const infobip = new Infobip({
baseUrl: process.env.OTPMOCK_URL ?? process.env.INFOBIP_BASE_URL,
apiKey: process.env.INFOBIP_API_KEY, // otpmock API key in tests
authType: AuthType.ApiKey,
});import os
from infobip_api_client.api.sms_api import SmsApi
from infobip_api_client.api_client import ApiClient, Configuration
config = Configuration(
host=os.environ.get("OTPMOCK_URL") or os.environ["INFOBIP_BASE_URL"],
api_key=os.environ["INFOBIP_API_KEY"], # otpmock API key in tests
)
sms = SmsApi(ApiClient(config))import com.infobip.*;
import com.infobip.api.SmsApi;
String base = System.getenv().getOrDefault("OTPMOCK_URL", System.getenv("INFOBIP_BASE_URL"));
ApiClient client = ApiClient.forApiKey(ApiKey.from(System.getenv("INFOBIP_API_KEY"))) // otpmock API key in tests
.withBaseUrl(BaseUrl.from(base))
.build();
SmsApi sms = new SmsApi(client);using Infobip.Api.Client.Api;
using Infobip.Api.Client.Client;
var baseUrl = Environment.GetEnvironmentVariable("OTPMOCK_URL")
?? Environment.GetEnvironmentVariable("INFOBIP_BASE_URL");
var config = new Configuration(
Environment.GetEnvironmentVariable("INFOBIP_API_KEY"), // otpmock API key in tests
new Uri(baseUrl));
var sms = new SmsApi(config);use Infobip\Api\SmsApi;
use Infobip\Configuration;
$config = new Configuration(
host: getenv('OTPMOCK_URL') ?: getenv('INFOBIP_BASE_URL'),
apiKey: getenv('INFOBIP_API_KEY'), // otpmock API key in tests
);
$sms = new SmsApi(config: $config);require "net/http"
require "json"
# Infobip has no official Ruby SDK; apps call the REST API, so only the host changes.
INFOBIP_URL = ENV["OTPMOCK_URL"] || ENV["INFOBIP_BASE_URL"]
res = Net::HTTP.post(
URI("#{INFOBIP_URL}/sms/2/text/advanced"),
{ messages: [{ from: "InfoSMS", destinations: [{ to: "15550142" }], text: "Your PIN is #{code}" }] }.to_json,
"Content-Type" => "application/json",
"Authorization" => "App #{ENV["INFOBIP_API_KEY"]}" # otpmock API key in tests
)cfg := infobip.NewConfiguration()
cfg.Host = os.Getenv("INFOBIP_BASE_URL")
if base := os.Getenv("OTPMOCK_URL"); base != "" {
cfg.Host = base
}
client := api.NewAPIClient(cfg)
auth := context.WithValue(ctx, infobip.ContextAPIKeys, map[string]infobip.APIKey{
"APIKeyHeader": {Key: os.Getenv("INFOBIP_API_KEY")}, // otpmock API key in tests
})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 Infobip API key variable to your otpmock API key. App, Basic and Bearer authorization are accepted.
What otpmock emulates
| Endpoint | What it does |
|---|---|
POST /sms/2/text/advanced | Send SMS (v2), used by the Node.js SDK |
POST /sms/3/messages | Send SMS (v3) |
POST /2fa/2/applications | 2FA: create an application (pinAttempts honoured, default 10) |
POST /2fa/2/applications/{appId}/messages | 2FA: create a message template ({{pin}}, pinLength, placeholders) |
POST /2fa/2/pin | 2FA: send a PIN by SMS (otpmock generates it) |
POST /2fa/2/pin/{pinId}/resend | 2FA: resend the same PIN |
POST /2fa/2/pin/{pinId}/verify | 2FA: verify (200 with verified, attemptsRemaining, pinError WRONG_PIN / NO_MORE_PIN_ATTEMPTS) |
Responses and errors follow Infobip's own format, so the SDK parses them as usual. Authentication failures and an exhausted monthly allowance also come back in Infobip's error shape.
Phone numbers
Infobip sends numbers without a leading +. otpmock stores them in E.164 form.
How to tell your app uses Infobip
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:
@infobip-api/sdkin package.jsoninfobip.channels.sms.send(infobip.service.twoFA.- requests to
*.api.infobip.com/sms/or/2fa/2/
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 Infobip
const { code } = await otp.waitForCode(phone, { since });The otp helper is a single file: get it from the docs. Full walkthroughs: Playwright, Cypress.
Good to know
- 2FA applications and templates are not stored: their settings are encoded in the returned
applicationIdandmessageId, so create them once in test setup (or on every run) and reuse the IDs. IDs from your real Infobip account won't work against otpmock. - The SDK exposes 2FA as
infobip.service.twoFA(createApplication,createMessageTemplate,sendPINCodeSMS,verifyPhoneNumber).
FAQ
Does otpmock send real SMS through Infobip?
No. In the test environment your app's Infobip requests go to otpmock, which stores the message for 10 minutes and never contacts Infobip or any carrier. Production keeps using Infobip because otpmock is only enabled when OTPMOCK_URL is set.
Is the official @infobip-api/sdk SDK supported?
Yes. otpmock's Infobip endpoints are tested end to end with the official @infobip-api/sdk package for Node.js. Other languages work too if their Infobip client lets you change the API host.
Which Infobip APIs does otpmock support?
POST /sms/2/text/advanced (Send SMS (v2), used by the Node.js SDK); POST /sms/3/messages (Send SMS (v3)); POST /2fa/2/applications (2FA: create an application (pinAttempts honoured, default 10)); POST /2fa/2/applications/{appId}/messages (2FA: create a message template ({{pin}}, pinLength, placeholders)); POST /2fa/2/pin (2FA: send a PIN by SMS (otpmock generates it)); POST /2fa/2/pin/{pinId}/resend (2FA: resend the same PIN); POST /2fa/2/pin/{pinId}/verify (2FA: verify (200 with verified, attemptsRemaining, pinError WRONG_PIN / NO_MORE_PIN_ATTEMPTS)).
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