Skip to main content

FastAPI middleware and decorators for the 402fly payment protocol

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fly402fastapi-0.1.1.tar.gz (7.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fly402fastapi-0.1.1-py3-none-any.whl (10.4 kB view details)

Uploaded Python 3

File details

Details for the file fly402fastapi-0.1.1.tar.gz.

File metadata

  • Download URL: fly402fastapi-0.1.1.tar.gz
  • Upload date:
  • Size: 7.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for fly402fastapi-0.1.1.tar.gz
Algorithm Hash digest
SHA256 9610868236a6d2decf3a008f614bdbe2d30f04a4cfa69376b446b0ba3019c1ce
MD5 025152378c1e960332bc2761fb4803cc
BLAKE2b-256 6d03dfc5fc2636fc26a255da41f7a797953b2e41ae9f3695cc5fa52389f5c5ed

See more details on using hashes here.

File details

Details for the file fly402fastapi-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: fly402fastapi-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 10.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for fly402fastapi-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b880099b2296940c959bbcefdac4b0403b9998e100dda4a4c1bbfc2cc6d16b99
MD5 6c3460d9d0e3c07c1954063417bee90b
BLAKE2b-256 afc86049ea8e0afe4808116b629b5375dccba2b81b5507756b57371035184b27

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page