MSG91 Tutorial: Send SMS and OTP with the MSG91 API (Step by Step with Code)
Set up MSG91 for transactional SMS and OTP login: account and DLT setup, creating templates, sending and verifying OTPs from your backend, retry and resend, and best practices for delivery.

Table of contents
What MSG91 offers
MSG91 is an Indian communications platform for SMS, OTP, WhatsApp, email and voice. For most apps, the two features that matter are transactional SMS (alerts and notifications) and the OTP service, which generates, sends and verifies one-time passwords for you.

Set up your account

- 1
Sign up and complete KYC: create an MSG91 account and verify your business.
- 2
Add DLT details: enter your DLT Entity ID and sender ID (header).
- 3
Create an OTP template: in the OTP section, add a template matching your DLT-approved text, such as “Your login OTP is ##OTP##. -ACME”, and link the DLT template ID.
- 4
Get your auth key: copy the authentication key from the dashboard and store it in your server’s environment variables.
- 5
Add balance: recharge credits for your expected volume.
Send and verify an OTP from your backend
Call MSG91 from your server only — never from the app or browser, where the auth key could be stolen.
const BASE = "https://control.msg91.com/api/v5"
const headers = { authkey: process.env.MSG91_AUTH_KEY, "Content-Type": "application/json" }
// 1. Send OTP
await fetch(BASE + "/otp?template_id=" + process.env.MSG91_OTP_TEMPLATE + "&mobile=919800000000", {
method: "POST", headers,
})
// 2. Verify OTP entered by the user
const res = await fetch(BASE + "/otp/verify?otp=482931&mobile=919800000000", { headers })
const data = await res.json() // check the success type in the response
// 3. Resend (text or voice) if the user did not receive it
await fetch(BASE + "/otp/retry?retrytype=text&mobile=919800000000", { headers })Make OTP login safe and reliable
“An OTP endpoint without rate limiting is an open wallet. Protect it before launch.”
- Rate limits: cap OTP requests per phone number and per IP to prevent abuse and SMS cost attacks.
- Resend cooldown: allow a resend after 30–60 seconds, with a limit per session.
- Expiry: keep OTPs valid for a few minutes only.
- Fallbacks: offer voice OTP or WhatsApp OTP if SMS fails.
- Autofill: use Android’s SMS Retriever API or iOS one-time-code autofill for a smoother experience.
- Monitor delivery: watch delivery reports and failure reasons in the dashboard.
Topics
Umer Hasan
Co-Founder & CTO at Zapprex
Leads engineering, cloud infrastructure and AI automation at Zapprex Technologies, with 7+ years of full-stack and mobile experience.
Keep reading
All articlesWhatsApp Business API Explained: How to Get Access, Pricing Basics and What You Can Build
A beginner-friendly explanation of the WhatsApp Business Platform: Cloud API vs Business Solution Providers, verification, phone numbers, message templates, pricing basics, limits and the most useful use cases.
WhatsApp Cloud API Tutorial: Send Your First Message and Receive Replies with Webhooks
A developer tutorial for Meta’s WhatsApp Cloud API: create the app, get a token and phone number ID, send a template message, set up and verify webhooks, reply to customers and go to production safely.
WhatsApp Message Templates: Categories, Approval Tips and 15 Examples That Get Approved
Learn the three WhatsApp template categories, how variables, buttons and media headers work, why templates get rejected, and copy-ready examples for orders, reminders, OTPs and offers.
Questions readers ask
Yes, for sending SMS with your own sender ID in India. MSG91 links your DLT Entity ID, header and template IDs to your account.
MSG91 also offers WhatsApp messaging. WhatsApp OTPs need an approved authentication template on the WhatsApp Business Platform.
Add rate limits per phone and IP, CAPTCHA or bot protection after several attempts, resend cooldowns and monitoring for unusual spikes.
Have a project in mind?
Let's build something that moves your business forward.
Share your idea, process or problem — we will come back with a clear plan, timeline and cost range. No technical spec needed.
- Founder reply within 1 business day
- NDA on request · 100% code ownership
- Talk directly to the engineers
