A Rust SDK for the X402 payment protocol on Solana, enabling pay-per-use APIs with automatic blockchain-based payment handling.
Watch a quick 1-minute demonstration of the complete payment flow:
See the SDK in action with:
- Facilitator service processing payments
- Protected API server with payment requirements
- Client automatically handling payments
- Real transaction verification on Solana blockchain
X402 is a payment protocol that allows API providers to charge users on a per-request basis using blockchain payments. This SDK provides three main components:
- Client: HTTP client with automatic payment handling (402 Payment Required)
- Server: Payment-protected API server with middleware
- Facilitator: Payment verification and settlement service
- π Automatic payment handling for HTTP 402 responses
- β‘ Async/await support with Tokio
- π Solana blockchain integration
- π° Support for SOL transfers (SPL Token support planned)
- π‘οΈ Built-in signature verification
- π Comprehensive error handling
- π§ Easy configuration via environment variables
Add this to your Cargo.toml:
[dependencies]
x402-sdk-solana-rust = "0.1.0"use x402_sdk_solana_rust::{PaymentClient, solana::Wallet};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Initialize wallet from private key
let wallet = Wallet::from_private_key("your-private-key")?;
// Create payment client
let client = PaymentClient::new(
wallet,
"your-receiver-address",
100000, // max payment (0.1 USDC in atomic units)
"https://api.mainnet-beta.solana.com",
Some("http://localhost:8081/api/settle"),
);
// Make request with automatic payment handling
let response = client.get("http://localhost:8080/api/data").await?;
println!("Response: {}", response.text().await?);
Ok(())
}use actix_web::{web, App, HttpServer, HttpResponse};
use x402_sdk_solana_rust::server::PaymentMiddleware;
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
.wrap(PaymentMiddleware::new(
"your-public-key",
"http://localhost:8081/api/verify",
"1800", // price in atomic units
))
.route("/api/data", web::get().to(|| async {
HttpResponse::Ok().json(serde_json::json!({"data": "value"}))
}))
})
.bind("127.0.0.1:8080")?
.run()
.await
}See examples/facilitator_example.rs for a complete payment verification and settlement service.
All examples support .env files for configuration:
WALLET_PRIVATE_KEY=your_private_key_base58
RECEIVER_ADDRESS=receiver_public_key
MAX_PAYMENT=100000
RPC_URL=https://api.mainnet-beta.solana.com
FACILITATOR_URL=http://localhost:8081/api/settle
TARGET_URL=http://localhost:8080/api/dataSERVER_PORT=8080
PUBLIC_KEY=your_public_key
FACILITATOR_URL=http://localhost:8081/api/verify
PRICE=1800FACILITATOR_PORT=8081
RPC_URL=https://api.mainnet-beta.solana.com
EXPECTED_RECEIVER=receiver_public_key
WALLET_PRIVATE_KEY=facilitator_private_key_base58
TOKEN_DECIMALS=6Run the examples:
# Start facilitator
cargo run --example facilitator_example
# Start server (in another terminal)
cargo run --example server_example
# Run client (in another terminal)
cargo run --example client_exampleβββββββββββ βββββββββββ ββββββββββββββββ
β Client β ββ402βββΆβ Server β βββββββΆ β Facilitator β
β β ββsigβββ€ β βverifyββ€ β
β β βββββββββββ β β
β β β β
β β ββtxβββΆ Solana Chain βββββββ€ β
βββββββββββ ββββββββββββββββ
- Client requests API endpoint
- Server returns 402 with payment challenge
- Client creates and signs payment transaction
- Client sends transaction to facilitator for settlement
- Facilitator verifies and submits to Solana
- Client retries request with payment proof
β οΈ Currently supports SOL transfers only (not SPL Tokens/USDC)- To use USDC/SPL Tokens, modify client to call
create_token_transfer_transaction - Server configured for USDC decimals (6) but client uses SOL transfers (9 decimals)
Run unit tests:
cargo test --libRun integration tests:
cargo test --test integration_testsFull API documentation is available at docs.rs/x402-sdk-solana-rust
For detailed guides, see:
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is dual-licensed under:
- MIT License (LICENSE-MIT or http://opensource.org/licenses/MIT)
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
You may choose either license for your use.
This SDK is inspired by the TypeScript implementation at x402-sdk-for-solana.
- π Documentation
- π Issue Tracker
- π¬ Discussions
