Skip to main content

Manual x402 client flow (Python)

This page shows how to perform the entire x402 flow by hand in Python: no starter kit, no x402.http.clients wrappers. You send raw HTTP requests, decode the 402 response, build the payment payload, and resend with the payment signature. For production youโ€™ll usually use the httpx or requests quickstarts; this tutorial is for agents or environments that need a minimal, dependency-light implementation or want to understand the protocol step by step.

1. Request the resource

Send a normal GET (or other method) to the protected URL. No special headers yet.

2. Handle 402 and decode PAYMENT-REQUIRED

If the server requires payment, it returns 402 Payment Required and puts the payment options in the PAYMENT-REQUIRED header (base64-encoded JSON).
Exact field names and shapes are in the x402 Reference. You must use x402Version: 2 and the accepts array.

3. Choose an accepted option

Pick one entry from payment_required["accepts"] by network or scheme. For EVM (steps below), take an option whose network starts with eip155:. For Solana, take one whose network starts with solana: (see Solana (exact scheme) in step 4).

4. Build the payment payload

For the exact scheme on EVM, the client must produce an EIP-3009-style authorization and sign it with EIP-712 (see x402 Reference). The payload is sent in the PAYMENT-SIGNATURE header as base64-encoded JSON. You need a signer (e.g. eth_account or web3) to produce the signature and authorization fields. Example shape (simplified; real code must use correct domain, types, and signing):

Solana (exact scheme)

On Solana, you choose a Solana accept option (e.g. network starting with solana:), then build a partially-signed transaction and send it in the payload as base64. The transaction must contain these instructions in this order:
  1. SetComputeUnitLimit โ€” max 40,000 compute units
  2. SetComputeUnitPrice โ€” max 5 microlamports per compute unit
  3. TransferChecked โ€” token transfer (amount, mint, decimals, source, destination)
  4. Optional: Lighthouse (wallets like Phantom/Solflare may add up to two; merchants do not add these)
You need a Solana SDK (e.g. solders or solana-py) to build and sign the transaction. The payment payload is JSON with the transaction in payload.transaction as base64:
Exact instruction formats, token-account derivation, and limits are in the x402 Reference (ยง6.2 Solana).

5. Resend the request with PAYMENT-SIGNATURE

Send the same request again (same URL and method), this time adding the PAYMENT-SIGNATURE header.

6. Parse the response and PAYMENT-RESPONSE

On success the server returns 200 and may include PAYMENT-RESPONSE (base64-encoded settlement details). Decode the header and parse the JSON to get settlement info.
Decoded PAYMENT-RESPONSE examples On success (EVM):
On success (Solana / SVM):
On failure (e.g. insufficient funds), the server may return a non-2xx status and/or a PAYMENT-RESPONSE header with an error. Example decoded payload:

Summary

For exact field names, types, and facilitator usage, see the x402 Reference. For a ready-made client, use the httpx or requests quickstarts.

Need help?

Join our Community

Have questions or want to connect with other developers? Join our Discord server.