How to Add Phone OTP Verification in Node.js
A complete guide to integrating robust phone number verification using the FlashAuth API.
1. Sign up for FlashAuth and get your API key
Before you start coding, you'll need a FlashAuth API key. If you don't have an account yet, you can get started instantly with our generous free tier (50 free trial OTPs).
2. Set up your Node.js project
We'll use native fetch in Node.js to interact with the API. Create a new file auth.js and add your API key as an environment variable or directly in the code for testing.
3. Send OTP
To send an OTP to a user, make a POST request to the /v1/otp/send endpoint.
async function sendOtp(phoneNumber) {
const res = await fetch(process.env.FLASHAUTH_API_URL + '/v1/otp/send', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-api-key': process.env.FLASHAUTH_API_KEY
},
body: JSON.stringify({ phone: phoneNumber })
});
return res.json();
}4. Verify OTP
When the user enters the code they received, verify it by making a request to /v1/otp/verify.
async function verifyOtp(phoneNumber, code) {
const res = await fetch(process.env.FLASHAUTH_API_URL + '/v1/otp/verify', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-api-key': process.env.FLASHAUTH_API_KEY
},
body: JSON.stringify({ phone: phoneNumber, otp: code })
});
return res.json();
}5. Built-in Session Handling (Optional)
FlashAuth also offers a higher-level Auth API that automatically handles JWT sessions for you. You can use /v1/auth/login and /v1/auth/verify instead of raw OTP endpoints.