# 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.

Source: https://resalax.com/resources/otp-best-practices/
Language: English · Category: OTP & verification · Updated: 2026-11-09
Publisher: Resalax (https://resalax.com)

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.randomInt` in Node.js or `secrets` in Python. Never `Math.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](https://resalax.com/resources/otp-expiry-time/) 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](https://resalax.com/resources/otp-resend-ux/).

## 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](https://resalax.com/resources/sms-character-limits-gsm7/).

## Make entry effortless

- Use `autocomplete="one-time-code"` and `inputmode="numeric"` on the input.
- Support paste, and auto-submit when all digits are entered.
- For web apps, add the origin-bound line `@yourdomain.com #482615` at the end of the SMS so browsers can autofill safely. See [OTP autofill on iOS and Android](https://resalax.com/resources/otp-autofill-ios-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](https://resalax.com/resources/sms-pumping-fraud-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.

> Resalax applies per-number rate limits and configurable expiry to every OTP, and reports delivery per message. [Learn more](https://resalax.com/).
