fly402-rocket

Crates.iofly402-rocket
lib.rsfly402-rocket
version0.0.3
created_at2025-11-01 07:33:21.358055+00
updated_at2025-11-01 07:33:21.358055+00
descriptionRocket web framework integration for X402 payment protocol
homepagehttps://github.com/SerPepe/402fly
repositoryhttps://github.com/fly402/fly402
max_upload_size
id1911788
size158,868
(SerPepe)

documentation

README

402fly-rocket

Rocket web framework integration for the X402 payment protocol.

This library provides middleware and utilities for implementing payment-protected endpoints in Rocket web applications.

Features

  • PaymentGuard: Request guard for enforcing payment requirements
  • PaymentRequiredResponse: Automatic 402 response generation
  • Configuration: Easy payment setup with Fly402Config
  • Flexible Requirements: Per-endpoint payment amounts and descriptions
  • Type Safe: Full Rocket integration with type safety

Quick Start

Add to your Cargo.toml:

[dependencies]
402fly-rocket = "0.0.1"
rocket = { version = "0.5", features = ["json"] }
tokio = "1.35"

Create a Protected Endpoint

use rocket::{get, routes, State};
use 402fly_rocket::{
    Fly402Config, PaymentGuard, PaymentRequirement,
    create_payment_request, PaymentRequiredResponse
};

#[get("/premium")]
fn premium_content(
    config: &State<Fly402Config>,
    _auth: Option<PaymentGuard>
) -> Result<String, PaymentRequiredResponse> {
    match _auth {
        Some(_) => {
            // Payment verified
            Ok("Premium content here!".to_string())
        }
        None => {
            // No payment, return 402
            let requirement = PaymentRequirement::new("0.10")
                .with_description("Access to premium content");
            let payment_request = create_payment_request(config, &requirement, "/premium");
            Err(PaymentRequiredResponse { payment_request })
        }
    }
}

#[rocket::main]
async fn main() -> Result<(), rocket::Error> {
    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 _rocket = rocket::build()
        .manage(config)
        .mount("/", routes![premium_content])
        .launch()
        .await?;

    Ok(())
}

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