TheDocumentation Index
Fetch the complete documentation index at: https://docs.usemandate.io/llms.txt
Use this file to discover all available pages before exploring further.
@mandate/middleware package lets you gate any API endpoint behind a Mandate payment check. When an agent sends a request without a valid payment proof, the middleware returns a 402 Payment Required response with a structured challenge. When the agent presents a valid proof, the middleware verifies it with the Mandate API and calls your handler — you get the agent ID, mandate ID, and transaction ID to use in your own logging or business logic.
Install the middleware
Hono
UserequireKyaPayment as route-level middleware:
Express
UserequireKyaPaymentExpress as route middleware:
Middleware options
BothrequireKyaPayment and requireKyaPaymentExpress accept the same KyaPaymentOptions object:
| Option | Required | Default | Description |
|---|---|---|---|
price | Yes | — | Amount to charge per request, as a string (e.g. "0.10") |
currency | No | "USDC" | Payment currency |
category | No | "" | Payment category for mandate policy matching (e.g. "data", "compute") |
merchantDomain | No | Auto-detected from request | Your API’s domain; falls back to MANDATE_MERCHANT_DOMAIN env var |
kyaApiUrl | No | https://api.kya.dev | Mandate API URL; falls back to MANDATE_API_URL env var |
kyaApiKey | No | "" | Your Mandate API key; falls back to MANDATE_API_KEY env var |
How the 402 response works
When a request arrives without anX-Payment-Proof header, the middleware returns 402 Payment Required with a JSON body and an X-Payment-Challenge header containing a base64url-encoded challenge:
scheme is kya-sandbox when you use a sandbox API key (ky_sand_...) and kya-usdc for production keys. The Mandate SDK reads this challenge and handles the full payment flow automatically — your seller-side code does not need to do anything else.
Verification flow
For every request that includes anX-Payment-Proof header, the middleware:
- Decodes the base64url proof from the header.
- Calls
POST /v1/payments/verifyon the Mandate API with the proof, your merchant domain, and the expected amount and currency. - If verification fails, returns
402with{ "error": "payment_verification_failed" }. - If verification succeeds, sets the agent context on the request and calls
next().
The middleware calls
POST /v1/payments/verify on every verified request. Each successful verification records a transaction and consumes the proof’s nonce — the same proof cannot be reused.Environment variables
| Variable | Description |
|---|---|
MANDATE_API_KEY | Your Mandate API key (sandbox or production) |
MANDATE_API_URL | Optional; defaults to https://api.kya.dev |
MANDATE_MERCHANT_DOMAIN | Optional; your merchant domain, used when merchantDomain is not passed in options |

