402 handshake on top of the same calls.
You need a facilitator URL and a merchant address. The examples use the public demo facilitator. Payments are in USDC base units, where 1000 is $0.001.
Accept payments
Quote your price
Ask the facilitator for payment terms once per product or metered resource. Put your own address inpayTo, an identifier for what you sell in resource, and the price per payment in price:
TERMS is what a payer signs against. It names your address, the price, the asset, and the hub. Send it to payers however your product works: in a checkout response, an app’s configuration, or the first message of a stream. Quote again when you change the price or payTo, or when the hubs listed in the facilitator’s /meta change. Do not edit any field in it.
Settle each payment
For each unit the payer buys, they sign a payment for their new running total with you and send it to your server. Pass it to/settle together with the terms:
payTo inside the settle call, so you do not need a separate verification step. Deliver the unit once success is true. If it is false, errorReason says why, and nothing was charged.
A retry with the same payment ID returns the original receipt with replay: true, so a network failure never charges twice. If the facilitator is unreachable or times out, retry with the same payment.
Agentic payments with x402
Agents discover prices and pay over HTTP with x402. A Paychannels facilitator speaks x402 directly: the quote is already a complete402 Payment Required body, and payments arrive in a request header.
Serve the quote as a 402
Quote each paid route with its absolute public URL as theresource:
accepts array has two entries, both addressed to you:
Return this body with status
402 to any request that arrives without a payment. It is safe to cache: send Cache-Control: max-age=3600 and an ETag. Besides the cases above, quote again when a settle is declined with unsupported-requirements.
Settle the payment header
When a request arrives with anX-PAYMENT header (newer x402 clients send PAYMENT-SIGNATURE; treat them the same), decode it, pick the matching entry from your cached 402, and call /settle:
Respond to the caller
Map the facilitator’s answer to your HTTP response. Never serve content before a successful settle.
On a replayed payment (
replay: true), serve the content again and never charge twice.
Cash out
Payments made withexact are already in your wallet. Channel payments accrue to your account on the hub, and you withdraw them in two steps:
- Request. Sign a withdrawal authorization and submit
requestWithdrawto the hub’s channel contract, or have anyone relay it for you. The hub reserves the amount at its next settlement. - Redeem. After the hub posts a settlement covering your reservation, call
withdrawAttestedwith signatures from the hub’s verifiers for an instant payout, orwithdrawwith a Merkle proof once the settlement matures.
Related
- Try a demo: see the payer side of this flow.
- Trust model: when a receipt is final and what the operator is trusted for.
- x402 introduction