Example: an x402 client
The x402 payment handshake from the buyer's side: request a paid resource, receive 402 with payment requirements, sign the authorization with your own key, retry, and walk away with the resource plus a quitanza proving the matter closed. The example starts a local API in-process.
From a checkout of the Quitanza repository:
pnpm install
pnpm exec tsx examples/x402-client.ts
What it shows
- The offer: a bare GET returns
402with machine-readable payment requirements: amount, asset, resource, and where to pay. - Acceptance is a signature:
createPaymentHeadersigns the payment authorization with the buyer's key, client-side, and the retry carries it inX-PAYMENT. - Settlement rides the response:
X-PAYMENT-RESPONSEnames the escrow and the quitanza; behind one round trip the full lifecycle ran, create, fund, deliver, verify. - The proof is first-class: the quitanza verifies like any other, API or offline.
The shape of it
const offer = await fetch(url); // 402 + requirements
const header = createPaymentHeader(offer.accepts[0], buyerKeys);
const paid = await fetch(url, { headers: { "x-payment": header } });
const settled = decodePaymentResponse(paid.headers.get("x-payment-response"));
await qz.quitanzas.verify(settled.quitanzaId); // all true
One HTTP round trip more than a free request, one proof more too. The server side of this handshake is described on the x402 adapter page.