Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
x402-reqwest
Reqwest middleware that transparently handles HTTP 402 Payment Required responses using the x402 protocol.
This crate enables your reqwest or reqwest-middleware-based HTTP clients to:
- Detect
402 Payment Requiredresponses - Extract payment requirements from the response
- Sign payments using registered scheme clients
- Retry the request with the payment header attached
All in all: automatically pay for resources using the x402 protocol.
Features
- Pluggable reqwest middleware using reqwest-middleware
- Multi-chain support (EVM via EIP-155, Solana)
- Full V1 and V2 protocol support with automatic detection and handling
- Multi-scheme architecture supporting various payment schemes
- Customizable payment selection logic
- Tracing support (opt-in via
telemetryfeature)
Installation
Add the dependency:
# Cargo.toml
= "0.6"
To enable tracing:
= { = "0.6", = ["telemetry"] }
Quickstart
use ;
use V1Eip155ExactClient;
use PrivateKeySigner;
use Arc;
use Client;
let signer: = new;
// Create an X402 client and register scheme handlers
let x402_client = new
.register;
// Build a reqwest client with x402 middleware
let http_client = new
.with_payments
.build;
// Use the client - payments are handled automatically
let response = http_client
.get
.send
.await?;
println!;
Registering Scheme Clients
The [X402Client] uses a plugin architecture for supporting different payment schemes.
Register scheme clients for each chain/network you want to support:
use ;
use ;
use ;
use PrivateKeySigner;
use RpcClient;
use Keypair;
use Arc;
use Client;
let evm_signer: = new;
let solana_keypair = new;
let solana_rpc_client = new;
let x402_client = new
// Register EVM schemes (V1 and V2)
.register
.register
// Register Solana schemes (V1 and V2)
.register
.register;
let http_client = new
.with_payments
.build;
How It Works
- A request is made to a server
- If a
402 Payment Requiredresponse is received, the middleware:- Parses the Payment-Required response (V1 body or V2 header)
- Finds registered scheme clients that can handle the payment
- Selects the best matching payment option
- Signs the payment using the scheme client
- Retries the request with the payment header attached
Payment Selection
When multiple payment options are available, the [X402Client] uses a [PaymentSelector]
to choose the best option. By default, it uses [FirstMatch] which selects the first
matching scheme.
You can implement custom selection logic:
use X402Client;
use ;
;
let client = new
.with_selector;
Optional Features
telemetry: Enables tracing annotations for richer observabilityjson: Enables JSON support for the reqwest-middleware, allowing.json()calls when making a HTTP request
Enable them via:
= { = "0.6", = ["telemetry", "json"] }
Telemetry
When the telemetry feature is enabled, the middleware emits structured tracing events for key operations:
- x402.reqwest.handle: Span covering the entire middleware handling, including 402 detection and payment retry
- x402.reqwest.next: Span for the underlying HTTP request (both initial and retry)
- x402.reqwest.make_payment_headers: Span for payment header creation and signing
- x402.reqwest.parse_payment_required: Span for parsing 402 responses (V1 body or V2 header)
The telemetry includes:
- Payment version (V1 or V2)
- Selected scheme and network
- Request URLs and response status codes
- Payment parsing results
This integrates with any tracing-compatible subscriber. For OpenTelemetry export, see x402-types telemetry.
Related Crates
- x402-types: Core x402 types, facilitator traits, helpers.
- x402-axum: Axum middleware for accepting x402 payments.
- x402-chain-eip155: EIP-155 chain support for x402.
- x402-chain-solana: Solana chain support for x402.