authik
v1.0.0
Published
Lightweight OTP authentication using Fast2SMS (no external backend needed)
Maintainers
Readme
Authik 🔐
A simple, zero-dependency OTP authentication module that uses Fast2SMS API to send and verify OTPs.
🚀 Installation
npm install authik✨ Usage
const { sendOTP, verifyOTP } = require("authik");
const phone = "+91XXXXXXXXXX";
const apiKey = "YOUR_PUBLIC_FAST2SMS_API_KEY"; // keep public if for demo
// Send OTP
sendOTP(phone, apiKey).then(console.log);
// Verify OTP
const result = verifyOTP(phone, "123456");
console.log(result);Note: This uses an in-memory store (non-persistent). Don't use for production as-is. For production, use Redis or a DB-backed store.
📦 Features
- Generate 6-digit OTPs
- Send SMS via Fast2SMS
- Verify OTPs with expiry logic
- Works with Express or standalone
🔑 API Key Usage
If this is for open demos or dev playgrounds, you can use a public test key in your examples. But don't hardcode it inside the library — keep it as a parameter like this:
sendOTP(phone, "your-public-api-key")📖 API Reference
sendOTP(phone: string, apiKey: string): Promise<object>
Sends an OTP to the specified phone number using Fast2SMS.
Parameters:
phone(string): Phone number with country code (e.g., "+911234567890")apiKey(string): Your Fast2SMS API key
Returns: Promise that resolves to the SMS API response
verifyOTP(phone: string, otp: string): object
Verifies the OTP for the given phone number.
Parameters:
phone(string): Phone number used during OTP generationotp(string): The OTP entered by the user
Returns: Object with success (boolean) and message (string)
🧠 Complete Example
const { sendOTP, verifyOTP } = require("authik");
const phone = "+911234567890";
const apiKey = "your-fast2sms-api-key";
async function handleOTPFlow() {
try {
// Send OTP
const response = await sendOTP(phone, apiKey);
console.log("OTP sent:", response);
// Later, when user enters OTP
const userEnteredOTP = "123456"; // from user input
const result = verifyOTP(phone, userEnteredOTP);
if (result.success) {
console.log("✅ OTP verified successfully!");
// Proceed with authentication
} else {
console.log("❌ OTP verification failed:", result.message);
}
} catch (error) {
console.error("Error:", error);
}
}
handleOTPFlow();⚡ Express.js Integration
const express = require("express");
const { sendOTP, verifyOTP } = require("authik");
const app = express();
app.use(express.json());
const API_KEY = "your-fast2sms-api-key";
app.post("/send-otp", async (req, res) => {
const { phone } = req.body;
try {
const result = await sendOTP(phone, API_KEY);
res.json({ success: true, data: result });
} catch (error) {
res.status(500).json({ success: false, error: error.message });
}
});
app.post("/verify-otp", (req, res) => {
const { phone, otp } = req.body;
const result = verifyOTP(phone, otp);
res.json(result);
});
app.listen(3000, () => {
console.log("Server running on port 3000");
});🛡️ Security Notes
- In-memory storage: OTPs are stored in memory and will be lost on server restart
- Production usage: For production apps, integrate with Redis or a database
- Rate limiting: Implement rate limiting to prevent OTP abuse
- API key security: Keep your Fast2SMS API key secure and use environment variables
- One-time use: OTPs are automatically deleted after verification or expiry
🔧 Configuration
Default OTP settings:
- Length: 6 digits
- Expiry: 5 minutes
- Storage: In-memory (non-persistent)
📄 License
MIT © 2025 Om Bhayde
✨ Contributing
Pull requests, ideas, and suggestions are welcome! Feel free to open an issue or contribute to the project.
💬 Support
If you use Authik in your project, let me know — I'd love to feature it!
🪄 Pro Tips
- Use environment variables for API keys:
process.env.FAST2SMS_API_KEY - Implement proper error handling for SMS delivery failures
- Add logging for security monitoring and debugging
- Consider implementing OTP retry mechanisms with exponential backoff
