x402-sdk-solana-rust
A Rust SDK for the X402 payment protocol on Solana, enabling pay-per-use APIs with automatic blockchain-based payment handling.
Overview
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
Features
- π 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
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
Quick Start
Client Example
use ;
async
Server Example
use ;
use PaymentMiddleware;
async
Facilitator Example
See examples/facilitator_example.rs for a complete payment verification and settlement service.
Configuration
All examples support .env files for configuration:
Client (.env)
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 (.env)
SERVER_PORT=8080
PUBLIC_KEY=your_public_key
FACILITATOR_URL=http://localhost:8081/api/verify
PRICE=1800
Facilitator (.env)
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
Examples
Run the examples:
# Start facilitator
# Start server (in another terminal)
# Run client (in another terminal)
Architecture
βββββββββββ βββββββββββ ββββββββββββββββ
β 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
Current Limitations
- β οΈ 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)
Testing
Run unit tests:
Run integration tests:
Documentation
Full API documentation is available at docs.rs/x402-sdk-solana-rust
For detailed guides, see:
Contributing
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
License
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.
Acknowledgments
This SDK is inspired by the TypeScript implementation at x402-sdk-for-solana.
Support
- π Documentation
- π Issue Tracker
- π¬ Discussions