| Crates.io | x402-sdk-solana-rust |
| lib.rs | x402-sdk-solana-rust |
| version | 0.1.4 |
| created_at | 2025-11-05 11:30:17.484382+00 |
| updated_at | 2025-11-07 06:53:46.7203+00 |
| description | Rust SDK for X402 payment protocol on Solana - enabling pay-per-use APIs with automatic payment handling |
| homepage | https://github.com/zhiming817/x402-sdk-solana-rust |
| repository | https://github.com/zhiming817/x402-sdk-solana-rust |
| max_upload_size | |
| id | 1917866 |
| size | 300,178 |
A Rust SDK for the X402 payment protocol on Solana, enabling pay-per-use APIs with automatic blockchain-based payment handling.
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:
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/data
SERVER_PORT=8080
PUBLIC_KEY=your_public_key
FACILITATOR_URL=http://localhost:8081/api/verify
PRICE=1800
FACILITATOR_PORT=8081
RPC_URL=https://api.mainnet-beta.solana.com
EXPECTED_RECEIVER=receiver_public_key
WALLET_PRIVATE_KEY=facilitator_private_key_base58
TOKEN_DECIMALS=6
Run 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 βββββββ€ β
βββββββββββ ββββββββββββββββ
create_token_transfer_transactionRun unit tests:
cargo test --lib
Run integration tests:
cargo test --test integration_tests
Full 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.
git checkout -b feature/amazing-feature)git commit -m 'Add amazing feature')git push origin feature/amazing-feature)This project is dual-licensed under:
You may choose either license for your use.
This SDK is inspired by the TypeScript implementation at x402-sdk-for-solana.