Skip to content

OTP

The /otp endpoint generates and verifies one-time passwords (TOTP/HOTP), compatible with Google Authenticator and Aegis.

Parameters

The request body must be JSON:

ParameterRequiredDescription
actionYesAction to perform: secret, generate, or verify
secretDependsBase32 secret key (required for generate and verify)
codeDependsOTP code to verify (required for verify)
labelNoAccount label for the otpauth:// URI (used with secret action)
issuerNoIssuer name for the otpauth:// URI (used with secret action)
algorithmNoHMAC algorithm: sha1 (default), sha256, or sha512
digitsNoCode length: 6 (default) or 8
periodNoTOTP period in seconds: 15, 30 (default), or 60
counterNoHOTP counter (integer). If provided, HOTP mode is used instead of TOTP

Available Actions

ActionDescription
secretGenerates a new random base32 secret and returns an otpauth:// URI ready for QR code scanning
generateGenerates the current OTP code. Without counter: TOTP (time-based). With counter: HOTP (deterministic)
verifyVerifies a code with a ±1 period tolerance window. Returns valid and a drift offset (0, +1, or -1)

RFC Compliance

Implements RFC 4226 (HOTP) and RFC 6238 (TOTP). Compatible with Google Authenticator and Aegis.

Code Examples

curl -X POST \
  -H "Content-Type: application/json" \
  -d '{
    "action": "secret",
    "label": "alice",
    "issuer": "Demo"
  }' \
  "https://api.sylvain.sh/v5/otp"

Response Fields

secret action:

FieldTypeDescription
secretstringBase32-encoded secret key
uristringotpauth:// URI for QR code generation

generate action:

FieldTypeDescription
codestringGenerated OTP code
typestringtotp or hotp

verify action:

FieldTypeDescription
validbooleanWhether the code is valid
driftnumberTime drift detected (0, +1, or -1)

Try It

Error Handling

If parameters are missing or invalid, the API will return an error:

Error MessageDescription
Action is requiredThe action parameter is missing
Action must be one of: secret, generate, verifyThe action value is not valid
Secret is requiredThe secret parameter is missing
Code is requiredThe code parameter is missing for verify
Digits must be 6 or 8The digits value is not valid
Period must be one of: 15, 30, 60The period value is not valid
Algorithm must be one of: sha1, sha256, sha512The algorithm value is not supported
Counter must be a non-negative integerThe counter value is invalid