Skip to main content

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

  1. fetchWithPayment makes the initial request
  2. If the server returns 402, the wrapper reads the payment requirements from the response
  3. It signs a USDC transfer authorization using your wallet
  4. It retries the request with the signed payment in the X-PAYMENT header
  5. The server settles the payment via the facilitator and returns the data
No manual header construction. No base64 encoding. The wrapper handles everything.

Reading the payment response

After a successful payment, the server includes settlement details in the PAYMENT-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 USDC transferWithAuthorization (EIP-3009) or permit + transferFrom authorization matching the requirements.

4. Retry with the payment

Base64-encode the signed payment payload and include it in the X-PAYMENT header:
The server will settle the payment via the facilitator and return the data.

Solana client

For Solana, use the @x402/svm package instead of @x402/evm:

Install

Example