Prerequisites
- A wallet with USDC on the target network (Base, Base Sepolia, Solana, or Solana Devnet)
- The wallet’s private key (for signing payments)
Using @x402/fetch
The @x402/fetch package wraps the standard fetch API to automatically handle 402 responses — detecting payment requirements, signing the USDC transfer, and retrying the request.
Install
Minimal example
How it works
fetchWithPaymentmakes the initial request- If the server returns
402, the wrapper reads the payment requirements from the response - It signs a USDC transfer authorization using your wallet
- It retries the request with the signed payment in the
X-PAYMENTheader - The server settles the payment via the facilitator and returns the data
Reading the payment response
After a successful payment, the server includes settlement details in thePAYMENT-RESPONSE header:
Raw integration (without @x402/fetch)
If you can’t use the wrapper, handle the 402 flow manually:
1. Make the initial request
2. Parse the 402 response
The response body contains payment requirements:3. Sign the payment
Construct and sign a USDCtransferWithAuthorization (EIP-3009) or permit + transferFrom authorization matching the requirements.
4. Retry with the payment
Base64-encode the signed payment payload and include it in theX-PAYMENT header:
Solana client
For Solana, use the@x402/svm package instead of @x402/evm: