Skip to main content

a402 Python SDK

ASGI middleware that gates FastAPI/Starlette endpoints behind a402 HTTP payments. The seller never handles keys or crypto — the facilitator does.

Install

pip install a402
# With FastAPI extras:
pip install "a402[fastapi]"

Usage

import os
from fastapi import FastAPI
from a402 import A402Middleware, A402Config

app = FastAPI()

app.add_middleware(
    A402Middleware,
    # facilitator_url is optional — defaults to a402's hosted facilitator.
    # Set it only to self-host or point at a staging facilitator.
    config=A402Config(api_key=os.environ["A402_KEY"]),
    routes=[
        {"method": "GET", "path": "/v1/financials", "price": "$0.05"},
        {"path": "/v1/quote", "price": "$0.01"},  # method defaults to GET
    ],
)

@app.get("/v1/financials")
async def get_financials():
    return {"ticker": "NVDA", "revenue": "$60.9B"}

@pay decorator

Attach pricing metadata to a route function. Must still be listed in the routes dict.

from a402 import pay

@app.get("/v1/report")
@pay("$0.10")
async def get_report():
    return {"report": "..."}

How it works

  1. A request arrives with no X-PAYMENT header. The middleware calls POST /facilitator/challenge with the seller's API key and route price. The 402 response contains an accepts array describing the EIP-3009 USDC authorization the client must sign.

  2. The client signs the authorization off-chain and retries with an X-PAYMENT header (base64-encoded JSON).

  3. The middleware decodes the header and calls POST /facilitator/settle. On success (HTTP 200) the real handler runs and the response carries X-PAYMENT-RESPONSE (base64 settlement receipt). On 402/403 the status and body are forwarded to the client.

The settlement uses EIP-3009 (transferWithAuthorization) — the on-chain transfer is confirmed before the 200 is returned, so a 200 from the middleware means funds are settled on the source chain. Cross-chain consolidation via CCTP may still be in progress (check mintTxHash).

Route config keys

Key Type Default
price "$0.05" or 0.05 required
description str "Access to METHOD /path"
resource str "METHOD /path"

Metadata

Release files for a402 0.1.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 a402 0.1.0
File Size Uploaded Provenance
a402-0.1.0.tar.gz 9.6 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for a402 0.1.0
File Interpreter ABI Platform
a402-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 15.9 kB

Release files / a402-0.1.0.tar.gz

Download URL a402-0.1.0.tar.gz
Size 9.6 kB
Tags Source
SHA-256 checksum
How to use checksums
527f34807cca4e286899bd06075564e6113a59ac6d9aab81db592c8bc9369b21
BLAKE2b-256 checksum
How to use checksums
b888f794fcae87abd0d51e32c2a1aacf7c831a7811536298c7649f7cb869c27a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.5

Release files / a402-0.1.0-py3-none-any.whl

Download URL a402-0.1.0-py3-none-any.whl
Size 6.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
38672db98ca53f38fa20de00d07d49c0639b6c21b9c2387a6432fbab265406c7
BLAKE2b-256 checksum
How to use checksums
27787aeb36b1aea4a10e5bad0f73e23998feb4ac08009837dd4117f0f787b2e3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.5

Security for a402 0.1.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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

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.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

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