A complete Rust implementation of the x402 protocol for blockchain-based micropayments over HTTP
x402-rs is an early-stage Rust project in the AI payments / x402 ecosystem. It currently has 0 GitHub stars and 0 forks.
A complete, production-ready Rust implementation of the x402 protocol for blockchain-based micropayments over HTTP.
The x402 protocol revitalizes the HTTP 402 "Payment Required" status code to enable seamless, instant payments for web resources like APIs, content, or files. It provides:
Add to your Cargo.toml:
[dependencies]
x402-rs = "0.1.0"
For async runtime:
[dependencies]
x402-rs = "0.1.0"
tokio = { version = "1", features = ["full"] }
Make requests to x402-enabled APIs with automatic payment handling:
use x402_rs::client::{X402ClientConfig, get};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Configure client with your private key and RPC endpoint
let config = X402ClientConfig::new(
"0xYOUR_PRIVATE_KEY",
"https://mainnet.base.org"
);
// Make a request - payment is handled automatically
let response = get(&config, "https://api.example.com/weather").await?;
println!("Response: {}", response.text().await?);
Ok(())
}
Protect your endpoints with payment requirements:
use x402_rs::server::PaymentConfig;
use axum::{routing::get, Router};
#[tokio::main]
async fn main() {
// Configure payment requirements
let payment_config = PaymentConfig::new(
"0xYOUR_ADDRESS", // Recipient address
"0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913", // USDC on Base
6, // Token decimals
"8453", // Base mainnet
"exact", // Payment scheme
0.01, // $0.01 USD
"API access fee",
"https://facilitator.example.com",
);
// Your endpoint handler would check for X-PAYMENT header
// and verify/settle payments using the config
// See examples/server.rs for complete implementation
}
A facilitator verifies and settles payments on behalf of servers:
use x402_rs::facilitator::FacilitatorConfig;
#[tokio::main]
async fn main() {
let config = FacilitatorConfig::new(
"0xFACILITATOR_PRIVATE_KEY", // For paying gas
"https://mainnet.base.org"
);
// Implement /verify, /settle, /supported endpoints
// See examples/facilitator.rs for complete implementation
}
X-PAYMENT headerX-PAYMENT-RESPONSE headerCurrently supports EVM-compatible chains with EIP-3009:
Any EVM chain with EIP-3009 compatible tokens (USDC, etc.) is supported.
The "exact" scheme requires payment of exactly the specified amount using EIP-3009 transferWithAuthorization. Key features:
The repository includes three complete examples:
# Set environment variables
export PAY_TO="0xYourAddress"
export FACILITATOR_URL="http://localhost:3001"
# Run the server
cargo run --example server
# Set environment variables
export PRIVATE_KEY="0xYourPrivateKey"
export RPC_URL="https://mainnet.base.org"
export API_URL="http://localhost:3000/weather"
# Run the client
cargo run --example client
# Set environment variables
export FACILITATOR_KEY="0xFacilitatorPrivateKey"
export RPC_URL="https://mainnet.base.org"
# Run the facilitator
cargo run --example facilitator
Run the test suite:
cargo test
Run with logs:
RUST_LOG=debug cargo test
maxTimeoutSeconds to prevent stale authorizationsx402-rs/
├── src/
│ ├── lib.rs # Main library entry point
│ ├── types.rs # Protocol type definitions
│ ├── errors.rs # Error types
│ ├── utils.rs # Utility functions
│ ├── client.rs # Client implementation
│ ├── server.rs # Server middleware
│ ├── facilitator.rs # Facilitator service
│ └── schemes/
│ ├── mod.rs # Scheme trait
│ └── exact_evm.rs # EIP-3009 implementation
├── examples/
│ ├── server.rs # Example API server
│ ├── client.rs # Example client
│ └── facilitator.rs # Example facilitator
└── tests/
└── integration.rs # Integration tests
Generate and view the full API documentation:
cargo doc --open
Contributions are welcome! Please:
cargo testcargo clippy -- -D warningscargo fmtLicensed under Apache License, Version 2.0 (LICENSE or http://www.apache.org/licenses/LICENSE-2.0)
Note: This library is under active development. The API may change before 1.0 release.
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
Context-aware agentic LLM gateway & router that optimize your agentic workflows with every runs, works with any harnesses, any models, any workflows.
Production-ready x402 facilitator server.
Rust SDK for the x402 payment protocol.