Skip to content

Latest commit

 

History

History
 
 

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

README.md

x402-reqwest-exact

An example client that uses x402-reqwest to pay for HTTP requests using the exact x402 payment scheme.

The "exact" scheme transfers the precise token amount required by the server — no permit signatures, no approval flows. This example registers exact scheme clients for both EVM and Solana chains across both V1 and V2 protocol versions.

This small demo shows how to configure a reqwest client to:

  • Interact with x402-protected endpoints using the exact scheme
  • Support multiple chains (EVM and Solana)
  • Work with both V1 and V2 protocol versions

What it does

On startup, this example:

  • Reads your private keys from env variables EVM_PRIVATE_KEY and SOLANA_PRIVATE_KEY
  • Builds a reqwest client using reqwest-middleware and x402-reqwest
  • Registers exact scheme clients (V1Eip155ExactClient, V2Eip155ExactClient, V1SolanaExactClient, V2SolanaExactClient)
  • Sends a request to a protected endpoint

If the server responds with a 402 Payment Required, the client:

  • Parses the server's requirements (supports both V1 and V2 protocols)
  • Selects a supported token (e.g. USDC on Base Sepolia or Solana)
  • Signs a TransferWithAuthorization (EVM) or SPL token transfer (Solana) for the exact amount requested
  • Retries with the signed payment attached

The best part? You don't have to worry about any of this. Just set your token preferences and treat it like any other reqwest HTTP client.

Prerequisites

  • Private keys with testnet funds (Base Sepolia USDC for EVM, Solana Devnet USDC for Solana)
  • Rust + Cargo
  • EVM_PRIVATE_KEY and/or SOLANA_PRIVATE_KEY set in your environment (or in .env file, see .env.example)

Running the Example

# 1. Clone the repo and cd into this example folder
# 2. Create `.env` file
cp .env.example .env
# 3. Set your EVM_PRIVATE_KEY and/or SOLANA_PRIVATE_KEY inside `.env`
# 4. Run
cargo run

You should see the request succeed and print the server's response.

Behind the scenes

This example uses:

  • x402-reqwest to intercept 402s and attach signed payments
  • alloy for EVM signing
  • dotenvy to load the .env file
  • x402-rs for token/network definitions and amount conversion

Related

  • x402-rs: Common types and facilitator logic
  • x402-axum: Axum server-side middleware to accept payments
  • x402-reqwest: The crate this example is showcasing