| Crates.io | rust-x402 |
| lib.rs | rust-x402 |
| version | 0.3.0 |
| created_at | 2025-09-28 13:43:43.549897+00 |
| updated_at | 2025-12-04 12:23:49.9146+00 |
| description | HTTP-native micropayments with x402 protocol |
| homepage | https://x402.org |
| repository | https://github.com/RyanKung/rust-x402 |
| max_upload_size | |
| id | 1858341 |
| size | 1,163,530 |
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.