Skip to content

otplib API Documentation / otplib / generate

Function: generate()

generate(options): Promise<string>

Defined in: otplib/src/functional.ts:212

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

OTPFunctionalOptions

OTP generation options

Returns

Promise<string>

OTP code

Examples

ts
import { generate } from 'otplib';

const token = await generate({
  secret: 'JBSWY3DPEHPK3PXP',
});
// Returns: '123456'
ts
import { generate } from 'otplib';

const token = await generate({
  secret: 'JBSWY3DPEHPK3PXP',
  strategy: 'hotp',
  counter: 0,
});
ts
import { generate, NodeCryptoPlugin } from 'otplib';

const token = await generate({
  secret: 'JBSWY3DPEHPK3PXP',
  crypto: new NodeCryptoPlugin(),
});

Released under the MIT License.