AccessTokenIssuer
AccessTokenIssuer handles JWT creation and verification for the Key0 payment flow. After a client completes an on-chain USDC payment, the engine uses AccessTokenIssuer to mint a signed JWT that grants access to the purchased resource.
It supports two signing algorithms:
- HS256 — symmetric shared secret (default). Suitable for single-service deployments.
- RS256 — asymmetric RSA key pair. Suitable for distributed systems where multiple services verify tokens using the public key.
Constructor
Parameters
Either a plain string (interpreted as an HS256 shared secret) or a configuration object.
AccessTokenIssuerConfig
| Property | Type | Required | Description |
|---|---|---|---|
secret | string | When using HS256 | Shared secret. Must be at least 32 characters. |
privateKey | string | When using RS256 | RSA private key in PEM (PKCS#8) format. |
algorithm | "HS256" | "RS256" | No | Signing algorithm. Defaults to "HS256". |
Validation
The constructor throws immediately if:- An HS256 secret is shorter than 32 characters.
- RS256 is selected but no
privateKeyis provided. - HS256 is selected (or defaulted) but no
secretis provided.
Methods
sign
Signs a JWT containing the provided claims.The claims to embed in the JWT payload. See TokenClaims below.
Token time-to-live in seconds. The
exp claim is set to iat + ttlSeconds.The signed JWT string.
Promise<TokenResult> — an object with a single token property.
verify
Verifies a JWT signed with HS256 and returns the decoded payload.The JWT string to verify.
iat and exp claims.
verifyWithFallback
Attempts verification with the primary secret first, then iterates through fallback secrets. Designed for zero-downtime secret rotation.The JWT string to verify.
An ordered list of previous secrets to try if the primary secret fails.
"Token verification failed with all secrets" if the primary and all fallback secrets fail.
Types
TokenClaims
Claims embedded in every Key0 access token.| Claim | Type | Description |
|---|---|---|
sub | string | The requestId that initiated the payment flow. |
jti | string | The challengeId (used for replay prevention). |
resourceId | string | Identifier of the purchased resource. |
planId | string | Identifier of the purchased plan. |
txHash | string | On-chain USDC transaction hash that funded the access. |
iat (issued at) and exp (expiration) are set automatically by sign and included in the return type of verify and verifyWithFallback.

