fly402-client

Crates.iofly402-client
lib.rsfly402-client
version0.0.3
created_at2025-11-01 07:30:28.623218+00
updated_at2025-11-01 07:30:28.623218+00
descriptionHTTP client library for the X402 payment protocol
homepagehttps://github.com/SerPepe/402fly
repositoryhttps://github.com/fly402/fly402
max_upload_size
id1911785
size160,089
(SerPepe)

documentation

README

402fly-client

HTTP client library for the X402 payment protocol with automatic and explicit payment handling.

This library provides both high-level (automatic) and low-level (explicit) APIs for interacting with X402-enabled services.

Features

  • Fly402Client: Explicit control over payment requests and responses
  • Fly402AutoClient: Automatic payment handling with configurable payment limits and retry logic
  • Full HTTP Support: GET and POST requests with transparent payment integration
  • Error Handling: Proper error types for payment failures and network issues
  • Async/Await: Built on Tokio for non-blocking operations

Quick Start

Add to your Cargo.toml:

[dependencies]
402fly-client = "0.0.1"
tokio = { version = "1.35", features = ["full"] }

Automatic Payment (Recommended)

use 402fly_client::{Fly402AutoClient, AutoClientOptions};
use solana_sdk::signer::keypair::Keypair;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let keypair = Keypair::new();
    let client = Fly402AutoClient::new(
        keypair,
        None,  // use default RPC
        AutoClientOptions::default(),
    );

    // Request is automatically handled - client pays if needed
    let response = client.get("https://api.example.com/premium").await?;
    let body = response.text().await?;
    println!("{}", body);

    Ok(())
}

Explicit Payment Control

use 402fly_client::Fly402Client;
use solana_sdk::signer::keypair::Keypair;

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

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

    // Check for payment requirement
    if client.is_payment_required(&response) {
        let payment_request = client.parse_payment_request(&response).await?;
        let auth = client.create_payment(&payment_request).await?;

        // Retry with payment
        let response = client.get_with_auth(
            "https://api.example.com/premium",
            auth
        ).await?;
        println!("{}", response.text().await?);
    }

    Ok(())
}

Configuration

AutoClientOptions

pub struct AutoClientOptions {
    pub max_payment_amount: String,  // Default: "10.0"
    pub auto_retry: bool,            // Default: true
    pub max_retries: u32,            // Default: 3
}

Documentation

For full documentation, visit: https://402fly.github.io/docs

License

MIT License - See LICENSE file for details.

Related Packages

Commit count: 0

cargo fmt