Contents

OTP

Send and verify one-time passwords by SMS or WhatsApp.

SMS21 generates the code, delivers it, and checks it. The code is never returned by the API and never stored in plain text, so your application never handles or logs it — it only passes on what the user typed.

  1. Your backend calls POST /otp/send and keeps the requestId.
  2. The user types the code into your app.
  3. Your backend calls POST /otp/verify; on success, sign the user in as the verified number.

Send

FieldTypeDescription
mobilestringrequiredInternational format with country code: 919876543210 or +919876543210.
purposestringrequiredOne of login, registration, password_reset, transaction, mobile_verification.
channelstringoptionalsms (default) or whatsapp.
fallbackChannelstringoptionalWith channel: "whatsapp": sms sends the code by SMS if WhatsApp definitely fails.
templateIdstringoptionalSMS template (tmpl_…) containing {{otp}}. Defaults to the project OTP template.
variablesobjectoptionalOther template variables. otp and minutes are filled in by SMS21.
expiresInintegeroptionalValidity in seconds, 60–900 (default 300).
ipstringoptionalThe end user's IP address, for per-IP abuse limits. Never your server's address.
cURL
curl -X POST https://sms21.in/v1/otp/send \
  -H "X-API-Key: $SMS21_API_KEY" -H "Content-Type: application/json" \
  -H "Idempotency-Key: login-attempt-7f3a9c" \
  -d '{"mobile":"919876543210","purpose":"login","channel":"whatsapp","fallbackChannel":"sms","ip":"203.0.113.24"}'

The 202 response contains requestId, expiresIn (seconds), resendAfter (seconds until another code may be sent) and messageId for delivery tracking. Show a resend countdown from resendAfter.

Channels and fallback

  • SMS uses the project OTP template (or templateId). Live OTPs to India need a DLT-approved template.
  • WhatsApp uses the project’s approved WhatsApp authentication template. With fallbackChannel: "sms", SMS21 sends the same code by SMS when WhatsApp definitely fails — for example when the number is not on WhatsApp. When the WhatsApp outcome is uncertain, nothing is resent.
  • SMS21 fails over between providers on definite failures, without generating a new code.

Verify

FieldTypeDescription
requestIdstringrequiredrequestId from the send response.
otpstringrequiredThe 4–10 digit code the user entered.
mobilestringoptionalThe number you expect. A mismatch counts as a wrong code.
import { Sms21 } from '@sms21/node';

const sms21 = new Sms21();   // SMS21_API_KEY

const result = await sms21.otp.check({ requestId, otp: codeFromUser, mobile });
if (!result.verified) {
  return showError(result.reason, result.attemptsRemaining);
}
await createSession(result.verification.mobile);
OutcomeHTTP · codeTell the user
Correct200Signed in.
Wrong code400 OTP_INVALID — details: “4 attempts remaining”The code is incorrect.
Expired or superseded410 OTP_EXPIREDThe code expired. Request a new one.
Used already409 OTP_ALREADY_VERIFIEDRequest a new code.
Too many wrong codes429 OTP_ATTEMPTS_EXCEEDEDRequest a new code.
Unknown request404 NOT_FOUNDRequest a new code.

Limits

LimitDefault
Code validity300 seconds (max 900)
Wrong codes per request5, then the request is locked
Resend cooldown, same number and purpose30 seconds (429 OTP_RESEND_COOLDOWN, Retry-After)
OTPs per number per project5 per hour
OTPs per number across all SMS21 customers10 per hour (SMS pumping protection)
OTPs per end-user IP per project20 per hour (when ip is sent)
OTPs per project10,000 per day

Defaults can differ per SMS21 deployment; your integration should rely on the error codes, not the numbers.

Security checklist

  • Sign users in as verification.mobile returned by SMS21, not a number from the browser.
  • Send mobile on verify and keep requestId in your server-side session where possible.
  • Never retry verify automatically — a lost response must not use up attempts or be replayed.
  • Use the right purpose; do not reuse a login verification to authorise a transaction.
  • Respond identically whether or not the number has an account, to avoid account enumeration.
  • Never log codes. Log requestId and X-Request-Id instead.

Test keys

Nothing is sent and the code is always 123456. Numbers ending in 0001–0004 simulate delivery failures.