WhatsApp & Messaging 9 min read

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.

Umer Hasan

Umer Hasan

Co-Founder & CTO at Zapprex

MSG91 Tutorial: Send SMS and OTP with the MSG91 API (Step by Step with Code)
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.

At a glance: Make OTP login safe and reliable — Rate limits, Resend cooldown, Expiry, Fallbacks
At a glance: Make OTP login safe and reliable

Set up your account

Set up your account: 5 steps
  1. 1

    Sign up and complete KYC: create an MSG91 account and verify your business.

  2. 2

    Add DLT details: enter your DLT Entity ID and sender ID (header).

  3. 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. 4

    Get your auth key: copy the authentication key from the dashboard and store it in your server’s environment variables.

  5. 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.

Send and verify OTP (illustrative — confirm endpoints in MSG91’s current docs)javascript
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.”

— Umer Hasan, CTO
  • 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

MSG91OTP APISMS API IndiaOTP Login
Umer Hasan
Written by

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.

FAQ

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.

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