otplib API Documentation / otplib / functional / generate
Function: generate()
generate(
options):Promise<string>
Defined in: packages/otplib/src/functional.ts:221
Generate an OTP code
Generates a one-time password based on the specified strategy.
- 'totp': Time-based OTP (default)
- 'hotp': HMAC-based OTP
Parameters
options
OTP generation options
Returns
Promise<string>
OTP code
Throws
If the algorithm is invalid or unsupported by the plugin
Throws
If HMAC computation fails in the crypto plugin
Examples
TOTP
ts
import { generate } from 'otplib';
const token = await generate({
secret: 'JBSWY3DPEHPK3PXP',
});
// Returns: '123456'HOTP
ts
import { generate } from 'otplib';
const token = await generate({
secret: 'JBSWY3DPEHPK3PXP',
strategy: 'hotp',
counter: 0,
});With custom plugins
ts
import { generate, NodeCryptoPlugin } from 'otplib';
const token = await generate({
secret: 'JBSWY3DPEHPK3PXP',
crypto: new NodeCryptoPlugin(),
});