Skip to main content
Node.js & Express Guide Node.js 18+

Sending OTPs in
Node.js & Express

A production-ready, step-by-step technical guide to integrating the NepalOTP API into your Node.js backend. Learn how to handle requests, prevent abuse with Redis, and verify users securely.

Updated: August 2026
10 min read
ES Modules / CJS
otp.controller.js
Async / Await
import axios from 'axios';

export const sendOtp = async (phone) => {
  const { data } = await axios.post('https://nepalotp.com/api/v1/otp/send', {
    phone
  }, {
    headers: { Authorization: `Bearer ${process.env.NEPALOTP_API_KEY}` }
  });
  return data;
};
Target: Express Router 100% Axios / Fetch

Implementing phone verification from scratch is deceptively complex. It requires handling external API requests, generating cryptographically secure random codes, hashing them securely in your database, handling strict expirations, and managing retry attempts to prevent brute-force attacks.

In this guide, we will walk through building an enterprise-grade phone authentication flow in Node.js and Express using NepalOTP. We will also cover using Redis for sub-millisecond rate limiting.

1

Project Setup & Dependencies

Initialize a new Node.js project and install the necessary dependencies including Express, Axios, and dotenv:

Terminal npm
npm init -y
npm install express axios dotenv express-rate-limit
2

Express Controller Setup

Create an Express route handler to process OTP dispatch requests from your frontend:

controllers/auth.controller.js JavaScript
import axios from 'axios';

export const requestPhoneVerification = async (req, res) => {
  try {
    const { phone } = req.body;
    
    const response = await axios.post(
      'https://nepalotp.com/api/v1/otp/send',
      { phone },
      { headers: { Authorization: `Bearer ${process.env.NEPALOTP_API_KEY}` } }
    );
    
    return res.status(200).json(response.data);
  } catch (error) {
    return res.status(500).json({ error: error.response?.data || error.message });
  }
};
Build in Node.js

Ship Express OTP in 5 mins

Claim your free developer key with 100 test credits. No credit card required.

Get API Key →

Start verifying users today.

Get your API key in minutes. Test in sandbox. Ship when you're ready.