OTP & verification
OTP Best Practices: Length, Expiry, Retries, and Rate Limits
A practical checklist for one-time passwords: code length, generation, storage, expiry, attempt limits, resend rules, message text, autofill, and fraud protection.
One-time passwords look simple, but small decisions add up: a code that expires too fast frustrates users, a resend button with no limit invites fraud, and a badly worded message gets ignored. Here is a checklist that works for most consumer apps.
Generating the code
- Use 6 digits. Four digits is too easy to guess with repeated attempts; eight is annoying to type.
- Use a cryptographically secure random generator, such as
crypto.randomIntin Node.js orsecretsin Python. NeverMath.random. - Generate a new code on every resend. Do not resend the old one.
Storing and checking the code
- Store a hash, not the code, ideally an HMAC with a server-side secret, and compare in constant time.
- Bind the code to the phone number and the purpose (login, signup, payment), so a signup code cannot approve a payment.
- Delete the code after successful use. One-time means one time.
- Limit wrong attempts to around 5 per code, then invalidate it and require a new one.
Expiry
Five minutes is a sensible default for SMS OTPs: long enough for a slow delivery, short enough to limit misuse. Some teams allow up to 10 minutes. See how long an OTP should be valid for the trade-offs.
Sending and resending
- Cooldown before resend, typically 30 to 60 seconds, shown as a countdown.
- Cap sends per number, for example 3 to 5 within 10 to 15 minutes.
- Also limit by IP address and device, because fraudsters rotate numbers.
- Offer a different channel on retry, such as WhatsApp or voice, rather than another SMS. See designing the OTP resend flow.
The message itself
A good OTP message is short, branded, and clear:
482615 is your Acme verification code. It expires in 5 minutes. Never share it with anyone.
- Put the code early, so it shows in the notification preview.
- Name your brand, even though the sender ID shows it too.
- Add a "never share" warning; it reduces social engineering.
- Avoid links in OTP messages.
- Keep it in one segment; check encoding for non-Latin languages. See SMS character limits.
Make entry effortless
- Use
autocomplete="one-time-code"andinputmode="numeric"on the input. - Support paste, and auto-submit when all digits are entered.
- For web apps, add the origin-bound line
@yourdomain.com #482615at the end of the SMS so browsers can autofill safely. See OTP autofill on iOS and Android.
Protect against abuse
OTP endpoints are a prime target for SMS pumping, where attackers trigger messages to premium-rate or attacker-controlled numbers. Besides rate limits:
- Block or challenge destinations you do not serve.
- Watch for spikes in sends to unusual country codes or number ranges.
- Track the ratio of verified to sent codes; a sudden drop is a red flag.
Read the full guide on SMS pumping prevention.
Measure it
Track OTP conversion (verified ÷ sent), median delivery time, resend rate, and failures by reason, broken down by country and network. These four numbers tell you almost everything about the health of your verification flow.