Complete an x402 payment
x402 turns HTTP 402 into a working payment protocol. You request a resource, the server tells you what it costs, you present a signed payment intent, and you get the resource plus a receipt you can verify against an on-chain batch commitment. This walks the whole loop against a real sequencer.Prerequisites
- A payment-gated endpoint (the sequencer ships a demonstration premium route)
- An Ed25519 signing key registered in the agent key registry
- The sequencer base URL
Step 1 — Request the resource, get a 402
details carry the
payment terms:
Step 2 — Build and sign the intent
The signing hash is SHA-256 over a domain-separated, order-dependent preimage. Every field is concatenated in exactly this order, integers as big-endian u64:Step 3 — Retry with X-Payment
The X-Payment header carries standard base64 of the JSON document that
POST /api/v1/x402/payments accepts — base64 because raw JSON is not safe in a header value.
A gated request and a direct submission to
POST /api/v1/x402/payments run through the same
verification and sequencing path. The header route is not a shortcut — the intent gets a sequence
number, burns its nonce, and enters the normal batching pipeline either way.Replay protection, and one trap
The intent is consumed by the request it pays for.- The nonce is reserved in a nonce-tracking table keyed by payer. Presenting the same signed intent
twice fails the second time with
Nonce already used for this payer. - Any
idempotency_keyin the decoded intent is discarded on the header path, deliberately: it is not part of the signed hash, so honouring it would let a replay short-circuit through the idempotency lookup and get the resource twice for one payment.
Step 4 — Fetch and verify the receipt
receipt_url becomes fully populated once the intent is batched — sequencing is immediate,
batching is not.
The full loop
Related
- Sequencer x402 reference — every field and endpoint
- Set L2 verification example — proving inclusion
- Sequencer architecture — batching and anchoring