← All use cases

One-time password (OTP) SMS

A six-digit code by SMS is still the login step most people understand. For a small app, an internal tool or a side project, NS SMS sends the code from your own phone with one API call and no per-message fee.

The problem

Classic OTP providers are built for millions of users: monthly minimums, sender registration and per-message prices that add up even for a small user base.

For a few dozen or a few hundred logins a day you mainly need the code to arrive quickly and reliably.

Why sending from your own number works

  • One HTTPS request per code, answered with an ID you can check if a user says the code never came.
  • The SMS comes from a normal mobile number, which carriers deliver like any person-to-person text.
  • Good for internal tools, admin panels, small SaaS, two-factor login for your team and phone number checks at sign-up.

Honest limits

  • For OTP at large scale (thousands of sign-ups a day, many countries, strict delivery times) a classic SMS or verification provider is the better choice. Daily caps of 50, 500 or 1,000 texts protect your SIM, and one phone sends one message after another.
  • Delivery takes a few seconds and depends on the phone's connection. Let users ask for a new code after 30 to 60 seconds.
  • Protect the endpoint that sends codes: limit requests per phone number and per IP, or anyone can spend your daily cap.

SMS templates

Short, plain characters only (GSM-7), so each one fits a single SMS of up to 160 characters. Accents, emoji and curly quotes switch the message to Unicode, where one SMS holds only 70 characters.

Your Acme code is 482913. It expires in 5 minutes. Do not share it with anyone.

79 characters, 1 SMS

482913 is your Acme verification code. @acme.example #482913

61 characters, 1 SMS

The second template uses the WebOTP format: the last line with your domain and the code lets supporting browsers offer the code for autofill.

Code example

No account, email or password. Install the app, turn on the API and copy your phone ID and API key.

Send a code (cURL)

The simplest version, handy for testing from a terminal.

send-code.sh
CODE_VALUE=$(shuf -i 100000-999999 -n 1)

curl -X POST https://sms-api.n-soft.pl/v1/sms/send \
  -H "Authorization: Bearer $NS_SMS_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"device_id":"'"$NS_SMS_DEVICE_ID"'","to":"+14155550123","text":"Your Acme code is '"$CODE_VALUE"'. It expires in 5 minutes."}'

Generate, send and verify (Node.js)

The code is random, stored only as a hash, expires after 5 minutes and allows 5 tries. Replace the Map with Redis or your database.

otp.mjs
import crypto from "node:crypto";

const codes = new Map(); // phone -> { hash, expires, tries }; use Redis or your DB in production

const hash = (code) => crypto.createHash("sha256").update(code).digest("hex");

export async function sendCode(phone) {
  const code = String(crypto.randomInt(100000, 1000000));
  codes.set(phone, { hash: hash(code), expires: Date.now() + 5 * 60_000, tries: 0 });

  const response = await fetch("https://sms-api.n-soft.pl/v1/sms/send", {
    method: "POST",
    headers: {
      Authorization: `Bearer ${process.env.NS_SMS_API_KEY}`,
      "Content-Type": "application/json",
    },
    body: JSON.stringify({
      device_id: process.env.NS_SMS_DEVICE_ID,
      to: phone,
      text: `Your Acme code is ${code}. It expires in 5 minutes. Do not share it.`,
    }),
  });
  if (response.status !== 202) {
    throw new Error(`SMS not accepted: ${response.status} ${await response.text()}`);
  }
}

export function verifyCode(phone, code) {
  const entry = codes.get(phone);
  if (!entry || entry.expires < Date.now() || ++entry.tries > 5) {
    codes.delete(phone);
    return false;
  }
  const ok = crypto.timingSafeEqual(Buffer.from(entry.hash), Buffer.from(hash(String(code))));
  if (ok) codes.delete(phone);
  return ok;
}

Without code

The same thing works from these tools:

Other use cases