How to add OTP verification to your app, with code

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

7 min read

Phone OTP verification needs two server-side calls: one to send a code to the number, one to check the code the user typed. This guide shows both, with working examples, and the details that make it robust.

How the flow works

  1. The user enters their mobile number in your app.
  2. Your server calls POST /v1/otp/send. DiOTP generates the code, sends the SMS and returns a request_id.
  3. The user types the code they received.
  4. Your server calls POST /v1/otp/verify with the request_id and the code. A success response means the number is verified.

Your server never sees or stores the code. DiOTP holds it, expires it and counts the attempts, so a leaked database cannot be used to replay codes.

Step 1: get an API key

Create an account and copy a test key from the API Keys page of your dashboard. Keep it on your server in an environment variable such as DIOTP_API_KEY; never put it in a mobile app or web page.

Step 2: send the code

Send the number and, optionally, what the code is for. The Idempotency-Key header makes retries safe: if your request times out and you send it again with the same key, the user does not get a second code.

curl -X POST https://api-diotp.diorbit.com/v1/otp/send \
  -H "Authorization: Bearer $DIOTP_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: 5f2b8c1e-signup-9876543210" \
  -d '{
    "to": "9876543210",
    "country_code": 91,
    "channel": "sms",
    "purpose": "signup"
  }'

Step 3: verify what the user typed

Pass the request_id from step 2 and the code. A 200 response means the code was right; an error tells you why it was not, for example a wrong or expired code.

curl -X POST https://api-diotp.diorbit.com/v1/otp/verify \
  -H "Authorization: Bearer $DIOTP_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "request_id": "7c9e6679-7425-40de-944b-e07fc1f90ae7",
    "code": "482913"
  }'

Details that make it robust

  • Show a countdown before "Resend code": DiOTP allows one resend to the same number every 30 seconds and returns resend_cooldown if you try sooner.
  • Treat the verify call as the only proof. Never trust a client that says the number is verified.
  • Use webhooks if you want delivery status (sent, delivered, failed) in your own system.
  • Start in test mode: your free test credits send to your own verified number while you build.

API reference Send endpoint Verify endpoint

Quickstart Postman collection API reference

Framework walkthroughs with complete code: OTP verification in Django, Spring Boot, Go and Ruby on Rails. On Supabase, see Supabase phone auth in India.

Questions people ask

Any language that can make an HTTPS request. The API is plain REST with JSON; the examples above cover cURL, Node.js, Python and PHP.

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.