How I put a data product behind x402 so AI agents can buy it with USDC
I sell verified B2B lead packs. For a one-off campaign, a $49/month data seat is a terrible deal — and for an AI agent, a subscription signup is a non-starter. Agents can't fill out billing forms. So I put the product behind x402: HTTP 402 "Payment Required", resurrected as a machine checkout. Here's exactly how it works, with the real shapes and flows. What x402 actually is x402 (from Coinbase)…
To sell verified B2B lead packs, I implemented the product behind x402, a machine checkout that allows AI agents to purchase the product without the need for human intervention. The HTTP 402 status code, which signifies payment required, is repurposed as a payment handshake that agents can complete using USDC on a supported network.
When an agent requests a paid resource with no payment attached, the server responds with a 402 status code containing machine-readable payment terms in the body. The agent pays the specified USDC amount using an X-Payment header carrying proof of payment. The server then verifies the payment on-chain and serves the resource. This process eliminates the need for accounts, API keys, or OAuth dances for the buyer, as the wallet is the identity and the retry loop serves as the session.
The 402 response body is not an error page but an offer, containing details such as the payment terms, supported network, token asset, wallet address to receive payment, maximum amount required, timeout, and the resource endpoint for which the terms apply. The agent pays this address directly. The structure of the 402 response body is crucial to ensure accurate and efficient processing of payments.
The client-side implementation involves fetching the pack details from the server and, upon receiving a 402 response, building and signing a USDC transfer according to the scheme provided. The signed payment payload is then included as the X-Payment header in a subsequent request to the payment endpoint. The server-side implementation involves verifying the payment and, if successful, delivering the requested leads as JSON.
By returning 402 status codes and providing a machine-readable manifest at /.well-known/x402, agents can discover the available packs and their terms before attempting to purchase them. This approach reduces the need for trial-and-error through 402 responses and streamlines the purchasing process for AI agents. The pattern of using 402 responses and a single-sourced catalog for both listing and buying products can be applied to any agent-sold data product.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.