otp-gen-agent: Secure One-Time Password Generation for Nodejs
A small and secure one time password (otp) generator for Javascript based on nanoid.
![]()
If you find this utility library useful, you can buy me a coffee to keep me energized for creating libraries like this.
What is OTP-Gen-Agent?
otp-gen-agent
is a lightweight, flexible Node.js library designed to simplify one-time password (OTP) generation for authentication systems. With mobile number verification becoming the standard authentication method across India and many other regions, this utility provides developers with reliable OTP generation capabilities.
Installation
npm install otp-gen-agent --save
Core Features
- Simple API: Generate secure OTPs with minimal code
- Customizable: Control OTP length and character set
- Bulk Generation: Create multiple OTPs in a single operation
- Lightweight: No bloated dependencies
- Promise-based: Modern async/await support
- Webhook Support: Trigger webhooks for OTP generation events
Usage
Mobile number has become the defacto user authentication mechanism in India and hence, OTP generation is a very common phenomena. This is a small utility lib to generate OTP.
Standard OTP Generation
Generate a standard 6-digit numeric OTP:
const { otpGen } = require('otp-gen-agent');
const otp = await otpGen(); // '344156' (OTP length is 6 digit by default)
- Default OTP lenght: 6
- Default characters set: 0123456789 (Numeric [0-9])
Custom OTP Generation
Create OTPs with custom length and character sets:
const { customOtpGen } = require('otp-gen-agent');
const otp = await customOtpGen({length: 4, chars: 'abc123'}); // 'a3c1'
Arguments:
- options: optional
- length: custom otp length
- chars: custom characters
You can customise the OTP length and also the characters to be used for OTP generation.
- Default OTP lenght is 6.
- Default characters used to generate OTP is 0123456789
Bulk OTP Generation
Generate multiple OTPs in a single operation:
const { bulkOtpGen } = require('otp-gen-agent');
const otp = await bulkOtpGen(2); // Array of otps: ['344156', '512398']
const { bulkOtpGen } = require('otp-gen-agent');
const otp = await bulkOtpGen(2, {length = 5, chars: 'abcd123'} ); // Array of otps: ['312b3', 'bcddd']
Arguments:
- num: number of OTPs to be generated in bulk
- opts: optional argument
- length: custom otp length (default: 6)
- chars: custom characters (default: 0123456789)
When to Use Bulk Generation
The bulk generation feature is particularly useful when:
- Pre-generating OTPs for upcoming authentication requests
- Testing authentication systems with multiple users
- Creating backup validation codes
Webhook Support
The library provides webhook support to track OTP generation events. You can set up a webhook handler to receive notifications when OTPs are generated:
const { otpGen, bulkOtpGen, setWebhookHandler, WEBHOOK_EVENTS } = require('otp-gen-agent');
// custom webhook handler function
const webHookHandler = async (event, data) => {
switch (event) {
case WEBHOOK_EVENTS.OTP_GENERATED:
console.log('Single OTP generated:', data.otp);
break;
case WEBHOOK_EVENTS.OTP_BULK_GENERATED:
console.log(`Generated ${data.count} OTPs:`, data.otps);
break;
}
};
// Set up webhook handler
setWebhookHandler(webHookHandler);
// Generate OTPs - webhook will be triggered automatically
const otp = await otpGen(); // Triggers OTP_GENERATED event
const bulkOtps = await bulkOtpGen(3); // Triggers OTP_BULK_GENERATED event
Webhook Events:
otp-generated
: Triggered when a single OTP is generated- Payload:
{ otp: string }
- Payload:
bulk-otp-generated
: Triggered when multiple OTPs are generated- Payload:
{ count: number, otps: string[] }
- Payload:
Features:
- Automatic webhook triggering for all OTP generation methods
- Error handling for webhook failures
- Type-safe event handling
- Customizable webhook handler implementation
Test
npm run test
Learn More
For detailed documentation and implementation examples, read our comprehensive guide on A One Time Password (OTP) generator
License
This project is licensed under the MIT License - see the LICENSE file for details
If you find this utility library useful, you can buy me a coffee to keep me energized for creating libraries like this.
Tech Blog
Read my personal blog on various tech topics at Tech Insights: Personal Tech Blog