Skip to main content

Crate openlibx402_actix

Crate openlibx402_actix 

Source
Expand description

§OpenLibx402 Actix Web

Actix Web framework integration for the X402 payment protocol.

This library provides middleware and extractors to protect Actix Web endpoints with payment requirements using the X402 protocol.

§Example

use actix_web::{web, App, HttpResponse, HttpServer};
use openlibx402_actix::{PaymentExtractor, X402Config, X402State};

async fn premium_data(auth: PaymentExtractor) -> HttpResponse {
    HttpResponse::Ok().json(serde_json::json!({
        "message": "Premium content!",
        "payment_id": auth.authorization.payment_id
    }))
}

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

    HttpServer::new(move || {
        App::new()
            .app_data(web::Data::new(X402State { config: config.clone() }))
            .route("/premium", web::get().to(premium_data))
    })
    .bind(("127.0.0.1", 8080))?
    .run()
    .await
}

Structs§

PaymentExtractor
Extractor that enforces payment requirements
PaymentRequirement
Payment requirement configuration for a specific endpoint
X402Config
Global X402 configuration
X402State
Application state containing X402 configuration

Enums§

PaymentError
Error type for payment operations

Functions§

create_payment_request
Create a payment request for an endpoint
payment_required_response
Helper function to create a 402 Payment Required response