Observed since 20 Oct, 19:13 UTC
No superseded metadata states collected. Unchanged observations do not create new revisions.
A TypeScript SDK for sending and validating OTPs with support for rate limiting.
An sdk for 2Factor.in SMS OTP service with support for rate limiting and built-in validation using Zod.
npm install 2factor-sdk
import { TWOFactor } from '2factor-sdk';
const twofactor = TWOFactor.init({
apiKey: 'YOUR_API_KEY',
// only when you want to use rate limiting.
upstashToken: 'YOUR_UPSTASH_TOKEN', // Optional: for rate limiting
upstashUrl: 'YOUR_UPSTASH_URL' // Optional: for rate limiting
});
// Send OTP
const result = await twofactor.sendOTP({
phoneNumber: '9XXXXXXXXX',
templateName: 'Template_Name', //two factor template name (optional)
interval: '30 s', // Optional: rate limiting interval (default: '30 s')
limit: 1 // Optional: rate limiting count (default: 1)
});
If any of the validation fails it will throw an error. Wrap the code in try catch to handle the errors. use handleTFError function to get errors that can be sent to clients.
const response = await twofactor.sendOTP({
phoneNumber: '9876543210', // Valid format
templateName: 'Template_Name', // two factor template name (optional)
});
const verify = await twofactor.verifyByUID({
otp: '123456', // Valid 6-digit OTP
UID: 'session-uid',
});
This SDK supports rate limiting using Upstash Redis. To enable rate limiting, provide your Upstash token and URL when initializing the SDK. If these are not provided, rate limiting will not be applied.
To create an Upstash account and get your token and URL, follow these steps:
UPSTASH_REDIS_REST_URL and UPSTASH_REDIS_REST_TOKEN.You can customize the rate limiting for each method call by providing interval and limit parameters. The default is 1 request per 30 seconds.
const result = await twofactor.sendOTP({
phoneNumber: '9XXXXXXXXX',
templateName: 'Template_Name',
interval: '1 m', // 1 minute
limit: 5 // 5 requests allowed per minute
});
This sdk has proper support for error handling and will return sanitized errors that we can directly sent to client without exposing other errors.
import { handleTFError, TWOFactor } from '2factor-sdk'
try {
const response = await client.sendOTP({
phoneNumber: '9XXXXXXXXX',
templateName: 'Template_Name'
});
} catch (error) {
const handledError = handleTFError(error);
if (handledError) {
// return sanitized error to client
}
}
Generates and sends a random OTP to the specified phone number. Supports both 4-digit and 6-digit OTPs.
const response = await twofactor.sendOTP({
phoneNumber: "9XXXXXXXXX", // Required: 10-digit number
// Optional fields
templateName: "OTP1", // Optional: SMS template
otpLength: 4, // Optional: 4 or 6 (default: 4)
interval: "30 s", // Optional: rate limiting interval (default: '30 s')
limit: 1 // Optional: rate limiting count (default: 1)
});
Returns: { UID: string } - Session UID for verification
Similar to sendOTP but also returns the generated OTP value. Useful for testing or scenarios where you need to know the OTP.
const response = await client.sendAndReturnOTP({
phoneNumber: "9XXXXXXXXX", // Required: 10-digit number
// optional fields
templateName: "OTP1", // Optional: SMS template
interval: "30 s", // Optional: rate limiting interval (default: '30 s')
limit: 1 // Optional: rate limiting count (default: 1)
});
Returns: { OTP: string, UID: string } - Both OTP and session UID
Sends a custom OTP value instead of generating a random one. Useful when you want to control the OTP value.
const response = await twofactor.sendCustomOTP({
phoneNumber: "9XXXXXXXXX", // Required: 10-digit number
// optional fields
templateName: "OTP1", // Optional: SMS template
otp: "1234", // Required: 4 or 6 digit numeric OTP
interval: "30 s", // Optional: rate limiting interval (default: '30 s')
limit: 1 // Optional: rate limiting count (default: 1)
});
Returns: { UID: string } - Session UID for verification
Verifies an OTP using the session UID. Most secure method as it validates against a specific session.
const isValid = await client.verifyByUID({
otp: '1234', // Required: 4-6 digit OTP
UID: '47dac35a-c9f9-11ef-8b17-0200cd936042', // Required: 36-char session ID
});
if (!isValid)
{
// throw error
}
Returns: boolean - true if OTP is valid. Undefined if not valid.
Verifies an OTP using the phone number. Alternative verification method when UID is not available.
const isValid = await client.verifyByPhone({
otp: string, // Required: 4-6 digit OTP
phoneNumber: string, // Required: 10-digit number
});
if (!isValid)
{
// throw error
}
Returns: boolean - true if OTP is valid. Undefined if not valid.
import express from 'express';
import dotenv from 'dotenv';
import { TWOFactor, handleTFError } from '2factor-sdk';
// Load environment variables
dotenv.config();
const app = express();
const port = process.env.PORT || 3000;
// Initialize TWOFactor client
const tf = TWOFactor.init({
apiKey: process.env.TWOFACTOR_API_KEY!,
upstashToken: process.env.UPSTASH_TOKEN,
upstashUrl: process.env.UPSTASH_URL,
});
// Middleware to parse JSON bodies
app.use(express.json());
// OTP sending endpoint
app.post('/send-otp', async (req, res) => {
const { phoneNumber } = req.body;
try {
// Send OTP
const result = await tf.sendOTP({
phoneNumber: phoneNumber,
limit: 1,
interval: '30 s'
});
return res.json({
success: true,
data: result
});
} catch (error) {
// Handle TWOFactor specific errors including validation errors.
// can be send to client.
const handledError = handleTFError(error);
if (handledError) {
return res.status(400).json({
success: false,
error: handledError
});
}
// Handle unexpected errors
console.error('Unexpected error:', error);
return res.status(500).json({
success: false,
error: 'Internal server error'
});
}
});
// Start the server
app.listen(port, () => {
console.log(`OTP API server running on port ${port}`);
});
MIT
For support, email fateennisar121@gmail.com or create an issue in the GitHub repository.
Download counts and popularity will appear here as the collector receives them.
This describes our collected data; it is not a security assessment.
Observed since 20 Oct, 19:13 UTC
No superseded metadata states collected. Unchanged observations do not create new revisions.
package:b6a5bfffa9d83b24aa8d9df249ebdbce8f67b28ddb338738b2bcbeca25ca68d4