OTP & verification

What Is an SMS OTP, and How Does It Work?

A clear explanation of SMS one-time passwords: how codes are generated, sent, and verified, where they are used, their security limits, and how to implement them well.

Updated · 3 min read

An OTP, or one-time password, is a short code that is valid for a single use and a short time. An SMS OTP is that code delivered by text message to a phone number. You have used one every time an app said "enter the 6-digit code we sent you".

Why products use OTPs

OTPs answer one question: does this person control this phone number right now? That single check supports many flows:

  • Signup verification, so accounts are tied to real, reachable numbers.
  • Passwordless login, where the code replaces a password. See passwordless login with phone OTP.
  • Two-factor authentication, as a second step after a password.
  • Transaction confirmation, such as approving a payment or changing a payout account.
  • Account recovery when a user forgets their password.

How it works, step by step

  1. The user enters a phone number in your app.
  2. Your backend generates a random code, usually 6 digits, using a cryptographically secure random generator.
  3. You store a hash of the code, never the code itself, with an expiry time and an attempt counter.
  4. You send the code by SMS through an SMS API, which passes it to the mobile network.
  5. The user types or autofills the code.
  6. Your backend compares the submitted code with the stored hash, checks expiry and attempts, and on success marks the number as verified and deletes the code.

Many SMS providers offer a "verify" API that does steps 2, 3, and 6 for you, so your backend only calls "send" and "check".

What makes a good OTP

  • 6 digits is the common balance between security and usability.
  • Short validity, typically 5 to 10 minutes. See how long an OTP should be valid.
  • Limited attempts, for example 5 wrong tries before the code is invalidated.
  • Rate-limited sending, so nobody can request hundreds of codes. This also protects you against SMS pumping.
  • Clear message text that names your brand and warns users not to share the code.

Our OTP best practices covers each of these in detail.

How secure is SMS OTP?

SMS OTP is much better than no second factor, and it is easy for every user, which is why it is so common. Its known weaknesses are SIM swap attacks, phishing pages that relay codes in real time, and malware on the phone. For most consumer apps, SMS OTP combined with good risk checks is a reasonable choice; for high-risk actions you may add stronger methods. See OTP security and SIM swap.

SMS OTP vs other channels

Codes can also be delivered by WhatsApp, email, or voice call, or generated in an authenticator app. SMS remains the default because it reaches every phone without an app. WhatsApp is a strong alternative in markets where it is widely used; see WhatsApp OTP vs SMS OTP.

Getting it right in practice

The hard part of OTP is not generating a code; it is delivering it quickly and reliably to every user in every country. Choose a provider with good routes to your users' networks, delivery reports, and protection against abuse. See how to choose a phone verification API.