fly402-actix

Crates.iofly402-actix
lib.rsfly402-actix
version0.0.3
created_at2025-11-01 07:31:43.660162+00
updated_at2025-11-01 07:31:43.660162+00
descriptionActix Web framework integration for X402 payment protocol
homepagehttps://github.com/SerPepe/402fly
repositoryhttps://github.com/fly402/fly402
max_upload_size
id1911787
size155,489
(SerPepe)

documentation

README

402fly-actix

Actix Web framework integration for the X402 payment protocol.

This library provides extractors and utilities for implementing payment-protected endpoints in Actix Web applications.

Features

  • PaymentExtractor: Request extractor for payment enforcement
  • PaymentError: Custom error type with automatic 402 responses
  • X402State: Application state wrapper for configuration
  • Configuration: Easy payment setup with Fly402Config
  • High Performance: Built on Actix Web's actor system
  • Type Safe: Full type safety with Actix Web patterns

Quick Start

Add to your Cargo.toml:

[dependencies]
402fly-actix = "0.0.1"
actix-web = "4.4"
tokio = "1.35"

Create a Protected Endpoint

use actix_web::{get, web, App, HttpServer, HttpResponse};
use 402fly_actix::{
    Fly402Config, X402State, PaymentExtractor, PaymentRequirement,
    create_payment_request, payment_required_response
};

#[get("/premium")]
async fn premium_content(
    state: web::Data<X402State>,
    auth: Option<PaymentExtractor>,
) -> HttpResponse {
    match auth {
        Some(_) => {
            // Payment verified
            HttpResponse::Ok().json("Premium content here!")
        }
        None => {
            // No payment, return 402
            let requirement = PaymentRequirement::new("0.10")
                .with_description("Access to premium content");
            let payment_request = create_payment_request(&state.config, &requirement, "/premium");
            payment_required_response(payment_request)
        }
    }
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    let config = Fly402Config {
        payment_address: "7xKXtg2CW87d97TXJSDpbD5jBkheTqA83TZRuJosgAsU".to_string(),
        token_mint: "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v".to_string(),
        network: "solana-devnet".to_string(),
        rpc_url: None,
        auto_verify: true,
    };

    let state = web::Data::new(X402State { config });

    HttpServer::new(move || {
        App::new()
            .app_data(state.clone())
            .service(premium_content)
    })
    .bind("127.0.0.1:8080")?
    .run()
    .await
}

Configuration

Fly402Config

pub struct Fly402Config {
    pub payment_address: String,      // Where payments go
    pub token_mint: String,            // Token to accept (e.g., USDC)
    pub network: String,               // Solana network (mainnet, devnet, testnet)
    pub rpc_url: Option<String>,       // Custom RPC endpoint
    pub auto_verify: bool,             // Auto-verify payments
}

PaymentRequirement

let requirement = PaymentRequirement::new("1.00")
    .with_description("Premium API access")
    .with_expires_in(600);  // Expires in 10 minutes

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