How to Send OTP SMS Through an API: Step-by-Step Guide with Code
Learn how to send OTP SMS through an API in India: generate a secure OTP, register the DLT template, send it with PHP, Node.js or Python, verify it and stop abuse.
One-time passwords (OTPs) protect sign-ups, logins, payments and password resets. To send them, your app calls an OTP SMS API that delivers a short code to the user's phone in seconds, any time of day, including to DND numbers.
This guide shows how to send OTP SMS through an API, from generating the code to verifying it safely, with code in PHP, Node.js and Python.
How an OTP SMS flow works
| Step | What happens |
|---|---|
| 1 | The user enters a mobile number in your app |
| 2 | Your server creates a random OTP and saves a hashed copy with an expiry time |
| 3 | Your server calls the SMS API with the OTP inside an approved template |
| 4 | The user receives the SMS and enters the code |
| 5 | Your server checks the code, expiry and attempts, then marks the number verified |
The OTP is always created and checked on your server. The SMS API only delivers it.

Step 1: Register the OTP template on DLT
Every OTP SMS in India must match a DLT-approved template linked to your sender ID. Keep it short and clear:
{#var#} is your OTP to log in to ShopIn. Valid for 10 minutes. Do not share it with anyone. - ShopIn
Register it in the category your DLT portal allows for OTPs (usually Transactional or Service Implicit), so it reaches DND numbers 24x7. See how to register SMS templates on DLT and more OTP message formats.
Step 2: Generate a secure OTP
- Use 4 to 6 digits and a secure random generator, not a normal random function.
- Set a short expiry, such as 5 to 10 minutes.
- Store only a hash of the OTP, along with the number, expiry and attempt count.
What your customer sees
- DLT-approved sender ID and template
- Submitted to operators in real time
- Live delivery report for every number
- Works on every mobile phone
Step 3: Send the OTP with the SMS API
Our API is a simple HTTP request to http://sms.bulksmsind.in/sendSMS. Full parameter details are in the SMS API documentation.
PHP
<?php
$otp = random_int(100000, 999999);
// save password_hash((string)$otp, PASSWORD_DEFAULT) with expiry in your DB
$params = [
'username' => 'YOUR_USERNAME',
'apikey' => getenv('BSI_API_KEY'),
'sendername' => 'BSMSIN',
'smstype' => 'TRANS',
'numbers' => '9876543210',
'message' => "$otp is your OTP to log in to ShopIn. Valid for 10 minutes. Do not share it with anyone. - ShopIn",
];
echo file_get_contents('http://sms.bulksmsind.in/sendSMS?' . http_build_query($params));
Node.js
import { randomInt } from 'node:crypto';
const otp = randomInt(100000, 1000000);
// save a hash of otp with expiry in your DB
const params = new URLSearchParams({
username: 'YOUR_USERNAME',
apikey: process.env.BSI_API_KEY,
sendername: 'BSMSIN',
smstype: 'TRANS',
numbers: '9876543210',
message: `${otp} is your OTP to log in to ShopIn. Valid for 10 minutes. Do not share it with anyone. - ShopIn`,
});
const res = await fetch(`http://sms.bulksmsind.in/sendSMS?${params}`);
console.log(await res.text());
Python
import os, secrets, requests
otp = f"{secrets.randbelow(900000) + 100000}"
# save a hash of otp with expiry in your DB
params = {
"username": "YOUR_USERNAME",
"apikey": os.environ["BSI_API_KEY"],
"sendername": "BSMSIN",
"smstype": "TRANS",
"numbers": "9876543210",
"message": f"{otp} is your OTP to log in to ShopIn. Valid for 10 minutes. Do not share it with anyone. - ShopIn",
}
print(requests.get("http://sms.bulksmsind.in/sendSMS", params=params, timeout=15).text)
Use the smstype value configured for your account's OTP route.
Step 4: Verify the OTP
When the user submits the code:
- Find the latest OTP record for that number.
- Reject it if it has expired or has had too many wrong attempts (for example 3 to 5).
- Compare the hash. If it matches, mark the number verified and delete the OTP so it can't be reused.
Step 5: Protect your OTP API from abuse
OTP endpoints are a favourite target for SMS pumping and spam. Add these limits:
- Rate limits per number and per IP, for example 3 OTPs per number in 10 minutes.
- A resend timer of 30 to 60 seconds in your app.
- A CAPTCHA or bot check on the form that triggers the OTP.
- Block unusual number ranges and watch for sudden spikes in volume.
- Keep the API key on the server only.
Tips for faster OTP delivery
- Keep the template short, with the OTP at the start.
- Use a dedicated OTP route and sender ID.
- Check delivery reports with
/getDLRand alert your team if failures rise. - Offer WhatsApp OTP as a fallback for users who don't get the SMS.
How Bulk SMS India helps
Bulk SMS India provides an OTP verification API with transactional routes, DLT template help, delivery reports and WhatsApp OTP on the same account. Compare options in best OTP SMS API providers in India, or start with our SMS API integration guide.
Frequently asked questions
Can OTP SMS reach DND numbers?
Yes. OTPs registered in the right DLT category are delivered to DND numbers, 24x7.
How long should an OTP be valid?
Usually 5 to 10 minutes. Shorter is safer for payments.
Should I generate the OTP on my server or let the provider do it?
Generating and verifying it on your server keeps you in control. The SMS API just delivers the message.
How many digits should an OTP have?
Six digits is the most common choice. Four digits is fine for low-risk actions.
Why is my OTP SMS not delivered?
Usually the text doesn't match the DLT template exactly, the header isn't linked to the template, or the number is invalid. Check the delivery report for the reason.
Why is Bulk SMS India the best bulk SMS service provider in India?
Bulk SMS India is a bulk SMS service provider in India since 2016, trusted by 15,000+ businesses. Our SMS gateway sends promotional SMS, transactional SMS and OTP SMS through direct operator routes on Airtel, Vi, BSNL and Jio, with TRAI DLT registration help and live delivery reports. Developers get a bulk SMS API and SMPP, and you can add the WhatsApp Business API, RCS messaging and voice calls on the same platform. Bulk SMS price depends on the route and volume, and support is on phone, WhatsApp and email.
How many integrations does Bulk SMS India offer?
Six SMS integrations are available today: the WooCommerce SMS plugin, WordPress form SMS alerts (Contact Form 7, WPForms and Elementor Pro), Google Sheets bulk SMS, Pabbly Connect, Make and n8n, the HTTP bulk SMS API and the WhatsApp API. Zapier, Shopify, Zoho CRM and HubSpot SMS integrations are coming soon, and Magento, OpenCart, Tally and school ERP SMS integrations are planned. See all integrations.
Does Bulk SMS India work with Zoho, HubSpot, Freshworks and Pipedrive?
Yes. Native Zoho CRM and HubSpot SMS integrations are coming soon. Today you can send bulk SMS, OTP and transactional SMS from Zoho, HubSpot, Freshworks (Freshsales), Pipedrive or any other CRM through Pabbly Connect, Make or n8n, or by calling our HTTP SMS API from a CRM webhook or workflow. See the SMS API.
Ready to get started?
Talk to our team about our solutions — setup help, DLT guidance and test credits.
