Middleware
Key0 provides access token validation middleware for every supported framework, plus a standalone validator for backend services that do not run the full SDK. All middleware functions extract theBearer token from the Authorization header, verify it using the configured secret or public key, and attach the decoded payload to the request context. On failure they return a JSON error response and short-circuit the request.
Configuration Types
ValidateAccessTokenConfig
Used by the framework-specific middleware functions (validateAccessToken, honoValidateAccessToken, fastifyValidateAccessToken).
| Field | Type | Description |
|---|---|---|
secret | string | HS256 shared secret. Must match the secret used by the AccessTokenIssuer. |
ValidatorConfig
Used by the standalonevalidateKey0Token function. Supports both HS256 and RS256.
| Field | Type | Default | Description |
|---|---|---|---|
secret | string | — | Shared secret for HS256. Required when algorithm is "HS256". |
publicKey | string | — | PEM-encoded public key for RS256. Required when algorithm is "RS256". |
algorithm | "HS256" | "RS256" | "HS256" | Signing algorithm to expect. |
AccessTokenPayload
All middleware functions resolve to the same decoded JWT payload shape.| Claim | Type | Description |
|---|---|---|
sub | string | The requestId that initiated the payment flow. |
jti | string | The challengeId assigned by the challenge engine. |
resourceId | string | Identifier of the protected resource. |
planId | string | The plan the client paid for. |
txHash | string | On-chain transaction hash of the USDC payment. |
iat | number | Issued-at timestamp (seconds since epoch). |
exp | number | Expiration timestamp (seconds since epoch). |
Framework Middleware
- Express
- Hono
- Fastify
- Standalone
validateAccessToken
Express middleware. On success, attaches the decoded payload toreq.key0Token.req.key0Token (AccessTokenPayload)Internal: validateToken
The framework-agnostic function used byvalidateAccessToken, honoValidateAccessToken, and fastifyValidateAccessToken. You do not need to call this directly unless you are building a custom integration.
Key0Error with the following codes:
| Scenario | Error Code | HTTP Status |
|---|---|---|
Missing or malformed Authorization header | INVALID_REQUEST | 401 |
| Token signature expired | CHALLENGE_EXPIRED | 401 |
| Invalid signature or malformed token | INVALID_REQUEST | 401 |

