High-performance, type-safe Rust implementation of the x402 HTTP-native micropayment protocol
rust-x402 is an early-stage Rust project in the AI payments / x402 ecosystem, focused on blockchain, http402, web3, x402. It currently has 8 GitHub stars and 2 forks, and sits alongside related tools like gold-402, internet-court-skill, Inktoll.
A high-performance, type-safe Rust implementation of the x402 HTTP-native micropayment protocol.
🎉 First public debut at EthGlobal Online 2025
Add this to your Cargo.toml:
[dependencies]
rust-x402 = "0.2.2"
use axum::{response::Json, routing::get};
use rust_x402::{
axum::{create_payment_app, examples, AxumPaymentConfig},
types::FacilitatorConfig,
};
use rust_decimal::Decimal;
use std::str::FromStr;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create facilitator config
let facilitator_config = FacilitatorConfig::default();
// Create payment configuration
let payment_config = AxumPaymentConfig::new(
Decimal::from_str("0.0001")?,
"0x209693Bc6afc0C5328bA36FaF03C514EF312287C",
)
.with_description("Premium API access")
.with_facilitator_config(facilitator_config)
.with_testnet(true);
// Create the application with payment middleware
let app = create_payment_app(payment_config, |router| {
router.route("/joke", get(examples::joke_handler))
});
// Start server
let listener = tokio::net::TcpListener::bind("0.0.0.0:4021").await?;
axum::serve(listener, app).await?;
Ok(())
}
use rust_x402::client::X402Client;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = X402Client::new()?;
// Make a request to a protected resource
let response = client.get("http://localhost:4021/joke").send().await?;
if response.status() == 402 {
println!("Payment required! Status: {}", response.status());
// Handle payment required - parse PaymentRequirements and create signed payload
// See examples/client.rs for complete implementation
} else {
let text = response.text().await?;
println!("Response: {}", text);
}
Ok(())
}
The facilitator can run as a standalone binary with optional Redis storage:
# In-memory storage (default)
cargo run --bin facilitator --features axum
# Redis storage backend
STORAGE_BACKEND=redis cargo run --bin facilitator --features axum,redis
# Custom configuration
BIND_ADDRESS=0.0.0.0:4020 \
REDIS_URL=redis://localhost:6379 \
REDIS_KEY_PREFIX=x402:nonce: \
cargo run --bin facilitator --features axum,redis
The Rust implementation is organized into several modules:
types: Core data structures and type definitionsclient: HTTP client with x402 payment supportfacilitator: Payment verification and settlementfacilitator_storage: Nonce storage backends (in-memory and Redis)middleware: Web framework middleware implementationscrypto: Cryptographic utilities for payment signingerror: Comprehensive error handlingwallet: Real wallet integration with EIP-712 signingblockchain: Blockchain client for network interactionsblockchain_facilitator: Blockchain-based facilitator implementationhttp3: HTTP/3 (QUIC) support (feature-gated)proxy: Reverse proxy with streaming supportmultipart/form-data uploads (via multipart feature)streaming feature)http3 feature flagx402 supports optional features for a modular build:
[dependencies]
rust-x402 = { version = "0.2.2", features = ["http3", "streaming", "multipart"] }
http3: Enable HTTP/3 (QUIC) supportstreaming: Enable chunked and streaming responsesmultipart: Enable multipart/form-data upload support (requires streaming)redis: Enable Redis backend for facilitator storageaxum: Enable Axum web framework integration (default)actix-web: Enable Actix Web framework integrationwarp: Enable Warp web framework integrationCurrently supports:
See the examples/ directory for complete working examples:
axum_server.rs: Payment server using Axumclient.rs: Client making paymentsfacilitator.rs: Custom facilitator implementationreal_implementation_demo.rs: Real wallet and blockchain integrationreal_wallet_integration.rs: Production-ready wallet integrationThis project follows a clean, modular architecture for better maintainability:
src/
├── facilitator/ # Payment verification & settlement
│ ├── mod.rs # Main client implementation
│ ├── coinbase.rs # Coinbase CDP integration
│ └── tests.rs # Comprehensive test suite
│
├── crypto/ # Cryptographic utilities
│ ├── mod.rs # Module exports
│ ├── jwt.rs # JWT authentication
│ ├── eip712.rs # EIP-712 typed data hashing
│ ├── signature.rs # ECDSA signature verification
│ └── tests.rs # Crypto test suite
│
├── types/ # Core protocol types
│ ├── mod.rs # Type exports
│ ├── network.rs # Network configurations
│ ├── payment.rs # Payment types
│ ├── facilitator.rs # Facilitator types
│ ├── discovery.rs # Discovery API types
│ └── constants.rs # Protocol constants
│
├── middleware/ # Web framework middleware
│ ├── mod.rs # Module exports
│ ├── config.rs # Middleware configuration
│ ├── payment.rs # Payment processing logic
│ ├── service.rs # Tower service layer
│ └── tests.rs # Middleware tests
│
└── ... # Other modules
Benefits:
mod.rsAll module documentation is embedded in the code - run cargo doc --no-deps --open to view!
http3 feature)Licensed under the Apache License, Version 2.0. See LICENSE for details.
⚡ The gold standard for x402 resources. 300+ projects, SDKs, tools, facilitators, and ecosystem data for the HTTP 402 Payment Required protocol. Curated by 24K Labs.
The trust layer for agent-to-agent commerce — natural-language mandates, ERC-7710 delegated permissions, x402 payments, escrow, and dispute resolution as one open, catch-all Agent Skill / Claude Code plugin.
A decentralized micro-settlement protocol enabling autonomous AI agents to pay publishers for premium content via Circle Programmable Wallets and x402 nanopayments.