API Reference
otplib
One-time password library implementing TOTP and HOTP with secret generation, token verification, and otpauth URI helpers.
Official Documentation ↗CompatibilityThis documentation covers version
v12. Check the official docs for the latest API surface.Installation
npm
npm install otplibpnpm
pnpm add otplibMethods
No methods match this filter.
Overview
otplib implements TOTP and HOTP one-time passwords with secret generation, token verification, and otpauth enrollment URIs. This reference lists the most frequently used APIs with accurate signatures and runnable snippets.
Core APIs
authenticator.generate— Produces the current time-based one-time password for a base32 secret.authenticator.check— Verifies a submitted token against the secret within the configured window.authenticator.keyuri— Builds an otpauth:// URI for enrollment in authenticator apps.
Example
const { authenticator } = require('otplib');
const secret = authenticator.generateSecret();
const token = authenticator.generate(secret);
console.log(token);
Notes
Secrets are equivalent to passwords and must be encrypted at rest. A small window accommodates clock drift but widens the acceptance range.
Verification
Generate a token and immediately verify it with check, then adjust the window and confirm an adjacent time-step token is also accepted.
FAQ
- How do I persist enrollment secrets securely?
- Store the base32 secret encrypted at rest and never log it; treat it like a password and show it only once during setup.
- How do I allow for clock drift?
- Set authenticator.options.window to 1 so tokens from the previous and next time step are also accepted.
- What is the difference between TOTP and HOTP?
- TOTP derives the code from the current time, while HOTP derives it from an incrementing counter you must store and resync per user.
Report a problem with this API docOpens a pre-filled issue on GitHub.