How to Implement OTP Verification in a Web Application (React + Node.js + Fast2SMS)
One-Time Password (OTP) verification is one of the most widely used authentication mechanisms in modern web applications. Whether you're building a user registration system, login flow, password reset feature, or mobile number verification, OTP authentication helps verify that users actually own the phone number they're using.
For developers building applications in India, there's an additional requirement: DLT (Distributed Ledger Technology) compliance. Before sending transactional SMS messages, businesses must register with a telecom-approved DLT platform and create approved SMS templates.
In this article, we'll cover the complete OTP implementation workflow using:
- βοΈ React
- π’ Node.js
- π Express.js
- π© Fast2SMS
- π± DLT Compliance
Let's dive in!
π OTP Verification Flow
User Registration
β
βΌ
Enter Mobile Number
β
βΌ
Generate OTP
β
ββββββββββΊ Store OTP (Redis / Database)
β
βΌ
Fast2SMS API
β
βΌ
User Receives OTP
β
βΌ
User Enters OTP
β
βΌ
Verify OTP
β
βΌ
Registration/Login Successful
Why Use OTP Verification?
OTP authentication improves application security by verifying that users have access to their registered mobile number.
Common use cases
- User Registration
- Login Authentication
- Password Reset
- Mobile Number Verification
- Two-Factor Authentication (2FA)
Unlike passwords, OTPs are temporary and expire after a short duration, making them much more secure against unauthorized access.
Step 1: Register on a DLT Platform
If you're sending SMS messages in India, your business must first register on a telecom-approved DLT platform.
DLT registration helps telecom operators prevent spam and fraudulent SMS messages.
Popular DLT providers include:
- Jio DLT
- Airtel DLT
- Vodafone Idea (Vi) DLT
After registration, you'll be able to create:
- Business Entity
- Sender ID
- SMS Templates
- Template IDs
Step 2: Register Your Business Entity
The next step is registering your organization.
Typical requirements include:
- Company Name
- PAN
- GST
- Business Registration Documents
- Authorized Person Details
Requirements may vary depending on the telecom provider.
Step 3: Create a Sender ID
A Sender ID identifies your organization when SMS messages are delivered.
Example:
ABCORG
Users receive SMS like this:
ABCORG: Your OTP is 583214.
Step 4: Create an OTP SMS Template
Every OTP message must use an approved SMS template.
Example:
Your OTP for registration is {#var#}.
This OTP is valid for 5 minutes.
Do not share this OTP with anyone.
Here,
{#var#}
is replaced with the generated OTP.
Example SMS:
Your OTP for registration is 583214.
This OTP is valid for 5 minutes.
Do not share this OTP with anyone.
Step 5: Obtain the Template ID
Once approved, the DLT portal provides a Template ID.
Example:
1707161234567890123
Store these values safely:
- Entity ID
- Sender ID
- Template ID
You'll need them while configuring your SMS provider.
Step 6: Configure Fast2SMS
After DLT approval, create an account with Fast2SMS.
Your application architecture should look like this:
React Frontend
β
βΌ
Node.js + Express
β
βΌ
Fast2SMS API
β
βΌ
User Mobile
Never call the Fast2SMS API directly from React. Keep API keys secure by sending SMS requests through your backend.
Step 7: Generate OTP
Generate a random six-digit OTP on your server.
const otp = Math.floor(100000 + Math.random() * 900000);
console.log(otp);
Example output:
583214
Step 8: Store OTP
Store the OTP temporarily.
Example:
{
"phone": "9876543210",
"otp": "583214",
"expiresAt": "2026-08-14T11:00:00"
}
For production systems:
- β Store hashed OTPs
- β Set expiration time
- β Use Redis whenever possible
Step 9: Send OTP
Example:
await sendSMS({
phone: userPhone,
otp
});
Fast2SMS sends the SMS to the user's mobile number.
Step 10: User Enters OTP
React sends the OTP to your backend.
await axios.post("/api/auth/verify-otp",{
phone,
otp
});
Step 11: Verify OTP
Compare the stored OTP with the submitted OTP.
if (enteredOtp === storedOtp) {
// Verified
} else {
// Invalid OTP
}
Also verify the expiration time.
if(Date.now() > otpExpiresAt){
return res.status(400).json({
message:"OTP expired"
});
}
Only proceed if the OTP is valid and hasn't expired.
Example API Endpoints
Send OTP
POST /api/auth/send-otp
Request
{
"phone":"9876543210"
}
Response
{
"success":true,
"message":"OTP sent successfully"
}
Verify OTP
POST /api/auth/verify-otp
Request
{
"phone":"9876543210",
"otp":"583214"
}
Response
{
"success":true,
"message":"OTP verified successfully"
}
Security Best Practices
Never build an OTP system without implementing proper security measures.
β Hash OTPs before storing them.
β Set OTP validity to 5 minutes.
β Delete OTP after successful verification.
β Allow a maximum of 5 verification attempts.
β Add a 30β60 second resend cooldown.
β Validate mobile numbers.
β Rate-limit OTP APIs.
β Store API credentials securely.
β Use HTTPS.
These measures protect your application from abuse and brute-force attacks.
DLT vs Fast2SMS
Many developers think DLT is an SMS providerβbut that's not the case.
| DLT | Fast2SMS |
|---|---|
| Regulatory platform | SMS provider |
| Registers business | Sends SMS |
| Approves templates | Provides APIs |
| Creates Sender IDs | Delivers OTP |
| Provides Template IDs | Backend integration |
The complete flow looks like this:
DLT Registration
β
βΌ
Sender ID
β
βΌ
SMS Template Approval
β
βΌ
Fast2SMS Configuration
β
βΌ
Backend OTP API
β
βΌ
React Frontend
β
βΌ
OTP Verification
Final Implementation Flow
1. Register Business on DLT
β
2. Create Entity
β
3. Create Sender ID
β
4. Create OTP Template
β
5. Get Template ID
β
6. Configure Fast2SMS
β
7. Generate OTP
β
8. Store OTP
β
9. Send SMS
β
10. User Enters OTP
β
11. Verify OTP
β
12. Registration/Login Successful
Conclusion
Implementing OTP verification is more than generating a random six-digit number. A production-ready solution requires secure backend APIs, temporary OTP storage, expiration handling, rate limiting, and compliance with India's DLT regulations.
Using React, Node.js, Express.js, and Fast2SMS, you can build a secure, scalable, and reliable OTP authentication system for your applications.
If you're building authentication features for your next project, following this architecture will save you time and help you avoid common implementation mistakes.
Have questions or suggestions? Drop them in the comments!
β If you found this article useful, give it a β€οΈ and share it with fellow developers. Happy coding!
Top comments (0)