x402-reqwest
Wrapper around reqwest 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 - Build and sign x402 payment payloads
- Retry the request with the
X-Paymentheader attached - Respect client-defined preferences like token priority and per-token payment caps
All in all: automatically pay for resources using the x402 protocol.
Built with reqwest-middleware and compatible with any alloy::Signer.
Features
- Pluggable
reqwestmiddleware - EIP-712-compatible signing with
alloy - Fluent builder-style configuration
- Token preferences & per-asset payment limits
- Tracing support (opt-in via
telemetryfeature)
Installation
Add the dependency:
# Cargo.toml
= "0.2"
To enable tracing:
= { = "0.2", = ["telemetry"] }
💡 Examples
use Client;
use ;
use PrivateKeySigner;
use ;
async
See more examples on docs.rs
How it works
- A 402 Payment Required is received from a server.
- The middleware parses the Payment-Required response body.
- A compatible payment requirement is selected, based on client preferences.
- A signed payload is created (compatible with EIP-3009
TransferWithAuthorization). - The payload is base64-encoded into an
X-Paymentheader. - The request is retried, now with the payment inside the header.
Optional Features
telemetry: Enables tracing annotations for richer observability.
Enable it via:
= { = "0.2", = ["telemetry"] }
Related Crates
- x402-rs: Core x402 types, facilitator traits, helpers.
- x402-axum: Axum middleware for accepting x402 payments.