WhatsApp OTP Example
Send WhatsApp OTP with Node.js
A backend-only Node.js example that keeps the DNZ API key private.
Setup
- Install Node.js 18 or newer.
- Store WHATSAPP_OTP_API_KEY and WHATSAPP_OTP_ENGINE_URL in server environment variables.
Code
export async function sendOtpWithDnz(phone, code) {
const res = await fetch(`${process.env.WHATSAPP_OTP_ENGINE_URL}/api/send-otp`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
apiKey: process.env.WHATSAPP_OTP_API_KEY,
number: phone,
message: `Your verification code is ${code}`,
}),
});
if (!res.ok) {
throw new Error(`DNZ WhatsApp OTP failed: ${res.status} ${await res.text()}`);
}
return res.json();
}Production notes
- Generate and verify the OTP in your own backend.
- Do not send WHATSAPP_OTP_API_KEY to browser code.
