Skip to main content

Module client

Module client 

Source
Available on crate feature client only.
Expand description

Reqwest middleware for automatic x402 payment handling.

This crate provides a X402Client that can be used as a reqwest middleware to automatically handle 402 Payment Required responses. When a request receives a 402 response, the middleware extracts payment requirements, signs a payment, and retries the request with the payment header.

§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:

  • V2Eip155ExactClient (from r402-evm) - EIP-155 chains, “exact” payment scheme
  • V2SolanaExactClient (from r402-svm) - Solana chains, “exact” payment scheme

See X402Client::register for more details on registering scheme clients.

§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 by providing your own selector.

See X402Client::with_selector for custom payment selection.

Re-exports§

pub use hooks::ClientHooks;

Modules§

hooks
Lifecycle hooks for the x402 client payment creation pipeline.

Structs§

ReqwestWithPaymentsBuilder
Builder for creating a reqwest client with x402 middleware.
X402Client
The main x402 client that orchestrates scheme clients and selection.

Traits§

ReqwestWithPayments
Trait for adding x402 payment handling to reqwest clients.
ReqwestWithPaymentsBuild
Trait for building the final client from a ReqwestWithPaymentsBuilder.

Functions§

parse_payment_required
Parses a 402 Payment Required response into a proto::PaymentRequired.