DEV Community

Cover image for πŸš€ How to Send Instant WhatsApp OTPs in Node.js (Sub-15ms Routing)
MUHAMMAD WAKEEL for 5MinutesAPI

Posted on

πŸš€ How to Send Instant WhatsApp OTPs in Node.js (Sub-15ms Routing)

Authentication is the most critical part of any SaaS or e-commerce application. But if you are still relying on traditional SMS gateways, you are likely dealing with delayed deliveries, low conversion rates, and high international costs.
WhatsApp Cloud API has completely changed the game with a 98% open rate and near-instant delivery. However, routing those messages efficiently without overpaying for third-party platform markups is a challenge developers face as they scale.
In this tutorial, we will build a lightning-fast WhatsApp OTP system using Node.js and 5MinutesAPIβ€”an enterprise-grade WhatsApp gateway built on Golang that routes messages in under 15ms.
Why 5MinutesAPI? (The Architectural Advantage)
Most legacy API providers add a fractional "per-message markup" on top of the official Meta API costs. When your app scales to millions of users, this markup turns into a massive, unnecessary expense.
We built 5MinutesAPI to solve this exactly:
Zero Markups: For high-volume users, our Dedicated WABA route connects directly to Meta billing. You pay a flat platform fee ($99/mo) and $0 in message markups.
Built for Speed: Our entire backend is written in Golang, optimized for high concurrency and sub-15ms routing.
Developer First: Get a production-ready API key in 5 minutes with zero red tape.
Prerequisites
Node.js installed on your machine.
Your 5MinutesAPI Key (Get it instantly for $0/mo on the Pay-As-You-Go Route at 5minutesapi.com).
Step 1: Install Axios
We will use axios to make a simple HTTP POST request to the API.
npm install axios

Step 2: Write the OTP Sending Logic
Create a file named sendOTP.js and add the following code. This function uses a pre-approved Meta template for OTPs.

const axios = require('axios');

// Your secure API Key from 5MinutesAPI Dashboard
const API_KEY = 'YOUR_5MINUTESAPI_KEY';

async function sendWhatsAppOTP(phoneNumber, otpCode) {
const endpoint = 'https://api.5minutesapi.com/v1/messages/send';

const payload = {
to: phoneNumber,
type: 'template',
template: {
name: 'auth_otp_secure',
language: {
code: 'en_US'
},
components: [
{
type: 'body',
parameters: [
{
type: 'text',
text: otpCode
}
]
},
{
type: 'button',
sub_type: 'url',
index: '0',
parameters: [
{
type: 'text',
text: otpCode
}
]
}
]
}
};

try {
const response = await axios.post(endpoint, payload, {
headers: {
'Authorization': Bearer ${API_KEY},
'Content-Type': 'application/json'
}
});

console.log('βœ… WhatsApp OTP Delivered Successfully in under 15ms!');
console.log('Message ID:', response.data.message_id);
Enter fullscreen mode Exit fullscreen mode

} catch (error) {
console.error('❌ Failed to send OTP:', error.response ? error.response.data : error.message);
}
}

// Generate a random 6-digit OTP and test the function
const testOTP = Math.floor(100000 + Math.random() * 900000).toString();
sendWhatsAppOTP('+1234567890', testOTP);

Step 3: Run Your Code
Execute the script in your terminal:
node sendOTP.js

Handling Scale and Deliverability
When you deploy this to production, you don't have to worry about rate limits or dropped messages. The API is designed to handle high-concurrency spikes (like Black Friday sales or viral app launches) effortlessly.
If an OTP fails to deliver (e.g., the user doesn't have WhatsApp installed), 5MinutesAPI instantly triggers a micro-refund to your wallet, so you never pay for undelivered auth messages.
Ready to upgrade your auth flow?
Grab your free instant API key and start building at 5MinutesAPI.com.

Top comments (0)