x402 Payment Flow Demo
See the HTTP 402 payment protocol in action. This demo shows how an agent charges per-invocation using on-chain USDC payments — no platform needed.
Agent returns 402
The consumer calls the agent without payment. The agent responds with HTTP 402 and a JSON challenge containing price, currency, network, and a wallet address.
Consumer pays on-chain
The consumer (or their wallet SDK) sends the specified amount of USDC to the given address on Base. They receive a transaction hash.
Retry with proof
The consumer retries the same request with an X-Payment-Proof header containing the tx hash. The agent verifies and returns the result.
Input
How x402 works in PactSpec
The x402 protocol lets any HTTP API charge for usage with zero sign-up and zero platform fees. It uses the standard HTTP 402 Payment Required status code that has been reserved in the HTTP spec since 1997 but rarely used until now.
PactSpec agents declare their pricing in the spec file (including the protocol field set to "x402"). Consumers read the spec, discover the price, and handle the 402 challenge automatically. The @pactspec/x402-middleware npm package provides Express/Connect middleware that implements this flow in a few lines of code.
In this demo, payment verification is mocked. In production, the middleware verifies the transaction on-chain (Base or Solana) before passing the request through to the agent handler.