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",
)
async def report(report_id: str):
    return {"id": report_id, "result": "..."}

access402.install(app)

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

Download files

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

Source Distribution

access402_fastapi-0.3.0.tar.gz (13.2 kB view details)

Uploaded Source

Built Distribution

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

access402_fastapi-0.3.0-py3-none-any.whl (13.3 kB view details)

Uploaded Python 3

File details

Details for the file access402_fastapi-0.3.0.tar.gz.

File metadata

  • Download URL: access402_fastapi-0.3.0.tar.gz
  • Upload date:
  • Size: 13.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for access402_fastapi-0.3.0.tar.gz
Algorithm Hash digest
SHA256 30c35a45a29408d488e2c37bb2aedddfd195988dbce53dc1949a4e8d180c42a4
MD5 28499809a441af295d58f1113475ac82
BLAKE2b-256 74aa433bd85a17d3b9d1767e25a43d1591dbf5b20d2351b9119d393958eb5480

See more details on using hashes here.

Provenance

The following attestation bundles were made for access402_fastapi-0.3.0.tar.gz:

Publisher: publish-fastapi.yml on JonathanRoyere/Access402Dash

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file access402_fastapi-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for access402_fastapi-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f156c78a113340cd88931124159df74eebfcd6e0063c3e435ea2c258e88cbf91
MD5 027d9f2e6a8f034ebabfb1dd3a192418
BLAKE2b-256 7ed6fc3fb8b692fa3119db991b045f874e9d3fab9976f7b1b5eeb20a68aceecb

See more details on using hashes here.

Provenance

The following attestation bundles were made for access402_fastapi-0.3.0-py3-none-any.whl:

Publisher: publish-fastapi.yml on JonathanRoyere/Access402Dash

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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