Integrate
x402 payment flow
The protocol#
| Header | Direction | Contents (base64-encoded JSON) |
|---|---|---|
PAYMENT-REQUIRED | Server → client, with HTTP 402 | x402Version, the resource, and accepts: one or more payment requirements. |
PAYMENT-SIGNATURE | Client → server, on the retry | The chosen requirement and the signed EIP-712 authorization. |
PAYMENT-RESPONSE | Server → client, with the paid response | The settlement result: success, transaction, network, payer. |
Version 1 servers use X-PAYMENT and X-PAYMENT-RESPONSE and put the requirements in the JSON body; CortexPay reads both versions. The only scheme is exact: pay exactly amount base units of asset to payTo on network (a CAIP-2 id such as eip155:999).
The flow, step by step#
- The client requests the resource without paying. The server answers
402withPAYMENT-REQUIRED. Decoded, from this deployment's sandbox:PAYMENT-REQUIRED, decoded{ "x402Version": 2, "resource": { "url": "https://cortexpay.vercel.app/x402/sandbox/sandbox-market-data", "description": "Mid prices for major perpetual markets.", "mimeType": "application/json", "serviceName": "Market Data" }, "accepts": [ { "scheme": "exact", "network": "eip155:31337", "asset": "0x5A4d00000000000000000000000000000000c0DE", "amount": "20000", "payTo": "0xC0De5a4d00000000000000000000000000000001", "maxTimeoutSeconds": 60, "extra": { "name": "Sandbox USDC", "version": "2" } } ], "error": "PAYMENT-SIGNATURE header is required" } - CortexPay picks one requirement, quotes it and runs the agent's ten policy checks. Nothing is signed if a check fails.
- An EIP-712 authorization is signed for exactly that amount, recipient and time window: an EIP-3009
TransferWithAuthorization, or a Permit2PermitWitnessTransferFromwhose spender is the x402 Permit2 proxy. In the sandbox a sandbox signer signs it. In a live workspace the payment first waits for the owner, who approves it and signs it in their own wallet; CortexPay holds no key. - The request is repeated with
PAYMENT-SIGNATURE. The server has a facilitator verify and settle the transfer, then returns the response withPAYMENT-RESPONSE. - CortexPay records the settlement. A live payment counts as delivered only after CortexPay finds the token transfer on chain and checks that the transaction spent this payment's authorization (its nonce), so one transfer can never stand in for another payment.
With authorize and execute: false, CortexPay stops after step 3 and returns the signed header for your agent to send itself.
Networks and assets#
| Network | CAIP-2 | Asset | Contract | EIP-712 domain | Transfer methods |
|---|---|---|---|---|---|
| CortexPay Sandbox | eip155:31337 | sUSDC | 0x5A4d00000000000000000000000000000000c0DE | Sandbox USDC, version 2 | eip3009 |
| Elysium | eip155:99901 | USDC | 0xb47c4C8F73F009CC0C0d3f92B388d13E293F0B65 | USDC, version 1 | permit2 |
| HyperEVM | eip155:999 | USDC | 0xb88339CB7199b77E23DB6E890353E22632Ba630f | USDC, version 2 | eip3009, permit2 |
- HyperEVM's native USDC supports both methods; EIP-3009 needs no prior approval, so it is preferred. It is the x402 default asset there.
- Elysium's USDC (bridged from HyperEVM) has no EIP-3009, so payments use Permit2. The payer must have approved Permit2 for USDC once.
- The public testnets of both chains have no verified USDC, so stablecoin payments are not available on them.
Permit2 contracts
| Contract | Address |
|---|---|
| Permit2 | 0x000000000022D473030F116dDEE9F6B43aC78BA3 |
| x402 exact Permit2 proxy | 0x402085c248EeA27D92E8b30b2C58ed07f9E20001 |
Which option CortexPay picks#
When a server offers several requirements, CortexPay keeps those it can pay and the agent's policy allows (network and asset), then takes:
- the cheapest amount;
- on a tie, EIP-3009 over Permit2;
- on a further tie, the server's own order.
Every option and why it was or was not payable is returned in quote.options.
CortexPay's facilitator#
A facilitator verifies a signed x402 payment and submits the transfer on chain. No public facilitator lists HyperEVM (eip155:999) or Elysium (eip155:99901), so CortexPay runs one, built on the official @x402/evm exact scheme. A provider selling on these chains points its x402 middleware at it:
import { HTTPFacilitatorClient } from "@x402/core/server";
// Settles x402 "exact" payments on HyperEVM (eip155:999) and Elysium (eip155:99901).
const facilitator = new HTTPFacilitatorClient({
url: "https://cortexpay.vercel.app/api/x402/facilitator",
createAuthHeaders: async () => {
const auth = { Authorization: `Bearer ${process.env.CORTEXPAY_API_KEY}` }; // live key, services:write
return { verify: auth, settle: auth, supported: {} };
},
});
// Pass it to your x402 resource server middleware, or call it yourself:
const check = await facilitator.verify(paymentPayload, paymentRequirements);
if (check.isValid) {
const settled = await facilitator.settle(paymentPayload, paymentRequirements);
// settled.transaction is the on-chain hash; return it in PAYMENT-RESPONSE
}GET /api/x402/facilitator/supportedis public. It lists a network only while the facilitator holds enough HYPE there to pay settlement gas, so it is the live status: an empty list means it cannot settle right now.verifyandsettleneed an API key from a live workspace with theservices:writescope, sent asAuthorization: Bearer cpk_live_…. Sandbox keys and browser sessions are refused.- The exact scheme with verified USDC only: on HyperEVM by EIP-3009 or Permit2, on Elysium by Permit2 (the payer approves Permit2 once; the x402 Permit2 proxy is the spender). The payment must be signed for the same network, asset, amount and
payToas the requirements. - The facilitator pays gas only. The tokens move from the payer straight to
payTo, exactly as signed; CortexPay never holds them. - Settling waits for the receipt (up to 30 seconds). Sending the same signed payment again returns the same transaction; it is never paid twice.
- Limits: 240 verifies and 120 settlements per minute per key, 1,000 settlements a day across the deployment and 250 per workspace.
Tested end to end on local forks of both mainnets with the official x402 client: EIP-3009 on HyperEVM, Permit2 on Elysium, replayed payloads, wrong signers and a refused settlement.
Sandbox resources#
Sandbox services are real x402 endpoints at /x402/sandbox/:slug on this deployment. They answer 402 with a genuine challenge for simulated sUSDC on eip155:31337 (EIP-3009), check the EIP-712 signature on the retry, serve the response and settle on the simulated ledger with a sim_ reference. Nothing is broadcast.
curl -i "https://cortexpay.vercel.app/x402/sandbox/sandbox-market-data"- GET sends query parameters as input; POST takes a JSON object up to 16 KB.
- CORS is open, and the payment headers are exposed to browsers.
- 120 requests per minute per IP address.
- The sandbox facilitator settles only payments a CortexPay spending policy authorized, so a signature made outside CortexPay is refused.
Endpoints#
- GET
/api/x402/facilitator/supportedx402 v2 discovery: the exact-scheme networks this facilitator can settle right now (only those where it holds gas).
Public
- POST
/api/x402/facilitator/verifyx402 v2 verify { x402Version, paymentPayload, paymentRequirements } → { isValid, invalidReason?, payer? }. Nothing is broadcast.
Key: services:write · 240/min · API key from a live workspace only (sessions and sandbox keys are refused).
- POST
/api/x402/facilitator/settlex402 v2 settle: re-verifies, simulates, broadcasts and waits for the receipt → { success, transaction, network, payer }. CortexPay pays gas; the tokens move from the payer to payTo.
Key: services:write · 120/min · API key from a live workspace only. Daily settlement caps apply per deployment and per workspace.
- GET
/x402/sandbox/:slugA sandbox x402 resource: 402 with PAYMENT-REQUIRED, or the paid response with PAYMENT-RESPONSE.
Public · 120/min per IP
- POST
/x402/sandbox/:slugSame resource with a JSON body (≤ 16 KB).
Public · 120/min per IP