New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

otp-number

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

otp-number

> 🔐 A lightweight and flexible JavaScript library to generate random OTPs (One-Time Passwords) for use in both frontend and backend applications.

latest
npmnpm
Version
1.0.4
Version published
Maintainers
1
Created
Source

otp-number

🔐 A lightweight and flexible JavaScript library to generate random OTPs (One-Time Passwords) for use in both frontend and backend applications.

✨ Features

  • ✅ Supports digits, uppercase, and lowercase characters
  • ✅ Configurable OTP length
  • ✅ Works in Node.js and browser (UMD compatible)
  • ✅ Zero dependencies
  • 🔢 Numeric-only OTP shortcut with otp_number_only(length)

📦 Installation

npm install otp-number

🔧 Usage

const { otp_number, otp_number_only } = require('otp-number');

// Generate a 6-digit OTP with digits, uppercase, and lowercase letters (default) const defaultOTP = otp_number(); console.log(defaultOTP); // Example: "aB3dE9"

// Generate an 8-digit numeric-only OTP using the helper function const numericOTP = otp_number_only(8); console.log(numericOTP); // Example: "12345678"

// Generate a 10-character OTP with only uppercase letters and digits const customOTP = otp_number(10, { digits: true, upperCase: true, lowerCase: false }); console.log(customOTP); // Example: "A1B2C3D4E5"

// Generate a 4-character OTP with only lowercase letters const lowercaseOTP = otp_number(4, { digits: false, upperCase: false, lowerCase: true }); console.log(lowercaseOTP); // Example: "wxyz"

// Attempting to generate an OTP with no character sets enabled will throw an error: try { otp_number(6, { digits: false, upperCase: false, lowerCase: false }); } catch (error) { console.error(error.message); // Output: "At least one character set must be enabled." }

Numeric Only Shortcut

const { otp_number_only } = require('otp-number');
console.log(otp_number_only(6)); // ➤ "920384"

## Browser (UMD)

<script src="dist/otp-generator.umd.js"></script>
<script>
  const otp = OtpGenerator.otp_number(6, { digits: true, upperCase: true });
  console.log("OTP:", otp);
</script>


## ⚙️ API
otp_number(length, options)

Parameter	Type	Default	Description
length	number	6	Length of the OTP
options.digits	boolean	true	Include digits (0-9)
options.upperCase	boolean	false	Include uppercase letters (A-Z)
options.lowerCase	boolean	false	Include lowercase letters (a-z)

## 📌 Example Outputs

otp_number();
// ➤ "729314"

otp_number(6, { digits: true, upperCase: true });
// ➤ "5J8K2Q"

otp_number(4, { lowerCase: true });
// ➤ "kfza"
otp_number_only(length = 6)

A helper function that generates a numeric-only OTP string of the specified length. This is equivalent to calling otp_number(length, { digits: true }).

## 📁 Build (for Browser Support)

Install dev dependencies:

bash
Copy
Edit
npm install --save-dev rollup @rollup/plugin-commonjs @rollup/plugin-node-resolve
Build UMD:

bash
Copy
Edit
npx rollup -c

## 🔎 Keywords

otp, otp-number, random otp, otp generator, javascript otp, nodejs otp, one-time password, verification code, auth code, frontend backend otp, browser otp, 2FA

Keywords

otp

FAQs

Package last updated on 24 Apr 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts