Skip to content
Intracent
Log in Talk to sales Start building
Verify (OTP & 2FA) Products Virtual numbers Wholesale SMS Industries Developers API reference Pricing Compliance Log in
Talk to sales Start building

OTP and 2FA over SMS: security best practices for developers

Code length, expiry, rate limiting and fraud protection — how to build SMS verification that is secure and still easy to use.

IT
Intracent Team
· 7 min read
In this article 6 sections
  1. Generate codes securely
  2. Keep the window short
  3. Rate-limit everything
  4. Defend against SMS pumping
  5. Keep the logic on your server
  6. Design for humans

One-time passcodes sent by SMS remain the most widely used second factor in the world. Every user already has a phone number, there is nothing to install, and the flow is familiar. Done well, SMS verification blocks the vast majority of automated account-takeover attempts.

Done badly, it becomes an attack surface of its own. Here is how to build it properly.

Generate codes securely

  • Use a cryptographically secure random generator — never Math.random() or a timestamp.
  • Six digits is the sweet spot: one million combinations, still easy to type.
  • Store a hash of the code, not the code itself.
  • Invalidate a code the moment it is used successfully.

Keep the window short

Codes should expire in five to ten minutes. Long-lived codes give attackers more time, and users rarely need more than a minute to enter one. Show the expiry in the message itself so users know to act.

Your Acme code is 418209. It expires in 10 minutes.
Never share this code with anyone.

Rate-limit everything

Limit verification attempts per code (three to five), sends per phone number (for example five per hour), and sends per IP address. Without limits, attackers can brute-force codes — or run up your bill by triggering thousands of messages.

Defend against SMS pumping

Artificially inflated traffic — or SMS pumping — is fraud in which bots trigger OTPs to premium or high-cost number ranges, and the attacker takes a share of the termination fees. Protect yourself with:

  • Country allow-lists: only send to the markets where you actually have users.
  • Number lookup before sending, to reject invalid and non-mobile numbers.
  • Anomaly alerts on sudden volume spikes to unusual prefixes.
  • A CAPTCHA or proof-of-work step before the "send code" button.

Keep the logic on your server

Generate the code, hash it and set its expiry on your own server, never in the client. Then send it through the Intracent REST or HTTP API as an ordinary SMS, and compare the code the user enters against the stored hash.

# Generate and store a hashed code server-side, then send it:
POST https://api1.intracent.com/rest/send_sms
     ?acc_id=…&from=Acme&to=61412345678
     &message=Your%20Acme%20code%20is%20418209
→ { "message_id": "test34-2026…-7a01fa" }

Design for humans

Support autofill with the one-time-code input attribute on web and the SMS Retriever API on Android. Offer a "resend" option after 30 seconds, and a voice-call fallback for users whose SMS is delayed. Security that frustrates users gets bypassed.

The best verification flow is the one users barely notice — and attackers can't get past.

IT
Intracent Team
Melbourne, Australia, Intracent
Put this into practice.
Free account, no credit card.
Start free

Keep reading