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
- The user enters their mobile number in your app.
- Your server calls POST /v1/otp/send. DiOTP generates the code, sends the SMS and returns a request_id.
- The user types the code they received.
- 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" }'
const res = await fetch("https://api-diotp.diorbit.com/v1/otp/send", { method: "POST", headers: { Authorization: `Bearer ${process.env.DIOTP_API_KEY}`, "Content-Type": "application/json", "Idempotency-Key": crypto.randomUUID(), }, body: JSON.stringify({ to: "9876543210", country_code: 91, channel: "sms", purpose: "signup", }), }); const otp = await res.json(); // Keep otp.request_id: you need it to verify.
import os, uuid, requests res = requests.post( "https://api-diotp.diorbit.com/v1/otp/send", headers={ "Authorization": f"Bearer {os.environ['DIOTP_API_KEY']}", "Idempotency-Key": str(uuid.uuid4()), }, json={ "to": "9876543210", "country_code": 91, "channel": "sms", "purpose": "signup", }, timeout=10, ) otp = res.json() # Keep otp["request_id"]: you need it to verify.
<?php $ch = curl_init("https://api-diotp.diorbit.com/v1/otp/send"); curl_setopt_array($ch, [ CURLOPT_POST => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_HTTPHEADER => [ "Authorization: Bearer " . getenv("DIOTP_API_KEY"), "Content-Type: application/json", "Idempotency-Key: " . bin2hex(random_bytes(16)), ], CURLOPT_POSTFIELDS => json_encode([ "to" => "9876543210", "country_code" => 91, "channel" => "sms", "purpose" => "signup", ]), ]); $otp = json_decode(curl_exec($ch), true); // Keep $otp["request_id"]: you need it to verify.
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" }'
const res = await fetch("https://api-diotp.diorbit.com/v1/otp/verify", { method: "POST", headers: { Authorization: `Bearer ${process.env.DIOTP_API_KEY}`, "Content-Type": "application/json", }, body: JSON.stringify({ request_id: requestId, code: codeTheUserTyped }), }); if (res.ok) { // The number is verified: sign the user in. } else { const { error } = await res.json(); // e.g. error.code === "invalid_code" }
res = requests.post(
"https://api-diotp.diorbit.com/v1/otp/verify",
headers={"Authorization": f"Bearer {os.environ['DIOTP_API_KEY']}"},
json={"request_id": request_id, "code": code_the_user_typed},
timeout=10,
)
if res.ok:
pass # The number is verified: sign the user in.
else:
error = res.json()["error"] # e.g. error["code"] == "invalid_code"<?php $ch = curl_init("https://api-diotp.diorbit.com/v1/otp/verify"); curl_setopt_array($ch, [ CURLOPT_POST => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_HTTPHEADER => [ "Authorization: Bearer " . getenv("DIOTP_API_KEY"), "Content-Type: application/json", ], CURLOPT_POSTFIELDS => json_encode([ "request_id" => $requestId, "code" => $codeTheUserTyped, ]), ]); $body = json_decode(curl_exec($ch), true); $verified = curl_getinfo($ch, CURLINFO_HTTP_CODE) === 200;
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.
