Skip to content

x402 payments in Rust: verify, settle, and monitor payments over HTTP 402 flows

License

Notifications You must be signed in to change notification settings

monad-developers/x402-rs

 
 

Repository files navigation

x402-rs

Crates.io Docs.rs GHCR

A comprehensive Rust toolkit for the x402 protocol, enabling blockchain payments directly through HTTP using the native 402 Payment Required status code.

x402-rs is a modular, production-ready implementation of the x402 protocol with support for multiple blockchains (EVM, Solana, Aptos, ..) and protocol versions (V1 and V2).

Core Crates

Crate Badges Description
x402-types Crates.io Docs.rs Core protocol types, facilitator traits, and utilities. Foundation for all x402 implementations.
x402-axum Crates.io Docs.rs Axum middleware for protecting routes with x402 payments.
x402-reqwest Crates.io Docs.rs Reqwest middleware for transparent x402 payment handling.
x402-facilitator-local Crates.io Docs.rs Local facilitator implementation for payment verification and settlement.

Blockchain Support

Crate Badges Description
x402-chain-eip155 Crates.io Docs.rs EVM/EIP-155 chain support (Ethereum, Base, Polygon, etc.)
x402-chain-solana Crates.io Docs.rs Solana blockchain support
x402-chain-aptos ⚠️ Git-only Aptos blockchain support (requires git dependency)

Deployment

Crate Description
x402-facilitator Production-ready facilitator server binary (not published to crates.io)

About x402

The x402 protocol is a proposed standard for making blockchain payments directly through HTTP using the native 402 Payment Required status code.

How it works:

  1. Server declares payment requirements for specific routes
  2. Client sends cryptographically signed payment payloads
  3. Facilitator verifies and settles payments on-chain

This enables seamless pay-per-use transactions without requiring clients to manage blockchain interactions directly.

Quick Start

Protect Routes (Server)

Use x402-axum to gate your routes behind on-chain payments:

use alloy_primitives::address;
use axum::{Router, routing::get};
use x402_axum::X402Middleware;
use x402_chain_eip155::V2Eip155Exact;
use x402_types::networks::USDC;

let x402 = X402Middleware::new("http://facilitator.example.com");

let app = Router::new().route(
    "/paid-content",
    get(handler).layer(
        x402.with_price_tag(V2Eip155Exact::price_tag(
            address!("0xYourAddress"),
            USDC::base_sepolia().amount(10u64),
        ))
    ),
);

See x402-axum documentation for more details.

Send Payments (Client)

Use x402-reqwest to automatically handle x402 payments:

use x402_reqwest::{ReqwestWithPayments, ReqwestWithPaymentsBuild, X402Client};
use x402_chain_eip155::V2Eip155ExactClient;
use alloy_signer_local::PrivateKeySigner;
use std::sync::Arc;
use reqwest::Client;

let signer: Arc<PrivateKeySigner> = Arc::new("0x...".parse()?);

let x402_client = X402Client::new()
    .register(V2Eip155ExactClient::new(signer));

let client = Client::new()
    .with_payments(x402_client)
    .build();

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

See x402-reqwest documentation for more details.

Run a Facilitator

Docker

Prebuilt Docker images are available at GitHub Container Registry:

docker run -v $(pwd)/config.json:/app/config.json -p 8080:8080 ghcr.io/x402-rs/x402-facilitator

Build Your Own

For custom facilitator implementations, see the Build Your Own Facilitator guide.

For full facilitator configuration and deployment details, see the x402-facilitator README.

Roadmap

Milestone Description Status
Facilitator for Base USDC Payment verification and settlement service, enabling real-time pay-per-use transactions for Base chain. ✅ Complete
Metrics and Tracing Expose OpenTelemetry metrics and structured tracing for observability, monitoring, and debugging ✅ Complete
Server Middleware Provide ready-to-use integration for Rust web frameworks such as axum and tower. ✅ Complete
Client Library Provide a lightweight Rust library for initiating and managing x402 payment flows from Rust clients. ✅ Complete
Solana Support Support Solana chain. ✅ Complete
Protocol v2 Support Support x402 protocol version 2 with improved payload structure. ✅ Complete
Multiple chains and multiple tokens Support various tokens and EVM compatible chains. ✅ Complete
Axum Middleware v2 Support Full x402 protocol v2 support in x402-axum with multi-chain, multi-scheme architecture. ✅ Complete
Reqwest Client v2 Support Full x402 protocol v2 support in x402-reqwest with multi-chain, multi-scheme architecture. ✅ Complete
Build your own facilitator hooks Pre/post hooks for analytics, access control, and auditability. 🔜 Planned
Bazaar Extension Marketplace integration for discovering and purchasing x402-protected resources. 🔜 Planned
Gasless Approval Flow Support for Permit2 and ERC20 approvals to enable gasless payment authorization. 🔜 Planned
Upto Scheme Payment scheme supporting "up to" amount payments with flexible pricing. 🔜 Planned
Deferred Scheme Payment scheme supporting deferred settlement and payment scheduling. 🔜 Planned

Related Resources

Contributions and Feedback

Feel free to open issues or pull requests to improve x402 support in the Rust ecosystem.

License

Apache-2.0

About

x402 payments in Rust: verify, settle, and monitor payments over HTTP 402 flows

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Rust 99.6%
  • Other 0.4%