How to Add OTP Verification to Your App: Best Practices for Secure
By Nimana · Last updated November 17, 2026 · 4 min read
Learn how to add OTP verification to your sign-in flow: choose the right channel, set a sensible expiry, limit resends, and show clear errors users understand.
Why This Matters
Passwords get reused, guessed, and leaked. A one-time password (OTP) sent to the user's phone or email adds a second check that an attacker cannot easily fake. It also confirms that the phone number a person gives you really belongs to them, which keeps fake sign-ups out.
An OTP flow is simple, but small choices decide whether it feels smooth or frustrating, and whether it is secure or easy to abuse.
How an OTP Flow Works
- The user enters their phone number or email.
- Your server asks the messaging service to send a code.
- The service delivers the code to the user.
- The user types the code into your app.
- Your server asks the service to check the code.
- If it is correct and has not expired, the user is verified.
Notice that your server drives every step. The user's device should never decide whether a code is valid.
Seven Best Practices
1. Choose the right channel
Each channel has strengths:
- SMS reaches almost any phone, so it is the safest default.
- WhatsApp can give a richer experience, if your users are on it and you have an active session.
- Email suits desktop-first apps where users are not on their phones.
2. Keep the code short and the expiry short
A six-digit code is the standard length. A lifetime of about five minutes is common. A short expiry limits the time an attacker has to guess or reuse a code.
3. Limit how often codes can be requested
Without limits, a bad actor can trigger hundreds of messages to one person, or run up your costs. Limit sends per phone number per hour and per day, and add a short cooldown on the "Resend code" button.
4. Limit wrong guesses
A six-digit code has a million possibilities, which is small for a computer. Lock the attempt after a few wrong guesses and require a new code.
5. Never log or display the code
Treat codes like passwords. Do not write them to logs, show them in error messages, or send them to analytics tools.
6. Show clear, calm error messages
Users get stuck in predictable ways: the code did not arrive, it expired, or they typed it wrong. Say which, and tell them what to do next:
- "We sent a code to your phone. It can take a minute to arrive."
- "That code has expired. Tap Resend to get a new one."
- "That code is not right. Please check it and try again."
7. Offer a way out
Some users change numbers or cannot receive texts. Give them another channel, such as email, or a way to contact support.
A Few Common Mistakes
- Verifying on the device instead of the server. Anyone could bypass the check.
- Using the same code twice. A code should work once.
- No cooldown on resends, which invites abuse and confuses users.
- Vague errors, which lead to repeated resend taps and support tickets.
How SendAPI Helps
SendAPI's Verify API handles the hard parts, so you do not need to generate codes, store them, or track expiry yourself. It works in two calls:
1. Send the code
curl -X POST https://sendapi.co/v1/verify/send \
-H "Authorization: Bearer sk_live_YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"to": "+447700900000",
"channel": "sms",
"code_length": 6,
"ttl_seconds": 300
}'
2. Check the code
curl -X POST https://sendapi.co/v1/verify/check \
-H "Authorization: Bearer sk_live_YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"to": "+447700900000",
"code": "847291"
}'
If valid: true comes back, the user is authenticated. Store the recipient number so you can pass it back when you check the code.
A few details that match the practices above:
- Channels:
smsworks everywhere,whatsappsuits richer delivery when you have an active session, andemailsuits desktop-first apps. - Expiry:
ttl_secondssets how long a code lasts, and 300 seconds (5 minutes) is standard for most sign-in flows. - Built-in abuse limits: each phone number can receive 5 OTPs per hour and 20 per day. Going over returns a
429with the code4291(hourly) or4292(daily). - No code handling on your side: the Verify API handles code generation and storage, so you never need to log a code.
- Smart Channel Routing and Phone Validation are available in the Verify API reference.
OTP sends are included in the paid plans: 200 a month on Starter, 1,000 on Growth, and 5,000 on Business. All of this comes with WhatsApp, SMS, OTP, and email under one API from $9.99 a month.