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.
- Your backend calls
POST /otp/sendand keeps therequestId. - The user types the code into your app.
- Your backend calls
POST /otp/verify; on success, sign the user in as the verified number.
Send
| Field | Type | Description | |
|---|---|---|---|
mobile | string | required | International format with country code: 919876543210 or +919876543210. |
purpose | string | required | One of login, registration, password_reset, transaction, mobile_verification. |
channel | string | optional | sms (default) or whatsapp. |
fallbackChannel | string | optional | With channel: "whatsapp": sms sends the code by SMS if WhatsApp definitely fails. |
templateId | string | optional | SMS template (tmpl_…) containing {{otp}}. Defaults to the project OTP template. |
variables | object | optional | Other template variables. otp and minutes are filled in by SMS21. |
expiresIn | integer | optional | Validity in seconds, 60–900 (default 300). |
ip | string | optional | The 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
| Field | Type | Description | |
|---|---|---|---|
requestId | string | required | requestId from the send response. |
otp | string | required | The 4–10 digit code the user entered. |
mobile | string | optional | The 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);| Outcome | HTTP · code | Tell the user |
|---|---|---|
| Correct | 200 | Signed in. |
| Wrong code | 400 OTP_INVALID — details: “4 attempts remaining” | The code is incorrect. |
| Expired or superseded | 410 OTP_EXPIRED | The code expired. Request a new one. |
| Used already | 409 OTP_ALREADY_VERIFIED | Request a new code. |
| Too many wrong codes | 429 OTP_ATTEMPTS_EXCEEDED | Request a new code. |
| Unknown request | 404 NOT_FOUND | Request a new code. |
Limits
| Limit | Default |
|---|---|
| Code validity | 300 seconds (max 900) |
| Wrong codes per request | 5, then the request is locked |
| Resend cooldown, same number and purpose | 30 seconds (429 OTP_RESEND_COOLDOWN, Retry-After) |
| OTPs per number per project | 5 per hour |
| OTPs per number across all SMS21 customers | 10 per hour (SMS pumping protection) |
| OTPs per end-user IP per project | 20 per hour (when ip is sent) |
| OTPs per project | 10,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.mobilereturned by SMS21, not a number from the browser. - Send
mobileon verify and keeprequestIdin 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 aloginverification to authorise a transaction. - Respond identically whether or not the number has an account, to avoid account enumeration.
- Never log codes. Log
requestIdandX-Request-Idinstead.
Test keys
Nothing is sent and the code is always 123456. Numbers ending in 0001–0004 simulate delivery failures.