SMSNET
Guide

How to Build an SMS OTP Verification Flow

To verify a number by SMS, send a generated code with one call, ask the user to type it, then check it with a second. SMSNET generates the code, stores only its hash, and locks it after 5 wrong tries. REST is two POSTs (/api/v1/otp/send, /api/v1/otp/verify); agents get the same flow as send_otp and verify_otp tools.

How do I send a verification code?

POST the recipient number to /api/v1/otp/send with your API key in the X-API-Key header:

curl -X POST https://smsnet.net/api/v1/otp/send \
  -H "X-API-Key: $SMSNET_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"to": "+15551234567"}'

A successful call returns 201 with an otp_id you keep for the verify step:

{
  "otp_id": "otp_01j9x8k2...",
  "to": "+15551234567",
  "expires_at": "2026-10-16T12:05:00.000Z",
  "message_id": "msg_01j9x8k2..."
}

The default message looks like this on the phone (the code here is an example):

Your code is 482913

1 segment · GSM-7

The defaults fit most logins: a 6 digit code that lives 5 minutes and reads "Your code is {code}". Tune it with optional fields: length from 6 to 8, ttl_s from 60 to 900, template (must contain {code}, at most 480 characters), sim_id to pick the sending line, and client_ref to attach your own session id, which comes back on a successful verify. Testing with a sandbox key returns the code in the response, and that is the only case where the code is ever visible to you.

How do I check the code the user types?

POST the otp_id and the typed code to /api/v1/otp/verify:

curl -X POST https://smsnet.net/api/v1/otp/verify \
  -H "X-API-Key: $SMSNET_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"otp_id": "otp_01j9x8k2...", "code": "482916"}'

A correct code returns valid: true and is consumed at once, so it can never be reused. Pass to instead of otp_id and the newest code for that number is checked, which matches what the person is looking at when several codes are live. The same check in Python:

import os
import requests

API = "https://smsnet.net/api/v1"
HEADERS = {"X-API-Key": os.environ["SMSNET_API_KEY"]}

def verify_otp(otp_id: str, code: str) -> bool:
    r = requests.post(
        f"{API}/otp/verify",
        headers=HEADERS,
        json={"otp_id": otp_id, "code": code.strip()},
        timeout=15,
    )
    r.raise_for_status()
    body = r.json()
    if body.get("valid"):
        return True
    print("rejected:", body.get("reason"), "attempts left:", body.get("attempts_left"))
    return False

What does each verify response mean?

A correct code succeeds once; anything else returns valid: false with a reason that tells you exactly what to do next.

ResponseMeaningWhat to do
valid: trueThe code matched and is now consumedLog the user in; the same code will never verify again
mismatchWrong digits; attempts_left counts down with each tryLet the user retry while attempts remain
expiredPast expires_at (5 minutes by default)Send a fresh code with /otp/send
too_many_attempts5 wrong tries, so the code is lockedSend a fresh code; the locked one is dead
not_foundUnknown otp_id, or the code was already usedCheck the id, or send a fresh code

One success and four failure branches: what a verify call can return.

OTP verify outcomes: one success and four ways a check can fail A two row diagram. Posting to otp send returns otp_id with status 201, the user types the code, and posting to otp verify returns valid true once when correct, or valid false with mismatch, expired, too many attempts, or not found. Send once, check once: what verify returns A correct code returns valid true and is consumed. Anything else names its reason. POST /otp/send POST /otp/verify valid: true 201 plus otp_id, 6 digits otp_id plus typed code consumed, log user in user types code match valid: false, four branches mismatch expired too_many_attempts not_found retry while attempts remain past expires_at, send a fresh code 5 wrong tries, locked, send a fresh code bad id or reused, check id or resend Codes live 5 minutes by default, lock after 5 wrong tries. Only a hash is stored, never the code.
State diagram: POST to otp send returns otp_id with status 201, the user types the code, POST to otp verify returns valid true once when correct, or valid false with mismatch, expired, too many attempts, or not found.

How does the same flow look for an AI agent?

An agent wired to SMSNET through MCP calls the same two steps as tools instead of HTTP. send_otp texts the code and returns an otp_id, then the agent asks the person for the code and calls verify_otp, which returns valid: true once and locks after five wrong tries, exactly like the REST version. The MCP server guide shows the full setup; the agent never sees the code, it only passes along what the person typed.

Why does the code text arrive so fast?

One time codes are marked transactional on the way out, so they skip the pacing queue and quiet hours and reach the handset in about a second. The message also expires with the code, so a code text can never arrive late and confuse someone with a dead number. Reserve this priority for codes and alerts the recipient is actively waiting for; routine texts go through POST /api/v1/messages/send with transactional left off, as shown in Send an SMS from a Python Script.

What errors should I handle?

StatusWhenWhat to do
201Code sentStore the otp_id against the login session
400Bad number, length outside 6 to 8, ttl_s outside 60 to 900, or a template without {code}Fix the request; the error names the field
429 otp_rate_limitedMore than 3 live codes for one number in 10 minutesWait for retry_after_s, or reuse the newest code
429 otp_verify_rate_limitedMore than 10 verify calls for one number in 10 minutesSlow down; the throttle counts every call so guessing cannot dodge the per code cap

Everything here runs on the free plan: 300 messages a month, 20 a day, and replies are never counted. For the rest of the API surface, start at What Is an SMS API and How to Use One.

FAQ

How do I send an OTP by SMS with the API?

POST JSON with to to https://smsnet.net/api/v1/otp/send with your SMSNET API key in the X-API-Key header. The 201 response returns an otp_id, an expiry timestamp and a message_id. Optional fields set the code length from 6 to 8 digits, the lifetime from 60 to 900 seconds, and a custom template that must contain {code}.

How do I verify the code the user types?

POST JSON with otp_id and code to https://smsnet.net/api/v1/otp/verify. A correct code returns valid true and is consumed immediately, so it can never be reused. You can pass to instead of otp_id to check the newest code sent to that number.

What happens after five wrong codes?

The code locks and verify returns valid false with reason too_many_attempts. The locked code can never succeed, so send a fresh code with /otp/send and have the user type the new one.

How long is a verification code valid?

Five minutes by default, configurable from 60 to 900 seconds with ttl_s. The text message carrying the code expires at the same moment, so a late text never arrives with a dead code.

Do you store the verification code?

No. SMSNET stores only a hash of the otp_id plus the code, never the code itself, and compares each guess in constant time. Nobody with database access can read a live code, and a sandbox key is the only case where the code is returned to the caller for testing.

Can an AI agent run the OTP flow?

Yes. The MCP server exposes send_otp and verify_otp tools with the same behavior as the REST endpoints: send_otp texts the code and returns an otp_id, and verify_otp checks what the person typed, consumes a correct code once, and locks the code after five wrong tries.

This is a spoke of What Is an SMS API and How to Use One. Siblings: Send an SMS from a Python Script and What Is an MCP Server for SMS?. To catch replies to your codes, read How to Receive SMS and Handle Replies with Webhooks.

Writing the text itself? The OTP message templates give code-first wording in English and Hebrew with a live segment count.

Send your first message today

Free plan, no card: 400 messages a month from your own number, with the API, webhooks and MCP included.

Get the app See pricing

Get it on Google Play

Written by , who builds SMSNET. Product facts on this page are checked against the live pricing page and the running API on the date shown above.