Documentation

FastAPI

Connect FastAPI

Install access402-fastapi, declare paid routes in Python, and synchronize code-owned payment policy.
Last reviewed August 9, 2026

Requirements

  • Python 3.10 or newer.
  • FastAPI 0.115 or newer.
  • A publicly addressable HTTPS API URL for production.
  • An Access402 project and FastAPI installation.

Create an installation

In Dashboard → Installations, create a FastAPI installation and add every authorized API origin that will use the connection key. For example, add http://localhost:8000, your sandbox origin, and your production origin. Generate a connection key and copy it once.

Install the package

pip install access402-fastapi

Store the installation ID and key in the server environment:

ACCESS402_INSTALLATION_ID=your-installation-uuid
ACCESS402_API_KEY=your-connection-key
ACCESS402_PUBLIC_BASE_URL=https://api.example.com
ACCESS402_MODE=sandbox

Initialize the adapter

Define routes first, then install Access402:

from fastapi import FastAPI
from access402_fastapi import Access402

app = FastAPI()
access402 = Access402.from_env()

@app.get("/premium-data")
@access402.protect(
    price="0.05",
    access_type="per_request",
    description="Premium data for agents",
    discovery_enabled=True,
)
async def premium_data():
    return {"result": "paid data"}

access402.install(app)

Synchronize routes

Start the application. The adapter authenticates, uploads only code-declared paid routes and their policies, and downloads signed configuration. The installation page shows a read-only mirror after synchronization. Undecorated routes remain public; decorated routes fail closed if their authenticated server policy is unavailable.