Send an OTP
/v1/otp/send Scopeotp:send
Generates a one-time password and queues its delivery by SMS (default) or WhatsApp. The code is never returned and never stored in plain text.
- A new OTP to the same number and purpose supersedes the previous one, and is subject to a resend cooldown (
resendAfter) and per-number, per-IP and per-project limits. channel: "whatsapp"uses the project WhatsApp authentication template; addfallbackChannel: "sms"to send by SMS when WhatsApp definitely fails.- Live OTPs to India need a DLT-approved SMS template with an
{{otp}}variable (templateId, or the project default OTP template). - Test keys: nothing is sent and the code is always
123456.
Parameters1
Idempotency-KeyheaderstringMakes the request safe to retry: repeating it with the same key and body returns the original result (with
Idempotent-Replayed: true) instead of sending again. Use one key per logical message, e.g. derived from your order or login-attempt ID. Reusing a key with a different body is409 IDEMPOTENCY_KEY_REUSED.
Body8
mobilestringrequiredRecipient in international format with country code: "919876543210" or "+919876543210".
purposestringrequiredWhy the OTP is requested; used in reports and abuse limits.
loginregistrationpassword_resettransactionmobile_verificationchannelstringDelivery channel of the first attempt.
smswhatsappfallbackChannelstringchannel: "whatsapp"only. If WhatsApp definitely fails (for example the number is not on WhatsApp), the code is sent by SMS. Nothing is resent while the WhatsApp outcome is uncertain.smstemplateIdstringSMS template with an
{{otp}}variable. Defaults to the project OTP template. Live OTPs to DLT countries (India) need one of the two.variablesobjectOther template variables.
otpandminutesare filled in by SMS21 and must not be sent.expiresInintegerValidity in seconds. Default 300; values above the platform maximum (900 unless configured otherwise) are refused.
ipany (ipv4) | any (ipv6)The END USER's IP address, when known, for per-IP abuse limits. Never send your server's address.
Responses
- 202
Accepted: the code was generated and its delivery queued.
- 400
Invalid request.
Example
Response 400{ "success": false, "error": { "code": "VALIDATION_FAILED", "message": "Request validation failed", "details": [ { "field": "purpose", "message": "purpose must be one of the following values: login, registration, password_reset, transaction, mobile_verification" } ] }, "requestId": "0199491c-8f3a-7c2e-9b1d-5e7f3a2c4b6d" } - 401
The API key is missing, malformed, unknown, revoked or expired.
Example
Response 401{ "success": false, "error": { "code": "AUTHENTICATION_REQUIRED", "message": "An API key is required; send it in the X-API-Key header or as \"Authorization: Bearer <key>\"" }, "requestId": "0199491c-8f3a-7c2e-9b1d-5e7f3a2c4b6d" } - 402
Live keys only: the wallet cannot cover the message. Add funds; retrying will not help.
Example
Response 402{ "success": false, "error": { "code": "INSUFFICIENT_BALANCE", "message": "Insufficient wallet balance" }, "requestId": "0199491c-8f3a-7c2e-9b1d-5e7f3a2c4b6d" } - 403
The key may not do this: missing scope, IP not on the allow-list, company, project or key suspended by SMS21, project archived, wallet suspended or destination blocked.
Example
Response 403{ "success": false, "error": { "code": "INSUFFICIENT_SCOPE", "message": "This API key does not have the required scope", "details": [ { "message": "requires scope sms:send" } ] }, "requestId": "0199491c-8f3a-7c2e-9b1d-5e7f3a2c4b6d" } - 404
The template does not exist in this company.
Example
Response 404{ "success": false, "error": { "code": "NOT_FOUND", "message": "Template not found", "details": [ { "field": "templateId", "message": "no such template" } ] }, "requestId": "0199491c-8f3a-7c2e-9b1d-5e7f3a2c4b6d" } - 409
Conflicting state.
Example
Response 409{ "success": false, "error": { "code": "IDEMPOTENCY_KEY_REUSED", "message": "This Idempotency-Key was already used with a different request" }, "requestId": "0199491c-8f3a-7c2e-9b1d-5e7f3a2c4b6d" } - 429
Too many OTPs or requests. Honour
Retry-After; do not loop.Example
Response 429{ "success": false, "error": { "code": "OTP_RESEND_COOLDOWN", "message": "Please wait 24 seconds before requesting another OTP" }, "requestId": "0199491c-8f3a-7c2e-9b1d-5e7f3a2c4b6d" } - 503
Temporarily unavailable, or live sending paused by SMS21. Retry later with the same Idempotency-Key.
Example
Response 503{ "success": false, "error": { "code": "SERVICE_UNAVAILABLE", "message": "Service temporarily unavailable" }, "requestId": "0199491c-8f3a-7c2e-9b1d-5e7f3a2c4b6d" }
curl -X POST "https://sms21.in/v1/otp/send" \
-H "X-API-Key: $SMS21_API_KEY" \
-H "Content-Type: application/json" \
-d '{"mobile":"919876543210","purpose":"login"}'{
"success": true,
"requestId": "otp_01k55q8y4m3d9v7x2c6b0n1r8t",
"status": "pending",
"channel": "sms",
"purpose": "login",
"expiresIn": 300,
"expiresAt": "2026-09-15T10:35:00.000Z",
"resendAfter": 30,
"messageId": "msg_01k55q8y4m3d9v7x2c6b0n1r8t"
}