We are pleased to announce the release of @onedaydevelopers/otp-detector, a robust, zero-dependency JavaScript utility designed to address the challenge of extracting One Time Passwords (OTP) from unstructured text.
Whether building a React Native application that parses SMS messages, a web dashboard for email processing, or a Node.js microservice, accurately identifying a 4-8 digit code within complex content is critical.
Live Demo
Experience the library's capabilities directly in your browser:
Try the OTP Detector Live Demo
The Problem
Parsing OTPs from real-world messages is often deceptively complex. Consider the following example:
"Your verification code for 2025 is 829103. Call 555-0199 for help."
A standard regular expression might incorrectly identify 2025 (the year) or 5550199 (the phone number) as the OTP.
OTP Detector employs context-aware heuristics to correctly interpret the message structure, identifying 829103 as the intended verification code.
Key Features
- Context-Aware: Distinguishes valid OTPs from years, dates, phone numbers, and currency.
- Format Flexible: Supports codes with dashes (
123-456), spaces (123 456), and variable lengths (4-8 digits). - Format Agnostic: Processes raw text strings from SMS, Emails, or HTML sources.
- Zero Dependencies: Optimized for performance and minimal bundle size.
- Universal Compatibility: Compatible with Node.js, React, React Native, and standard HTML/JS environments.
Installation
Install the package via npm:
npm install @onedaydevelopers/otp-detector
Usage Examples
Basic Extraction
import { extractOTP } from '@onedaydevelopers/otp-detector';
const text = "Your login code is 582491. Valid for 10 minutes.";
const otp = extractOTP(text);
console.log(otp); // "582491"
Email Processing
For applications processing incoming emails (e.g., using mailparser), the library provides a specialized helper:
import { extractOTPFromEmail } from '@onedaydevelopers/otp-detector';
const email = {
subject: "Verification Required",
text: "Hello user, use 902100 to verify."
};
const otp = extractOTPFromEmail(email);
console.log(otp); // "902100"
HTML Content Handling
The library automatically strips HTML tags to analyze the underlying text content:
import { extractOTP } from '@onedaydevelopers/otp-detector';
const html = `
<div>
<h1>Welcome!</h1>
<p>Your secret code is <b>4821</b></p>
</div>
`;
const otp = extractOTP(html);
console.log(otp); // "4821"
Contributing
This project is an open-source initiative by One Day Developers. We welcome community feedback, issue reports, and pull requests.
