How to Send OTP via API in Node.js (Step-by-Step Guide)
Sending OTP (One-Time Password) messages is a critical feature for modern applications like user authentication, payments, and account verification. In this guide, youβll learn how to send OTP via API in Node.js using smsgatewayhub.com.
π Prerequisites
Before you start, make sure you have:
A Node.js environment installed
An active account on smsgatewayhub.com
Your API key (provided in your dashboard)
A verified sender ID
π¦ Step 1: Install Required Package
Weβll use axios to send HTTP requests.
npm install axios
π Step 2: Understand the API Endpoint
Typically, an SMS API requires:
API Key
Mobile Number
Message Content (OTP)
Sender ID
Example API endpoint (replace with your actual one) refer : https://www.smsgatewayhub.com/free-sms-gateway-developer-api
π» Step 3: Node.js Code to Send OTP
Hereβs a simple example:
const axios = require('axios'); const sendOTP = async (mobile, otp) => { try { const response = await axios.post('https://smsgatewayhub.com/send', { apiKey: 'YOUR_API_KEY', number: mobile, message: `Your OTP is ${otp}`, senderId: 'SMGHUB' }); console.log('OTP sent successfully:', response.data); } catch (error) { console.error('Error sending OTP:', error.response?.data || error.message); } }; // Example usage sendOTP('919876543210', '123456');
π Step 4: Generate OTP Securely
Use a secure OTP generator:
const generateOTP = () => { return Math.floor(100000 + Math.random() * 900000); };
β‘ Step 5: Best Practices
OTP should expire in 2β5 minutes
Never store OTP in plain text
Use HTTPS APIs only
Implement rate limiting to prevent abuse
π Step 6: Handle API Responses
Make sure your API returns:
Success status
Message ID
Error codes (if failed)
Example:
{ "status": "success", "messageId": "abc123" }
π§ͺ Step 7: Test Your Integration
Test with multiple numbers
Check delivery speed
Verify OTP accuracy
Monitor failures
π― Conclusion
Integrating OTP SMS in Node.js using smsgatewayhub.com is straightforward. With proper implementation and security practices, you can ensure reliable and fast OTP delivery for your users.
For any Kind of Help please call us at : +91 99079 22122