Skip to main content

402fly-fastapi

FastAPI middleware and decorators for adding X402 payment requirements to API endpoints.

Overview

The 402fly-fastapi package makes it easy to add payment requirements to your FastAPI endpoints. Simply decorate endpoints with payment requirements, and the middleware handles payment verification automatically.

Features

  • Easy-to-use decorators for payment-required endpoints
  • Dependency injection support for accessing payment details
  • Automatic 402 Payment Required response generation
  • Configurable payment amounts and descriptions
  • Seamless integration with existing FastAPI applications

Installation

pip install 402fly-fastapi

Quick Start

1. Initialize X402 Configuration

from fastapi import FastAPI
from 402fly_fastapi import Fly402Config, init_fly402
import os

# Initialize 402 configuration
config = Fly402Config(
    payment_address=os.getenv("FLY402_PAYMENT_ADDRESS"),
    token_mint=os.getenv("FLY402_TOKEN_MINT"),
    network=os.getenv("FLY402_NETWORK", "solana-devnet"),
    rpc_url=os.getenv("FLY402_RPC_URL"),
)
init_fly402(config)

app = FastAPI()

2. Add Payment Requirements with Decorator

from fastapi import Request
from 402fly_fastapi import payment_required

@app.get("/premium-data")
@payment_required(
    amount="0.10",
    description="Access to premium market data"
)
async def get_premium_data(request: Request):
    """Endpoint requires 0.10 USDC payment"""
    return {
        "data": "This is premium content",
        "market_data": {"price": 100.50, "volume": 1_000_000}
    }

3. Access Payment Details with Dependency Injection

from fastapi import Depends
from 402fly_fastapi import verify_payment_factory
from 402fly_core import PaymentAuthorization

@app.get("/expensive-data")
async def get_expensive_data(
    payment: PaymentAuthorization = Depends(
        verify_payment_factory(
            amount="1.00",
            description="Access to expensive AI model inference"
        )
    )
):
    """Access payment details in your endpoint"""
    return {
        "data": "AI-generated content",
        "payment_id": payment.payment_id,
        "amount_paid": payment.actual_amount,
        "transaction_hash": payment.transaction_hash,
    }

Usage Patterns

Pattern 1: Simple Decorator (Recommended)

Best for endpoints that don't need to access payment details:

@app.get("/data")
@payment_required(amount="0.05", description="Data access")
async def get_data(request: Request):
    return {"data": "content"}

Pattern 2: Dependency Injection

Best when you need to access payment details (transaction hash, amount, etc.):

@app.get("/data")
async def get_data(
    payment: PaymentAuthorization = Depends(
        verify_payment_factory(amount="0.05", description="Data access")
    )
):
    return {
        "data": "content",
        "payment_id": payment.payment_id,
        "tx_hash": payment.transaction_hash,
    }

Pattern 3: Dynamic Pricing

Adjust payment requirements based on parameters:

@app.get("/tiered-data/{tier}")
@payment_required(amount="0.05", description="Tiered data access")
async def get_tiered_data(request: Request, tier: str):
    """Payment required regardless of tier"""
    tier_data = {
        "basic": {"quality": "720p"},
        "premium": {"quality": "4K"},
    }
    return {"tier": tier, "data": tier_data.get(tier)}

Complete Example

from fastapi import FastAPI, Depends, Request
from 402fly_fastapi import (
    payment_required,
    verify_payment_factory,
    Fly402Config,
    init_fly402
)
from 402fly_core import PaymentAuthorization
import os

# Initialize 402 config
config = Fly402Config(
    payment_address=os.getenv("FLY402_PAYMENT_ADDRESS"),
    token_mint=os.getenv("FLY402_TOKEN_MINT"),
    network=os.getenv("FLY402_NETWORK", "solana-devnet"),
)
init_fly402(config)

app = FastAPI()

@app.get("/")
async def root():
    """Public endpoint - no payment required"""
    return {"message": "Welcome to the API"}

@app.get("/premium")
@payment_required(amount="0.10", description="Premium data")
async def premium(request: Request):
    """Protected endpoint - payment required"""
    return {"data": "premium content"}

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8000)

Configuration

Fly402Config Options

  • payment_address: Your Solana wallet address for receiving payments
  • token_mint: Token mint address (e.g., USDC on Solana)
  • network: Network name (e.g., "solana-devnet", "solana-mainnet")
  • rpc_url: Solana RPC endpoint URL

Documentation

For complete API reference and guides, see:

Testing

pytest tests/

License

MIT License - See LICENSE file for details.

Metadata

Release files for fly402fastapi 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fly402fastapi 0.1.1
File Size Uploaded
fly402fastapi-0.1.1.tar.gz 7.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fly402fastapi 0.1.1
File Interpreter ABI Platform
fly402fastapi-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 17.9 kB

Release files / fly402fastapi-0.1.1.tar.gz

Download URL fly402fastapi-0.1.1.tar.gz
Size 7.5 kB
Tags Source
SHA-256 checksum
How to use checksums
9610868236a6d2decf3a008f614bdbe2d30f04a4cfa69376b446b0ba3019c1ce
BLAKE2b-256 checksum
How to use checksums
6d03dfc5fc2636fc26a255da41f7a797953b2e41ae9f3695cc5fa52389f5c5ed
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.7

Release files / fly402fastapi-0.1.1-py3-none-any.whl

Download URL fly402fastapi-0.1.1-py3-none-any.whl
Size 10.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b880099b2296940c959bbcefdac4b0403b9998e100dda4a4c1bbfc2cc6d16b99
BLAKE2b-256 checksum
How to use checksums
afc86049ea8e0afe4808116b629b5375dccba2b81b5507756b57371035184b27
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.7

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page