@onedaydevelopers/otp-detector
v1.1.0
Published
A robust, dependency-free OTP (one-time password) detection utility using context-aware heuristics.
Maintainers
Readme
@onedaydevelopers/otp-detector
A robust, dependency-free OTP (one-time password) detection utility using context-aware heuristics.
It intelligently extracts 4-8 digit codes from emails (Subject, Body, HTML) and SMS messages, handling various formats (dashed, spaced) and filtering out false positives like addresses, invoice numbers, and dates.
Try it Live
Check out the playground and test your own messages here: http://od2.in/contributions/otp-detector
Key Features
- Context-Aware: Detects OTPs even in complex sentences (e.g. "Your verification code is 123456").
- Smart Filtering: Ignores numbers in addresses (e.g. "1601 Willow Rd"), dates, phone numbers, and invoice IDs.
- Flexible Formats: Supports standard (
123456), dashed (123-456), and spaced (123 456) codes. - Transaction Support: Correctly identifies OTPs in transaction messages (e.g. "Use OTP 9999 to authorize payment") while ignoring pure invoice details.
- Zero Dependencies: Lightweight and fast.
Installation
npm install @onedaydevelopers/otp-detectorUsage
CommonJS
const { extractOTPFromEmail, extractOTP } = require('@onedaydevelopers/otp-detector');
// 1. From an Email Object (Subject + Body + HTML)
const result = extractOTPFromEmail({
subject: 'Login Verification',
text: 'Your code is 123-456',
html: '<div>Your code is <strong>123-456</strong></div>'
});
console.log(result); // '123456'
// 2. From Plain Text / SMS
const smsCode = extractOTP('Your OTP is 987 654. Do not share.');
console.log(smsCode); // '987654'ES Modules
import { extractOTPFromEmail, extractOTP } from '@onedaydevelopers/otp-detector';
const code = extractOTP('Your verification code is 424242');API
extractOTP(text, options?)
Extracts an OTP from a plain text string.
Parameters
text(string): The input string to analyze.options(object, optional): Configuration object to customize detection.positiveKeywords(string[]): Words that boost detection confidence (e.g.,'otp','code'). Default includes common terms like 'verification', 'pin', etc.negativeKeywords(string[]): Words that suppress detection (e.g.,'order','invoice'). Default includes address terms, 'tracking', etc.neighborhood(number): Number of characters around a potential match to scan for context. Default is80.
Returns
- (string | null): The detected 4-8 digit OTP, or
nullif none found.
extractOTPFromEmail(emailData, options?)
Specialized extractor for emails that prioritizes the Subject line, then Plain Text, then HTML content.
Parameters
emailData(object):subject(string?): The email subject line.text(string?): The plain text body of the email.html(string?): The HTML body of the email.
options(object, optional): Same configuration object asextractOTP.
Returns
- (string | null): The detected OTP, or
null.
Looking for more useful tools? Visit us on For more tools, check us out on One Day Developers
