Verify a phone number with a one-time code
Use one-time codes on your own website or app: when someone signs up, signs in or checks out, send a code to their phone and check what they type. The code is made, sent, expired and checked for you, and it is never returned to your server.
Before you start #
- An API key with read-and-write scope. See Getting started.
- For WhatsApp: a connected WhatsApp number and an approved template that carries the code. An authentication template is the right kind: create it in WhatsApp Manager with a copy-code button, then use Sync on the Templates page so it appears here. Any other approved template works too, as long as its first body variable is the code.
- For SMS: an SMS account connected under More channels.
Call these endpoints from your server, never from the browser: the API key must stay secret.
-
Send the code #
When the person enters their number, ask for a code to be sent. Six digits, good for ten minutes, unless you say otherwise with
lengthandexpires_in.curl -X POST "https://chatilo.me/api/v1/otp" \ -H "Authorization: Bearer YOUR_API_KEY" \ -H "Content-Type: application/json" \ -d '{ "to": "+919876543210", "channel": "whatsapp", "template": "login_code" }'By SMS, leave the template out:
curl -X POST "https://chatilo.me/api/v1/otp" \ -H "Authorization: Bearer YOUR_API_KEY" \ -H "Content-Type: application/json" \ -d '{ "to": "+919876543210", "channel": "sms" }'The answer tells you when the code expires. It does not contain the code.
-
Check what they typed #
curl -X POST "https://chatilo.me/api/v1/otp/verify" \ -H "Authorization: Bearer YOUR_API_KEY" \ -H "Content-Type: application/json" \ -d '{ "to": "+919876543210", "code": "482913" }'Read
verified. It istrueonce, for the right code; after that the code is spent. When it isfalse,reasonsays why:wrong_code: let them try again.expiredornot_found: send a new code.too_many_attempts: five wrong tries lock the code. Send a new one.
-
Offer “Send again” with a pause #
One code can go to a number every 30 seconds, and five in an hour. Sooner than that answers
429 otp_throttled. Show a countdown on your “Send again” button so people do not hit it.
Good practice #
- Treat a number as verified only after
verified: truefrom your own server’s call. - Do not tell the visitor whether a number already has an account before the code is verified.
- A new code replaces the earlier one for the same number, so only the latest message works.
- Codes sent this way are not saved as contacts or conversations.