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 x402-payments-mcp, image-analysis-mcp, sati, chaingpt-claude-skill, aibtc-mcp-server, moltspay.
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.
Give AI agents a wallet — x402 payment tools over Model Context Protocol.
Image analysis and visual understanding tools for AI agents — describe, detect, extract from any image.
Trust infrastructure for million-agent economies on Solana - identity, reputation, and validation designed for continuous feedback at scale.
154 Web3 MCP tools for Claude Code, custody-free. Plus an agent wallet prompt injection can't drain: per-tx, daily, and on-chain (ERC-4337) spending caps the model can't bypass. EVM + Solana + Tron DEX/DeFi, perps, prediction markets, contract deploy + audit.
Bitcoin-native MCP server for AI agents: BTC/STX wallets, DeFi yield, sBTC peg, NFTs, and x402 payments.
Blockchain payment infrastructure for AI Agents
Agent behavior that compiles
An open SDK for agentic payments. Let AI agents make payments, hold funds, and move money across chains with policy enforcement and human approval built in.
x402 payments in Rust: verify, settle, and monitor payments over HTTP 402 flows
The self-improving LLM router that optimize your agentic workflows with every runs, works with any harnesses, any models, any loops.
Production-ready x402 facilitator server.
Rust SDK for the x402 payment protocol.