otplib
TypeScript-first library for HOTP and TOTP / Authenticator with multi-runtime and plugin support
Breaking Changes (v13)
[!IMPORTANT]
v13 is a complete rewrite with breaking changes:
- New
- Security-audited plugins — Default crypto uses
@noble/hashes and @scure/base, both independently audited
- Cross-platform defaults — Works out-of-the-box in Node.js, Bun, Deno, and browsers
- Full type safety — Comprehensive TypeScript types with strict mode from the ground up
- Async-first API — All operations are async by default; sync variants available for compatible plugins
- Removed
- Separate authenticator package — TOTP now covers all authenticator functionality
- Outdated plugins — Legacy crypto adapters removed in favor of modern, audited alternatives
See Migration Guide for details.
Features
- Zero Configuration - Works out of the box with sensible defaults
- RFC Compliant - RFC 6238 (TOTP) and RFC 4226 (HOTP)
- TypeScript-First - Full type definitions
- Google Authenticator Compatible - Full otpauth:// URI support
Installation
npm install otplib
pnpm add otplib
yarn add otplib
Quick Start
Functional API (Recommended)
import { generateSecret, generate, verify, generateURI } from "otplib";
const secret = generateSecret();
const token = await generate({ secret });
const isValid = await verify({ secret, token });
const uri = generateURI({
issuer: "MyService",
label: "user@example.com",
secret,
});
Class API
import { OTP } from "otplib";
const otp = new OTP();
const secret = otp.generateSecret();
const token = await otp.generate({ secret });
const isValid = await otp.verify({ secret, token });
const uri = otp.generateURI({
issuer: "MyService",
label: "user@example.com",
secret,
});
Documentation
Full documentation available at here:
License
MIT © 2026 Gerald Yeo