Skip to content

otplib API Documentation / otplib / class / OTP

Class: OTP

Defined in: packages/otplib/src/class.ts:287

OTP Class

A wrapper class that dynamically handles TOTP and HOTP strategies.

Examples

ts
import { OTP } from 'otplib';

// Create OTP instance with TOTP strategy (default)
const otp = new OTP({ strategy: 'totp' });

// Generate and verify
const secret = otp.generateSecret();
const token = await otp.generate({ secret });
const result = await otp.verify({ secret, token });

With HOTP strategy

ts
import { OTP } from 'otplib';

const otp = new OTP({ strategy: 'hotp' });
const token = await otp.generate({ secret: 'ABC123', counter: 0 });

Generating otpauth:// URI for authenticator apps

ts
import { OTP } from 'otplib';

const otp = new OTP({ strategy: 'totp' });
const uri = otp.generateURI({
  issuer: 'MyApp',
  label: 'user@example.com',
  secret: 'ABC123',
});

Constructors

Constructor

new OTP(options?): OTP

Defined in: packages/otplib/src/class.ts:293

Parameters

options?

OTPClassOptions = {}

Returns

OTP

Methods

generate()

generate(options): Promise<string>

Defined in: packages/otplib/src/class.ts:332

Generate an OTP token based on the configured strategy

Parameters

options

OTPGenerateOptions

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


generateSecret()

generateSecret(length?): string

Defined in: packages/otplib/src/class.ts:320

Generate a random secret key

Parameters

length?

number = 20

Number of random bytes (default: 20)

Returns

string

Base32-encoded secret key


generateSync()

generateSync(options): string

Defined in: packages/otplib/src/class.ts:350

Generate an OTP token based on the configured strategy synchronously

Parameters

options

OTPGenerateOptions

Generation options

Returns

string

OTP code

Throws

If the algorithm is invalid or unsupported by the plugin

Throws

If HMAC computation fails or the plugin does not support sync operations


generateURI()

generateURI(options): string

Defined in: packages/otplib/src/class.ts:405

Generate an otpauth:// URI for QR code generation

Supports both TOTP and HOTP strategies.

Parameters

options

OTPURIGenerateOptions

URI generation options

Returns

string

otpauth:// URI string

Throws

If a non-empty algorithm is not supported


getStrategy()

getStrategy(): OTPStrategy

Defined in: packages/otplib/src/class.ts:310

Get the current strategy

Returns

OTPStrategy


verify()

verify(options): Promise<VerifyResult>

Defined in: packages/otplib/src/class.ts:368

Verify an OTP token based on the configured strategy

Parameters

options

OTPVerifyOptions

Verification options

Returns

Promise<VerifyResult>

Verification result with validity and optional delta

Throws

If the algorithm is invalid or unsupported by the plugin

Throws

If HMAC computation fails in the crypto plugin


verifySync()

verifySync(options): VerifyResult

Defined in: packages/otplib/src/class.ts:386

Verify an OTP token based on the configured strategy synchronously

Parameters

options

OTPVerifyOptions

Verification options

Returns

VerifyResult

Verification result with validity and optional delta

Throws

If the algorithm is invalid or unsupported by the plugin

Throws

If HMAC computation fails or the plugin does not support sync operations

Released under the MIT License.