pub struct X402Client<TSelector> { /* private fields */ }client only.Expand description
The main x402 client that orchestrates scheme clients and selection.
The X402Client acts as middleware for reqwest, automatically handling
402 Payment Required responses by extracting payment requirements, signing
payments, and retrying requests.
Implementations§
Source§impl X402Client<FirstMatch>
impl X402Client<FirstMatch>
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new X402Client with default settings.
The default client uses FirstMatch payment selection, which selects
the first matching payment scheme.
Source§impl<TSelector> X402Client<TSelector>
impl<TSelector> X402Client<TSelector>
Sourcepub fn register<S>(self, scheme: S) -> Selfwhere
S: SchemeClient + 'static,
pub fn register<S>(self, scheme: S) -> Selfwhere
S: SchemeClient + 'static,
Registers a scheme client for specific chains or networks.
Scheme clients handle the actual payment signing for specific protocols. You can register multiple clients for different chains or schemes.
§Arguments
scheme- The scheme client implementation to register
§Returns
A new X402Client with the additional scheme registered.
Sourcepub fn with_selector<P: PaymentSelector + 'static>(
self,
selector: P,
) -> X402Client<P>
pub fn with_selector<P: PaymentSelector + 'static>( self, selector: P, ) -> X402Client<P>
Sets a custom payment selector.
By default, FirstMatch is used which selects the first matching scheme.
You can implement custom selection logic by providing your own PaymentSelector.
Sourcepub fn with_policy<P: PaymentPolicy + 'static>(self, policy: P) -> Self
pub fn with_policy<P: PaymentPolicy + 'static>(self, policy: P) -> Self
Adds a payment policy to the filtering pipeline.
Policies are applied in registration order before the selector picks the final candidate. Use policies to restrict which networks, schemes, or amounts are acceptable.
Sourcepub fn with_hook(self, hook: impl ClientHooks + 'static) -> Self
pub fn with_hook(self, hook: impl ClientHooks + 'static) -> Self
Adds a lifecycle hook for payment creation.
Hooks allow intercepting the payment creation pipeline for logging, custom validation, or error recovery. Multiple hooks are executed in registration order.
Source§impl<TSelector> X402Client<TSelector>where
TSelector: PaymentSelector,
impl<TSelector> X402Client<TSelector>where
TSelector: PaymentSelector,
Sourcepub async fn make_payment_headers(
&self,
res: Response,
) -> Result<HeaderMap, ClientError>
pub async fn make_payment_headers( &self, res: Response, ) -> Result<HeaderMap, ClientError>
Creates payment headers from a 402 response.
This method extracts the payment requirements from the response, selects the best payment option, signs the payment, and returns the appropriate headers to include in the retry request.
§Arguments
res- The 402 Payment Required response
§Returns
A HeaderMap containing the payment signature header, or an error.
§Errors
Returns ClientError::ParseError if the response cannot be parsed.
Returns ClientError::NoMatchingPaymentOption if no registered scheme
can handle the payment requirements.
§Panics
Panics if the signed payload is not a valid HTTP header value.
Trait Implementations§
Source§impl Default for X402Client<FirstMatch>
impl Default for X402Client<FirstMatch>
Source§impl<TSelector> Middleware for X402Client<TSelector>
impl<TSelector> Middleware for X402Client<TSelector>
Source§fn handle<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
req: Request,
extensions: &'life1 mut Extensions,
next: Next<'life2>,
) -> Pin<Box<dyn Future<Output = Result<Response>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
fn handle<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
req: Request,
extensions: &'life1 mut Extensions,
next: Next<'life2>,
) -> Pin<Box<dyn Future<Output = Result<Response>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Handles a request, automatically handling 402 responses.
When a 402 response is received, this middleware:
- Extracts payment requirements from the response
- Signs a payment using registered scheme clients
- Retries the request with the payment header
If the request body is not cloneable (e.g. streaming), the middleware cannot auto-retry after a 402. In that case the original 402 response is returned as-is so the caller can handle it manually.