Documentation 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 lets you gate any HTTP endpoint behind a Mandate payment check without writing any payment logic yourself. Drop requireKyaPayment (Hono) or requireKyaPaymentExpress (Express) in front of your route handler and the middleware takes care of issuing 402 challenges, decoding incoming proofs, and verifying them against the Mandate API before your handler runs.
Installation
hono and express are optional peer dependencies — install whichever framework you use.
KyaPaymentOptions
Both middleware functions accept the sameKyaPaymentOptions interface.
Amount to charge per request, e.g.
"0.10".Payment currency. Defaults to
"USDC".Resource category used for mandate matching, e.g.
"data" or "reports".Your API’s merchant domain. Auto-detected from the incoming request URL when omitted. You can also set this via the
MANDATE_MERCHANT_DOMAIN environment variable.Mandate API base URL. Defaults to the
MANDATE_API_URL environment variable, then https://api.kya.dev.Mandate API key used to verify proofs. Defaults to the
MANDATE_API_KEY environment variable.requireKyaPayment — Hono
requireKyaPayment(options) returns a Hono middleware function. When a request arrives without an X-Payment-Proof header, the middleware responds with HTTP 402 and an X-Payment-Challenge header. When a valid proof is present and verified, it sets three context variables and calls next().
Context variables
Retrieve these in your handler withc.get():
| Variable | Type | Description |
|---|---|---|
kyaAgentId | string | The agent that submitted the proof. |
kyaMandateId | string | The mandate used to authorize payment. |
kyaTransactionId | string | Unique transaction ID for this payment. |
Hono example
requireKyaPaymentExpress — Express
requireKyaPaymentExpress(options) returns a standard Express (req, res, next) middleware function. On successful verification, it attaches a kya object to the request before calling next().
Request augmentation
The middleware attaches(req as any).kya with the following fields:
| Field | Type | Description |
|---|---|---|
agentId | string | The agent that submitted the proof. |
mandateId | string | The mandate used to authorize payment. |
transactionId | string | Unique transaction ID for this payment. |
Express example
402 response format
When a request arrives without anX-Payment-Proof header, the middleware responds with HTTP 402 and this body:
The
scheme and network fields in the challenge reflect your API key type. Sandbox keys (ky_sand_*) produce scheme: "kya-sandbox" and network: "sandbox". Production keys produce scheme: "kya-usdc" and network: "base".Verification flow
WhenX-Payment-Proof is present, both middleware functions follow the same steps:
Decode the proof header
Base64url-decodes the
X-Payment-Proof value and validates its shape. Returns HTTP 402 with error: "invalid_proof_header" if decoding fails.Verify with Mandate
Calls
POST /v1/payments/verify with the proof, merchant domain, expected amount, and expected currency.Handle verification failure
If
verified is false, responds with HTTP 402 and error: "payment_verification_failed" plus the server’s reason field.Environment variables
| Variable | Description |
|---|---|
MANDATE_API_KEY | Mandate API key used to verify proofs. Required if not passed via options. |
MANDATE_API_URL | Mandate API base URL. Defaults to https://api.kya.dev. |
MANDATE_MERCHANT_DOMAIN | Your API’s merchant domain. Used when merchantDomain is not set in options. |

