Skip to content

otplib API Documentation / otplib / functional / generateURI

Function: generateURI() ​

generateURI(options): string

Defined in: packages/otplib/src/functional.ts:142

Generate an otpauth:// URI for QR code generation

This URI can be used to generate a QR code that can be scanned by Google Authenticator and other authenticator apps.

Parameters ​

options ​

URI generation options

algorithm? ​

HashAlgorithm

counter? ​

number

digits? ​

number

issuer ​

string

label ​

string

period? ​

number

secret ​

string

Base32-encoded secret key

Note: By default, strings are assumed to be Base32 encoded. If you have a raw string/passphrase, you must convert it to Uint8Array first.

strategy? ​

OTPStrategy

OTP strategy to use (default: 'totp')

Returns ​

string

otpauth:// URI string

Throws ​

If a non-empty algorithm is not supported

Examples ​

TOTP

ts
import { generateURI } from 'otplib';

const uri = generateURI({
  issuer: 'ACME Co',
  label: 'john@example.com',
  secret: 'JBSWY3DPEHPK3PXP',
});
// Returns: 'otpauth://totp/ACME%20Co:john%40example.com?secret=...'

HOTP

ts
import { generateURI } from 'otplib';

const uri = generateURI({
  strategy: 'hotp',
  issuer: 'ACME Co',
  label: 'john@example.com',
  secret: 'JBSWY3DPEHPK3PXP',
  counter: 5,
});
// Returns: 'otpauth://hotp/ACME%20Co:john%40example.com?secret=...&counter=5'

Released under the MIT License.