Set OTPMOCK_URL=https://api.otpmock.com in your test environment, point the Netgsm client at it, and use your otpmock API key as the Netgsm credential. Your application code doesn't change, and production keeps sending real SMS through Netgsm.
Setup
The SDK has no base URL option, but its baseURL property can be set after construction (it's private only in the TypeScript types).
import { Netgsm } from "@netgsm/sms";
export const netgsm = new Netgsm({
username: process.env.NETGSM_USERNAME, // otpmock API key in tests
password: process.env.NETGSM_PASSWORD,
});
if (process.env.OTPMOCK_URL) (netgsm as any).baseURL = process.env.OTPMOCK_URL;import os
import requests
NETGSM_URL = os.environ.get("OTPMOCK_URL", "https://api.netgsm.com.tr")
requests.post(
f"{NETGSM_URL}/sms/rest/v2/send",
auth=(os.environ["NETGSM_USERNAME"], os.environ["NETGSM_PASSWORD"]), # username = otpmock API key in tests
json={"msgheader": "BASLIGIM", "encoding": "TR", "messages": [{"msg": f"Doğrulama kodunuz: {code}", "no": "5321234567"}]},
).raise_for_status()String base = System.getenv().getOrDefault("OTPMOCK_URL", "https://api.netgsm.com.tr");
String auth = Base64.getEncoder().encodeToString(
(System.getenv("NETGSM_USERNAME") + ":" + System.getenv("NETGSM_PASSWORD")).getBytes()); // username = otpmock API key in tests
HttpRequest req = HttpRequest.newBuilder(URI.create(base + "/sms/rest/v2/send"))
.header("Authorization", "Basic " + auth)
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(json)) // {"msgheader":"...","encoding":"TR","messages":[{"msg":"...","no":"5321234567"}]}
.build();// Netgsm has no official .NET SDK; call REST v2 with HttpClient.
var netgsmUrl = Environment.GetEnvironmentVariable("OTPMOCK_URL") ?? "https://api.netgsm.com.tr";
var user = Environment.GetEnvironmentVariable("NETGSM_USERNAME"); // otpmock API key in tests
var pass = Environment.GetEnvironmentVariable("NETGSM_PASSWORD");
var http = new HttpClient { BaseAddress = new Uri(netgsmUrl) };
http.DefaultRequestHeaders.Authorization = new("Basic",
Convert.ToBase64String(Encoding.UTF8.GetBytes($"{user}:{pass}")));
await http.PostAsJsonAsync("/sms/rest/v2/send", new
{
msgheader = "BASLIGIM", encoding = "TR",
messages = new[] { new { msg = $"Doğrulama kodunuz: {code}", no = "5321234567" } },
});use GuzzleHttp\Client;
$netgsmUrl = getenv('OTPMOCK_URL') ?: 'https://api.netgsm.com.tr';
(new Client())->post("$netgsmUrl/sms/rest/v2/send", [
'auth' => [getenv('NETGSM_USERNAME'), getenv('NETGSM_PASSWORD')], // username = otpmock API key in tests
'json' => ['msgheader' => 'BASLIGIM', 'encoding' => 'TR', 'messages' => [['msg' => "Doğrulama kodunuz: $code", 'no' => '5321234567']]],
]);require "net/http"
require "json"
# Netgsm has no official Ruby SDK; apps call REST v2, so only the host changes.
NETGSM_URL = ENV.fetch("OTPMOCK_URL", "https://api.netgsm.com.tr")
uri = URI("#{NETGSM_URL}/sms/rest/v2/send")
req = Net::HTTP::Post.new(uri, "Content-Type" => "application/json")
req.basic_auth(ENV["NETGSM_USERNAME"], ENV["NETGSM_PASSWORD"]) # username = otpmock API key in tests
req.body = { msgheader: "BASLIGIM", encoding: "TR", messages: [{ msg: "Doğrulama kodunuz: #{code}", no: "5321234567" }] }.to_json
res = Net::HTTP.start(uri.host, uri.port, use_ssl: true) { |h| h.request(req) }// Netgsm 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://api.netgsm.com.tr"
}
body, _ := json.Marshal(map[string]any{
"msgheader": os.Getenv("NETGSM_HEADER"), "encoding": "TR",
"messages": []map[string]string{{"msg": msg, "no": "5321234567"}},
})
req, _ := http.NewRequest("POST", base+"/sms/rest/v2/send", bytes.NewReader(body))
req.Header.Set("Content-Type", "application/json")
req.SetBasicAuth(os.Getenv("NETGSM_USERNAME"), os.Getenv("NETGSM_PASSWORD")) // username = otpmock key in tests
res, err := http.DefaultClient.Do(req) // body code "00" = queuedEach 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 the Netgsm username (or password) to your otpmock API key. Basic auth is read from either field.
What otpmock emulates
| Endpoint | What it does |
|---|---|
POST /sms/rest/v2/send | Send SMS (code "00" on success) |
POST /sms/rest/v2/otp | Send an OTP SMS |
Responses and errors follow Netgsm's own format, so the SDK parses them as usual. Authentication failures and an exhausted monthly allowance also come back in Netgsm's error shape.
Try it with cURL
Before wiring your app, check the emulation by hand. Put your own key in OTPMOCK_API_KEY; the number is made up. Keep the message text ASCII when you run this from a Windows terminal, which can garble accented letters on the command line.
curl https://api.otpmock.com/sms/rest/v2/send \
-u "$OTPMOCK_API_KEY:x" \
-H "content-type: application/json" \
-d '{"msgheader":"BASLIGIM","messages":[{"msg":"Dogrulama kodunuz: 482913","no":"5321234567"}],"encoding":"TR"}'{"code":"00","jobid":"64271089041352956440282421","description":"queued"}The response has Netgsm's own shape. Now read the code your test would read:
curl https://api.otpmock.com/v1/inbox/%2B905321234567/code \
-H "Authorization: Bearer $OTPMOCK_API_KEY"
{"code":"482913","messageSid":"SM…","receivedAt":1795257752284}Phone numbers
Netgsm uses Turkish local numbers (5321234567). otpmock stores them as +905321234567; query that form in tests.
How to tell your app uses Netgsm
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:
@netgsm/smsin package.jsonsendRestSms(sendOtpSms(- requests to
api.netgsm.com.tr/sms/rest/v2/
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 Netgsm
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
- The legacy XML and GET endpoints aren't emulated; the SDK uses REST v2.
FAQ
Does otpmock send real SMS through Netgsm?
No. In the test environment your app's Netgsm requests go to otpmock, which stores the message for 10 minutes and never contacts Netgsm or any carrier. Production keeps using Netgsm because otpmock is only enabled when OTPMOCK_URL is set.
Is the official @netgsm/sms SDK supported?
Yes. otpmock's Netgsm endpoints are tested end to end with the official @netgsm/sms package for Node.js. Other languages work too if their Netgsm client lets you change the API host.
Which Netgsm APIs does otpmock support?
POST /sms/rest/v2/send (Send SMS (code "00" on success)); POST /sms/rest/v2/otp (Send an OTP SMS).
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