x402-reqwest

Crates.iox402-reqwest
lib.rsx402-reqwest
version0.2.0
created_at2025-06-17 20:44:50.195935+00
updated_at2025-08-23 13:38:00.788395+00
descriptionWrapper for reqwest for transparent x402 payments
homepagehttps://x402.rs
repositoryhttps://github.com/x402-rs/x402-rs
max_upload_size
id1716271
size279,864
Sergey Ukustov (ukstv)

documentation

https://docs.rs/x402-reqwest

README

x402-reqwest

Crates.io Docs.rs

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 Required responses
  • Build and sign x402 payment payloads
  • Retry the request with the X-Payment header 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 reqwest middleware
  • EIP-712-compatible signing with alloy
  • Fluent builder-style configuration
  • Token preferences & per-asset payment limits
  • Tracing support (opt-in via telemetry feature)

Installation

Add the dependency:

# Cargo.toml
x402-reqwest = "0.2"

To enable tracing:

x402-reqwest = { version = "0.2", features = ["telemetry"] }

💡 Examples

use reqwest::Client;
use x402_reqwest::{ReqwestWithPayments, ReqwestWithPaymentsBuild, MaxTokenAmountFromAmount};
use alloy::signers::local::PrivateKeySigner;
use x402_rs::network::{Network, USDCDeployment};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let signer: PrivateKeySigner = "0x...".parse()?; // never hardcode real keys!

    let client = Client::new()
        .with_payments(signer)
        .prefer(USDCDeployment::by_network(Network::Base))
        .max(USDCDeployment::by_network(Network::Base).amount("1.00")?)
        .build();

    let res = client
        .get("https://example.com/protected")
        .send()
        .await?;

    println!("Status: {}", res.status());
    Ok(())
}

See more examples on docs.rs

How it works

  1. A 402 Payment Required is received from a server.
  2. The middleware parses the Payment-Required response body.
  3. A compatible payment requirement is selected, based on client preferences.
  4. A signed payload is created (compatible with EIP-3009 TransferWithAuthorization).
  5. The payload is base64-encoded into an X-Payment header.
  6. The request is retried, now with the payment inside the header.

Optional Features

  • telemetry: Enables tracing annotations for richer observability.

Enable it via:

x402-reqwest = { version = "0.2", features = ["telemetry"] }

Related Crates

  • x402-rs: Core x402 types, facilitator traits, helpers.
  • x402-axum: Axum middleware for accepting x402 payments.

License

Apache-2.0

Commit count: 125

cargo fmt