Integrate

x402 payment flow

x402 turns HTTP 402 Payment Required into a working payment: the server states its price in a header, the client answers with a signed stablecoin transfer, and the server delivers. CortexPay speaks x402 v2 with the exact scheme on behalf of your agents.
Code samples in

The protocol#

HeaderDirectionContents (base64-encoded JSON)
PAYMENT-REQUIREDServer → client, with HTTP 402x402Version, the resource, and accepts: one or more payment requirements.
PAYMENT-SIGNATUREClient → server, on the retryThe chosen requirement and the signed EIP-712 authorization.
PAYMENT-RESPONSEServer → client, with the paid responseThe 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#

  1. The client requests the resource without paying. The server answers 402 with PAYMENT-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"
    }
  2. CortexPay picks one requirement, quotes it and runs the agent's ten policy checks. Nothing is signed if a check fails.
  3. An EIP-712 authorization is signed for exactly that amount, recipient and time window: an EIP-3009 TransferWithAuthorization, or a Permit2 PermitWitnessTransferFrom whose 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.
  4. The request is repeated with PAYMENT-SIGNATURE. The server has a facilitator verify and settle the transfer, then returns the response with PAYMENT-RESPONSE.
  5. 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#

NetworkCAIP-2AssetContractEIP-712 domainTransfer methods
CortexPay Sandboxeip155:31337sUSDC0x5A4d00000000000000000000000000000000c0DESandbox USDC, version 2eip3009
Elysiumeip155:99901USDC0xb47c4C8F73F009CC0C0d3f92B388d13E293F0B65USDC, version 1permit2
HyperEVMeip155:999USDC0xb88339CB7199b77E23DB6E890353E22632Ba630fUSDC, version 2eip3009, 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

ContractAddress
Permit20x000000000022D473030F116dDEE9F6B43aC78BA3
x402 exact Permit2 proxy0x402085c248EeA27D92E8b30b2C58ed07f9E20001

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:

  1. the cheapest amount;
  2. on a tie, EIP-3009 over Permit2;
  3. 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:

Provider side (x402 v2, official client)
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/supported is 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.
  • verify and settle need an API key from a live workspace with the services:write scope, sent as Authorization: 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 payTo as 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.

See a real 402
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/supported

    x402 v2 discovery: the exact-scheme networks this facilitator can settle right now (only those where it holds gas).

    Public

  • POST/api/x402/facilitator/verify

    x402 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/settle

    x402 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/:slug

    A sandbox x402 resource: 402 with PAYMENT-REQUIRED, or the paid response with PAYMENT-RESPONSE.

    Public · 120/min per IP

  • POST/x402/sandbox/:slug

    Same resource with a JSON body (≤ 16 KB).

    Public · 120/min per IP