OTP security best practices: expiry, attempts and abuse limits

Written and reviewed by the DiOTP team Published 27 November 2026Last updated 27 November 2026

9 min read

An OTP is only as safe as the rules around it. A six-digit code sent by SMS can be a solid second factor, or an open door, depending on how long it lives, how many guesses it allows and who can trigger it. These are the practices that stop account takeover and stop attackers from running up your SMS bill, with the defaults DiOTP applies for you.

What you are defending against

Four kinds of attack account for most OTP abuse:

  • Guessing. A script tries codes until one works. With unlimited attempts, every code is eventually guessable.
  • SMS pumping. Bots trigger thousands of OTPs to numbers the attacker profits from, and you pay for every message.
  • Phishing and social engineering. A caller or a fake page persuades the user to read out or type in the code.
  • Interception and SIM swap. Someone gets the victim's number moved to a new SIM, or reads messages on a phone they have access to.

No single rule stops all four. The practices below work together: some make guessing useless, some make pumping expensive for the attacker instead of you, and some limit the damage when a code does fall into the wrong hands.

1. Expire codes quickly

A code should be valid for minutes, not hours. Long-lived codes give an attacker more time to guess or intercept them, and they make phishing easier because a stolen code is still useful later.

Five minutes is long enough for a real user to read the message and type the code, and short enough to keep the window small. DiOTP codes expire five minutes after they are sent; after that, verify returns expired and you send a new one.

2. Limit wrong attempts

Allow a small number of wrong guesses per code, then require a new one. A six-digit code has a million combinations; unlimited attempts make it guessable. Five tries leave room for typos and stop brute force cold.

DiOTP allows five wrong attempts per code and then returns too_many_attempts. Count attempts on the server, never in the app, so restarting the app does not reset them.

3. Cool down resends and cap each number

Attackers trigger OTPs to premium or random numbers in bulk to run up your bill, a fraud known as SMS pumping. A cooldown between resends and a cap per number per hour stop it without bothering real users.

LimitDiOTP default
Resend to the same numberOnce every 30 seconds
Codes per number per hour3
Codes per number per day10
Sends per API key per minute30

DiOTP enforces these by default and returns a clear error code for each, so your app can show the right message, such as a countdown before "Resend code". Requests stopped by a limit are not sent and not charged.

4. Put a check in front of the OTP form

Limits per number do not stop an attacker who rotates through thousands of different numbers. Add friction before the send: a CAPTCHA or bot check on the form, a limit per IP address or device on your side, and blocking for country codes you do not serve. These cost real users almost nothing and make automated abuse much harder.

5. Verify on the server only

Check the code from your server, with your secret API key, and act on the result there. A check done in the browser or app can be skipped by anyone who edits the client. Keep the API key on the server too: a key inside a mobile app can be extracted and used to send messages at your cost.

6. Make retries safe

Networks time out. Send an idempotency key with each send request so a retried request returns the original result instead of sending a second code. It keeps users from receiving two different codes and wondering which one is right.

7. Never log or store codes

Codes do not belong in your logs, analytics or database. With DiOTP your server never receives the code at all; it only receives a request_id, and sends back what the user typed. A leaked database or log file then contains nothing that can be replayed.

8. Write messages that resist phishing

The message itself is a security control. Say what the code is for and tell the user never to share it. A user who reads "to approve a payment of Rs 4,500" is far less likely to read the code out to a caller pretending to be support.

Example: 482913 is your code to approve a payment of Rs 4,500 to ABC Store. It expires in 5 minutes. Never share it, not even with our staff.

And never ask users for their code by phone, chat or email yourself. If your support team never asks, users learn that anyone who does is an attacker.

9. Add more for high-risk actions

SMS OTP proves control of a phone number at that moment. It cannot tell you whether the number was recently moved to a new SIM. For the riskiest actions, such as changing the phone number on an account, adding a payout account or a large transfer, add something more: a delay before the change takes effect, a notification to the old number or email, or a second factor such as an authenticator app.

10. Watch the numbers

Pumping and abuse show up in your data before they show up on your bill. Watch the share of sent codes that are verified: real users verify most codes, so a sudden drop means many codes are going to numbers nobody is reading. Also watch for spikes in sends to one country code or number range. DiOTP's message log shows every code with its status, and webhooks can feed sent, delivered and verified events into your own monitoring.

A checklist

  • Codes expire within minutes (DiOTP: five).
  • Five or fewer wrong attempts per code.
  • Resend cooldown and per-number hourly and daily caps.
  • Bot check and per-IP limits in front of the form.
  • Verification and the API key on the server only.
  • Idempotency keys on every send.
  • No codes in logs, analytics or databases.
  • Messages that say what the code is for and never to share it.
  • Extra checks for phone number changes and payouts.
  • An alert when the verified-to-sent ratio drops.

OTP service system design Add OTP verification, with code OTP SMS API

How DiOTP secures OTPs Rate limits reference

Questions people ask

A few minutes. Five minutes gives real users enough time and keeps the window for guessing or phishing small. DiOTP codes expire five minutes after sending.

Start building your customer messaging with DiOTP

Free test credits on sign-up, API keys in a minute, and a team that picks up the phone.