Skip to main content

Crate fly402_client

Crate fly402_client 

Source
Expand description

§402fly Client

HTTP client library for the 402fly payment protocol with automatic payment handling.

This library provides two client implementations:

  • X402Client: Explicit control over payment flow
  • X402AutoClient: Automatic payment handling

§Features

  • Automatic detection of 402 Payment Required responses
  • Seamless payment creation and transaction broadcasting
  • Configurable payment limits and retry behavior
  • Support for GET and POST requests

§Example: Explicit Client

use fly402_client::X402Client;
use solana_sdk::signature::Keypair;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let keypair = Keypair::new(); // Load your keypair
    let client = X402Client::new(keypair, None);

    // Make request
    let response = client.get("https://api.example.com/premium-data").await?;

    // Check if payment is required
    if client.is_payment_required(&response) {
        // Parse payment request
        let payment_request = client.parse_payment_request(response).await?;

        // Create payment
        let authorization = client.create_payment(&payment_request).await?;

        // Retry with payment
        let response = client.get_with_auth(
            "https://api.example.com/premium-data",
            &authorization
        ).await?;

        println!("Got data: {:?}", response.text().await?);
    }

    Ok(())
}

§Example: Auto Client

use fly402_client::{X402AutoClient, AutoClientOptions};
use solana_sdk::signature::Keypair;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let keypair = Keypair::new(); // Load your keypair

    // Configure options
    let options = AutoClientOptions {
        max_payment_amount: "5.0".to_string(),
        auto_retry: true,
        max_retries: 3,
    };

    let client = X402AutoClient::new(keypair, None, Some(options));

    // Make request - payment is handled automatically!
    let response = client.get("https://api.example.com/premium-data").await?;
    println!("Got data: {:?}", response.text().await?);

    Ok(())
}

Re-exports§

pub use auto_client::AutoClientOptions;
pub use auto_client::X402AutoClient;
pub use client::X402Client;

Modules§

auto_client
client

Structs§

PaymentAuthorization
Payment authorization sent with retry request
PaymentRequest
Payment request received from server in 402 response
SolanaPaymentProcessor
Solana payment processor for handling blockchain operations

Enums§

X402Error
Base error type for all X402 operations

Constants§

VERSION
Library version

Type Aliases§

X402Result
Result type alias for X402 operations