Skip to content

otplib API Documentation / @otplib/uri / generate

Function: generate() ​

generate(uri): string

Defined in: packages/uri/src/generate.ts:121

Generate an otpauth:// URI

Parameters ​

uri ​

OTPAuthURI

The URI components

Returns ​

string

The otpauth:// URI string

Throws ​

If a non-empty algorithm is not supported

Throws ​

If type is not "hotp"/"totp", or if digits, counter, or period are present but are not a safe integer (or a numeric string of one) in range - digits must be >= 1, counter must be >= 0, and period must be >= 1

Example ​

ts
import { generate } from '@otplib/uri';
import { base32 } from '@otplib/plugin-base32-scure';

const secret = base32.encode(new Uint8Array([1, 2, 3, 4, 5]));

const uri = generate({
  type: 'totp',
  label: 'ACME:john@example.com',
  params: {
    secret,
    issuer: 'ACME',
    algorithm: 'sha1',
    digits: 6,
  },
});
// Returns: 'otpauth://totp/ACME:john%40example.com?secret=...'

Released under the MIT License.