Skip to main content

Crate openlibx402_core

Crate openlibx402_core 

Source
Expand description

§OpenLibx402 Core

Core library for the X402 payment protocol with Solana blockchain integration.

This library provides the fundamental types, error handling, and payment processing capabilities for implementing the X402 protocol - an open standard enabling AI agents to autonomously pay for API access using Solana blockchain micropayments.

§Features

  • Payment Models: PaymentRequest and PaymentAuthorization for structured payment flow
  • Error Handling: Comprehensive error types for all X402 operations
  • Solana Integration: SolanaPaymentProcessor for blockchain transactions
  • Serialization: Base64-encoded JSON for HTTP headers

§Example

use openlibx402_core::{PaymentRequest, SolanaPaymentProcessor};
use solana_sdk::signature::Keypair;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Parse payment request from API response
    let payment_request = PaymentRequest::from_json(r#"{
        "max_amount_required": "0.10",
        "asset_type": "SPL",
        "asset_address": "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v",
        "payment_address": "7xKXtg2CW87d97TXJSDpbD5jBkheTqA83TZRuJosgAsU",
        "network": "solana-devnet",
        "expires_at": "2025-10-31T12:00:00Z",
        "nonce": "abc123",
        "payment_id": "pay_123",
        "resource": "/api/premium-data"
    }"#)?;

    // Create payment processor
    let processor = SolanaPaymentProcessor::new(
        "https://api.devnet.solana.com",
        None
    );

    // Create and send payment
    let keypair = Keypair::new(); // Use your actual keypair
    let authorization = processor.create_payment(&payment_request, &keypair).await?;

    // Use authorization in retry request
    let header_value = authorization.to_header_value()?;
    println!("X-Payment-Authorization: {}", header_value);

    Ok(())
}

Re-exports§

pub use errors::X402Error;
pub use errors::X402Result;
pub use models::PaymentAuthorization;
pub use models::PaymentRequest;
pub use payment_processor::SolanaPaymentProcessor;

Modules§

errors
models
payment_processor

Constants§

VERSION
Library version