Access402 FastAPI adapter
This package protects FastAPI routes with Access402's managed x402 v2 payment service. Route shapes and OpenAPI schemas come from the running application; pricing, enablement, sandbox/live mode, reusable access, and discovery remain dashboard settings.
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_ALLOW_LIVE=false
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.
Define routes first, then install Access402:
from fastapi import FastAPI
from access402_fastapi import Access402
app = FastAPI()
@app.get("/reports/{report_id}")
async def report(report_id: str):
return {"id": report_id, "result": "..."}
access402 = Access402.from_env()
access402.install(app)
On its first request, the adapter authenticates the installation, uploads a compact route catalog, and downloads an 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.
After the first catalog sync, open the installation's Manage API routes screen in the dashboard to choose routes, prices, access policies, environment, and Bazaar publication.
Dynamic path routes such as /reports/{report_id} can be protected. Discovery publication for those routes stays disabled until Access402 supports a dashboard-provided concrete path-parameter example; publishing a literal template URL would create a broken Bazaar entry.
Live mode requires both the dashboard toggle and ACCESS402_ALLOW_LIVE=true. This second switch is a deployment safety ceiling, not another credential.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file access402_fastapi-0.1.0.tar.gz.
File metadata
- Download URL: access402_fastapi-0.1.0.tar.gz
- Upload date:
- Size: 11.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4ae96870e6381ed5c5c570d189aee28c8821be46747a3007315d02e9fe589dab
|
|
| MD5 |
a6dd9d6ad3c41fa76a3af89af5eeb8c9
|
|
| BLAKE2b-256 |
d0e82f8aa658b33cc212dcc31369e71d99d9738000f3a0737670cdc5d178f6a6
|
Provenance
The following attestation bundles were made for access402_fastapi-0.1.0.tar.gz:
Publisher:
publish-fastapi.yml on JonathanRoyere/Access402Dash
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
access402_fastapi-0.1.0.tar.gz -
Subject digest:
4ae96870e6381ed5c5c570d189aee28c8821be46747a3007315d02e9fe589dab - Sigstore transparency entry: 2401364219
- Sigstore integration time:
-
Permalink:
JonathanRoyere/Access402Dash@85c4162f130a5418bc66047bebec6277b88f28f2 -
Branch / Tag:
refs/tags/fastapi-v0.1.0 - Owner: https://github.com/JonathanRoyere
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-fastapi.yml@85c4162f130a5418bc66047bebec6277b88f28f2 -
Trigger Event:
release
-
Statement type:
File details
Details for the file access402_fastapi-0.1.0-py3-none-any.whl.
File metadata
- Download URL: access402_fastapi-0.1.0-py3-none-any.whl
- Upload date:
- Size: 11.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
66073a15fbe229cfd86aa9247b21847538a13daa6a410fdeb1544a01dea826b6
|
|
| MD5 |
3b2320af7de61d28389d64bb6455d5a0
|
|
| BLAKE2b-256 |
d34aaa4a1c6b693afe86f5689e1b5ba762152e99a79d81dfbbc7d4b9693f8363
|
Provenance
The following attestation bundles were made for access402_fastapi-0.1.0-py3-none-any.whl:
Publisher:
publish-fastapi.yml on JonathanRoyere/Access402Dash
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
access402_fastapi-0.1.0-py3-none-any.whl -
Subject digest:
66073a15fbe229cfd86aa9247b21847538a13daa6a410fdeb1544a01dea826b6 - Sigstore transparency entry: 2401364347
- Sigstore integration time:
-
Permalink:
JonathanRoyere/Access402Dash@85c4162f130a5418bc66047bebec6277b88f28f2 -
Branch / Tag:
refs/tags/fastapi-v0.1.0 - Owner: https://github.com/JonathanRoyere
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-fastapi.yml@85c4162f130a5418bc66047bebec6277b88f28f2 -
Trigger Event:
release
-
Statement type: