Set OTPMOCK_URL=https://api.otpmock.com in your test environment, point the Sinch client at it, and use your otpmock API key as the Sinch credential. Your application code doesn't change, and production keeps sending real SMS through Sinch.
Setup
The SDK has documented smsHostname and verificationHostname options.
import { SinchClient } from "@sinch/sdk-core";
export const sinch = new SinchClient({
servicePlanId: process.env.SINCH_SERVICE_PLAN_ID,
apiToken: process.env.SINCH_API_TOKEN, // otpmock API key in tests
applicationKey: process.env.SINCH_APPLICATION_KEY, // otpmock API key in tests (Verification)
applicationSecret: process.env.SINCH_APPLICATION_SECRET,
...(process.env.OTPMOCK_URL && { smsHostname: process.env.OTPMOCK_URL, verificationHostname: process.env.OTPMOCK_URL }),
});import os
from sinch import SinchClient
sinch = SinchClient(
service_plan_id=os.environ["SINCH_SERVICE_PLAN_ID"],
sms_api_token=os.environ["SINCH_API_TOKEN"], # otpmock API key in tests
sms_region="us",
)
if os.environ.get("OTPMOCK_URL"):
sinch.configuration.sms_domain_with_service_plan_id = os.environ["OTPMOCK_URL"]
# Verification API (HTTP): Application auth, key = otpmock API key in tests
VERIFY_URL = os.environ.get("OTPMOCK_URL", "https://verification.api.sinch.com")
VERIFY_AUTH = {"Authorization": f"Application {os.environ['SINCH_APP_KEY']}:{os.environ['SINCH_APP_SECRET']}"}
# POST {VERIFY_URL}/verification/v1/verifications, PUT ./id/{id} {"method": "sms", "sms": {"code": ...}}import com.sinch.sdk.SinchClient;
import com.sinch.sdk.models.*;
String otpmock = System.getenv("OTPMOCK_URL");
Configuration.Builder config = Configuration.builder()
.setSmsServicePlanId(System.getenv("SINCH_SERVICE_PLAN_ID"))
.setSmsApiToken(System.getenv("SINCH_API_TOKEN")) // otpmock API key in tests
.setApplicationKey(System.getenv("SINCH_APPLICATION_KEY")) // Verification: otpmock API key in tests
.setApplicationSecret(System.getenv("SINCH_APPLICATION_SECRET"));
if (otpmock != null) {
config.setSmsContext(SmsContext.builder().setSmsUrl(otpmock).setSmsRegion(SMSRegion.US).build());
config.setVerificationContext(VerificationContext.builder().setVerificationUrl(otpmock).build());
}
SinchClient sinch = new SinchClient(config.build());using Sinch;
using Sinch.SMS;
var otpmock = Environment.GetEnvironmentVariable("OTPMOCK_URL");
var sinch = new SinchClient(default, default, default, options =>
{
options.UseServicePlanIdWithSms(
Environment.GetEnvironmentVariable("SINCH_SERVICE_PLAN_ID"),
Environment.GetEnvironmentVariable("SINCH_API_TOKEN"), // otpmock API key in tests
SmsServicePlanIdRegion.Us);
if (otpmock is not null) options.ApiUrlOverrides = new ApiUrlOverrides { SmsUrl = otpmock };
});
// Verification over HTTPS (application key = otpmock API key in tests; signature isn't checked):
var verifyHttp = new HttpClient { BaseAddress = new Uri(otpmock ?? "https://verification.api.sinch.com") };
verifyHttp.DefaultRequestHeaders.Authorization = new("Basic", Convert.ToBase64String(Encoding.UTF8.GetBytes(
$"{Environment.GetEnvironmentVariable("SINCH_APPLICATION_KEY")}:{Environment.GetEnvironmentVariable("SINCH_APPLICATION_SECRET")}")));
// POST /verification/v1/verifications { identity = { type = "number", endpoint }, method = "sms" } -> id
// PUT /verification/v1/verifications/id/{id} { method = "sms", sms = { code } } -> status "SUCCESSFUL"use GuzzleHttp\Client;
$sinchUrl = getenv('OTPMOCK_URL') ?: 'https://us.sms.api.sinch.com';
$planId = getenv('SINCH_SERVICE_PLAN_ID');
(new Client())->post("$sinchUrl/xms/v1/$planId/batches", [
'headers' => ['Authorization' => 'Bearer ' . getenv('SINCH_API_TOKEN')], // otpmock API key in tests
'json' => ['from' => '12345', 'to' => ['+15550142'], 'body' => "Your code is $code"],
]);
// Verification API: application key/secret auth.
$verifyUrl = getenv('OTPMOCK_URL') ?: 'https://verification.api.sinch.com';
$appAuth = ['Authorization' => 'Application ' . getenv('SINCH_APPLICATION_KEY') . ':' . $signature]; // key = otpmock API key in tests
(new Client())->post("$verifyUrl/verification/v1/verifications", [
'headers' => $appAuth,
'json' => ['identity' => ['type' => 'number', 'endpoint' => '+15550142'], 'method' => 'sms'],
]);require "net/http"
require "json"
# Sinch has no official Ruby SDK; apps call the REST API, so only the host changes.
SINCH_URL = ENV.fetch("OTPMOCK_URL", "https://us.sms.api.sinch.com")
res = Net::HTTP.post(
URI("#{SINCH_URL}/xms/v1/#{ENV["SINCH_SERVICE_PLAN_ID"]}/batches"),
{ to: ["+15550142"], from: "12345", body: "Your code: #{code}" }.to_json,
"Content-Type" => "application/json",
"Authorization" => "Bearer #{ENV["SINCH_API_TOKEN"]}" # otpmock API key in tests
)// Sinch has no official Go SDK; call its HTTP API with net/http, only the host changes.
base := os.Getenv("OTPMOCK_URL")
if base == "" {
base = "https://us.sms.api.sinch.com"
}
body, _ := json.Marshal(map[string]any{"to": []string{"+15550142"}, "from": os.Getenv("SINCH_NUMBER"), "body": msg})
req, _ := http.NewRequest("POST", base+"/xms/v1/"+os.Getenv("SINCH_SERVICE_PLAN_ID")+"/batches", bytes.NewReader(body))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+os.Getenv("SINCH_API_TOKEN")) // otpmock API key in tests
res, err := http.DefaultClient.Do(req)
// Verification: POST {base}/verification/v1/verifications, then PUT .../verifications/id/{id}.
// Production host is https://verification.api.sinch.com. Basic auth with the application key and secret:
req.SetBasicAuth(os.Getenv("SINCH_APPLICATION_KEY"), os.Getenv("SINCH_APPLICATION_SECRET")) // key = otpmock key in testsEach 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 Sinch API token (used with servicePlanId) to your otpmock API key. For the Verification API, set the application key to your otpmock API key; the SDK signs requests with your application secret, and otpmock doesn't check the signature.
What otpmock emulates
| Endpoint | What it does |
|---|---|
POST /xms/v1/{service_plan_id}/batches | Send an SMS batch (mt_text) |
POST /verification/v1/verifications | Verification: start an SMS verification (otpmock generates a 4-digit code) |
PUT /verification/v1/verifications/id/{id} | Verification: report the code by ID (200 with status SUCCESSFUL or FAIL) |
PUT /verification/v1/verifications/number/{number} | Verification: report the code by phone number |
GET /verification/v1/verifications/id/{id} | Verification: status |
Responses and errors follow Sinch's own format, so the SDK parses them as usual. Authentication failures and an exhausted monthly allowance also come back in Sinch's error shape.
Phone numbers
Send in E.164 or without +; both land in the same inbox.
How to tell your app uses Sinch
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:
@sinch/sdk-corein package.jsonsinch.sms.batches.send(sinch.verification.verifications.startSms(- requests to
*.sms.api.sinch.com/xms/v1/orverification.api.sinch.com
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 Sinch
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
- Only the service plan ID + API token authentication mode is emulated, not the OAuth (project ID / key) mode.
FAQ
Does otpmock send real SMS through Sinch?
No. In the test environment your app's Sinch requests go to otpmock, which stores the message for 10 minutes and never contacts Sinch or any carrier. Production keeps using Sinch because otpmock is only enabled when OTPMOCK_URL is set.
Is the official @sinch/sdk-core SDK supported?
Yes. otpmock's Sinch endpoints are tested end to end with the official @sinch/sdk-core package for Node.js. Other languages work too if their Sinch client lets you change the API host.
Which Sinch APIs does otpmock support?
POST /xms/v1/{service_plan_id}/batches (Send an SMS batch (mt_text)); POST /verification/v1/verifications (Verification: start an SMS verification (otpmock generates a 4-digit code)); PUT /verification/v1/verifications/id/{id} (Verification: report the code by ID (200 with status SUCCESSFUL or FAIL)); PUT /verification/v1/verifications/number/{number} (Verification: report the code by phone number); GET /verification/v1/verifications/id/{id} (Verification: status).
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