Skip to main content

Access402 FastAPI adapter

This package protects explicitly declared FastAPI routes with Access402's managed x402 v2 payment service. Routes, USDC prices, access policy, descriptions, and discovery intent live beside the endpoint in Python. Access402 provides signed configuration, x402 challenges, hosted verification and settlement, plan enforcement, discovery publication, and activity logs.

Install locally

pip install -e "./adapters/fastapi[test]"

Connect an application

Create a FastAPI installation in the Access402 dashboard, then configure server-only environment variables:

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

The API key belongs only in the FastAPI server environment. Never expose it in browser code, logs, OpenAPI documents, or a committed .env file. ACCESS402_API_BASE_URL is intentionally optional and should only be overridden for local Access402 backend development.

Create the Access402 manager before declaring paid routes, then install it after all routes:

from fastapi import FastAPI
from access402_fastapi import Access402

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

@app.get("/reports/{report_id}")
@access402.protect(
    price="0.05",
    access_type="per_request",
    description="Read one premium report",
    discovery_enabled=True,
    representative_queries=("Read report 42", "Get a premium research report"),
    capabilities=("x402", "research"),
)
async def report(report_id: str):
    return {"id": report_id, "result": "..."}

access402.install(app)

Live discoverable routes are published in an ARD 0.91 manifest at /.well-known/ard.json; /.well-known/ai-catalog.json is retained as a compatibility path. Each entry includes the route's media type, canonical URL, representative queries, capabilities, and Access402 payment metadata. Sandbox manifests remain valid but intentionally contain no public entries.

On startup, the adapter authenticates the installation, uploads only decorated payment policies, and downloads HMAC-authenticated configuration. The configuration is cached in memory and refreshed periodically; payment requests go directly to the Access402 settlement function. No CDP credential is installed in this package.

The dashboard displays a read-only mirror of code-owned policies and the mode reported by the deployment. The FastAPI environment controls the payment network, while protected routes fail closed if Access402 cannot confirm a matching server-side policy.

Dynamic path routes such as /reports/{report_id} can be protected. Discovery publication for those routes stays disabled until Access402 supports a concrete path-parameter example; publishing a literal template URL would create a broken Bazaar entry.

ACCESS402_PUBLIC_BASE_URL remains required. It is the trusted canonical origin placed in x402 resource URLs and validated against the installation's authorized origins. Inferring it from an incoming Host or forwarding header would allow a spoofed request to alter payment and discovery metadata. Use a separate FastAPI installation for local, staging, and production deployments; replicas of the same deployment may share one installation.

Set ACCESS402_MODE=sandbox for Base Sepolia or ACCESS402_MODE=live for Base mainnet, then restart the deployment. There is no dashboard mode override.

The adapter answers its own 402 responses with Access-Control-Allow-Origin: * by default so agent and browser clients can read the payment challenge. Set ACCESS402_CORS_ALLOW_ORIGIN to the API's exact browser origin when credentials are involved. CORS preflight (OPTIONS) is never paywalled.

Bypasses

There is no header-based administrator bypass. If an application needs trusted internal access, pass a callback that validates the application's real authentication state:

async def trusted_internal_request(scope):
    user = scope.get("state", {}).get("user")
    return bool(user and user.is_admin)

access402 = Access402.from_env(bypass=trusted_internal_request)

Put authentication middleware outside Access402 if the callback depends on middleware-populated state. A spoofable header must never be used as the bypass decision.

Tests

cd adapters/fastapi
python -m pip install -e ".[test]"
pytest

Metadata

Release files for access402-fastapi 0.4.0

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

Source distribution (sdist)

Source distribution for access402-fastapi 0.4.0
File Size Uploaded Provenance
access402_fastapi-0.4.0.tar.gz 15.9 kB With provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for access402-fastapi 0.4.0
File Interpreter ABI Platform
access402_fastapi-0.4.0-py3-none-any.whl Python 3 none any Details

Total release size: 31.7 kB

Release files / access402_fastapi-0.4.0.tar.gz

Download URL access402_fastapi-0.4.0.tar.gz
Size 15.9 kB
Tags Source
SHA-256 checksum
How to use checksums
1391bd1176513237c81dd8da2a27f122f0e515d2dea61eb79c3fd0cc1cb39690
BLAKE2b-256 checksum
How to use checksums
af2cda91df11c7bfa32a2868293c93f0a40c2b3c409e79028d4068b81b8f55da
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 27, 2026.

Transparency log

Release files / access402_fastapi-0.4.0-py3-none-any.whl

Download URL access402_fastapi-0.4.0-py3-none-any.whl
Size 15.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3260e5ef7ab792c76c552b192630698d5dfb5b7c10f8f81201f18558de01a31c
BLAKE2b-256 checksum
How to use checksums
04aea5683cb9b0af486638d71ba76fdeb958eb67cc9be71d0900fc2e74617a43
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 27, 2026.

Transparency log

Security for access402-fastapi 0.4.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for access402_fastapi-0.4.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 27, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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