Free Numbers Pricing Reviews Feedback Terms & Policy API Docs Blog About
Log in Get Started
6 views 5 min read
Back to Blog
Security

OTP Best Practices for Developers: Length, Expiry, Attempts and Resend Rules

OTP Best Practices for Developers: Length, Expiry, Attempts and Resend Rules

Verification flows fail in boring, predictable ways: a code that never expires, a resend button that fires ten messages, an attempt counter that resets on refresh. None of these are exotic attacks, and all of them are avoidable. The OTP best practices below are the defaults worth adopting before your first user signs up, written for developers who need rules they can implement this week rather than a security essay.

Direct answer: Use six digit codes with a five to ten minute expiry, allow three to five verification attempts per code, rate limit resends with a cooldown of at least thirty seconds, store only a hash of the code, and invalidate a code the moment it is used successfully. These defaults balance usability against brute force and abuse.

How Long Should an OTP Code Be?

Six digits is the practical default. It gives one million combinations, fits comfortably in a message and is easy to type on a small keyboard. Four digits is acceptable only when you pair it with a short expiry and a hard attempt cap, which together reduce the guessing window to something negligible. Eight digits raises security marginally and increases typing errors noticeably.

Whatever length you choose, enforce it on the server. Client side maxlength is a convenience, not a control.

Expiry: Short Enough to Matter

Five to ten minutes is the usual range. Below five minutes you will see legitimate failures on slow networks. Above fifteen minutes an intercepted code stays useful far longer than it should. Set expiry at issuance time and store the timestamp; do not derive it from a session.

Expire codes on the following events as well:

  • A successful verification.
  • A new code being issued for the same user and purpose.
  • A password change or account recovery completion.
  • An explicit logout from a security settings screen.

Attempt Limits and Lockouts

A six digit code with unlimited attempts is not a secret. Cap verification attempts per code at three to five, and cap total attempts per account per hour separately. When the code cap is hit, invalidate the code entirely rather than leaving it valid but unguessable; a stale valid code is a liability.

Distinguish between two failure types in your logging: wrong code and expired code. Users who repeatedly hit expiry need a different message than users typing the wrong digits.

Resend Rules That Do Not Annoy Users

  1. Show the resend button disabled with a visible countdown of thirty to sixty seconds.
  2. Enforce the same cooldown on the server, keyed by user and by IP address.
  3. Allow a small maximum number of resends per hour, then require a different verification path.
  4. Invalidate the previous code when a new one is issued, so only one code is ever valid.
  5. Log every resend with the request identifier, never with the code itself.

Resend abuse is one of the cheapest ways to burn a messaging budget. A script that hammers your endpoint sends real messages to real numbers and you pay for every one.

Storage and Comparison

Store a hash of the code with a per-record salt, not the plain value. Compare in constant time. Scope each code record to a purpose, so a code issued for login cannot be replayed against a payment confirmation. After a successful match, mark the record consumed inside the same transaction that grants the session, otherwise a race condition lets two requests use the same code.

For low-entropy values like six digits, a hash alone does not stop offline brute force if your database leaks, because the search space is small. The mitigation is short expiry and immediate deletion after use, not a heavier hashing algorithm.

Delivery Details That Affect Success

Send short messages. A code, a one line instruction and an expiry note. Long messages split into segments, cost more and can break delivery on some routes. Keep promotional content out of verification messages entirely, and never append a marketing link to a code.

When you test, use a virtual number so you can repeat the flow without exposing a personal line. Register with SMS Feel to provision numbers across many countries and read incoming verification codes through the API documentation.

Frequently Asked Questions

Is a four digit OTP safe enough?

Only with a short expiry and a strict attempt cap. Ten thousand combinations is thin if an attacker can guess freely, and adequate if they get three tries.

How many verification attempts should I allow?

Three to five per code is the common range. Track attempts per account and per IP so an attacker cannot simply request a new code after each lockout.

Should I invalidate the old code when resending?

Yes. Keeping two valid codes doubles the guessing surface and confuses users who receive the second message before the first.

Can I show the code in the app instead of sending an SMS?

That defeats the purpose of verification, because it no longer proves control of the phone number. Use an in-app code only for a second factor on an already verified device.

What should I log when a verification fails?

The request identifier, the reason category, the timestamp and the account reference. Never log the submitted code or the expected code.

Conclusion

None of these OTP best practices is difficult on its own; the value comes from applying all of them consistently across every flow that issues a code. Set the length, the expiry, the attempt cap and the resend cooldown as shared constants in one module, and every new feature inherits the same policy. To test delivery across many countries, open an SMS Feel account and start with a virtual number.

Related reading: more verification engineering notes on the SMS Feel blog.

Tags

#otp best practices #otp expiry #otp attempt limit #resend otp #otp code #verification #virtual numbers #api
Contact us