Sign in →
1 min read

Python SDK

Add metering to FastAPI, Django, and Flask with the Python SDK.

Updated 2026-07-29Suggest edits
Get the Python SDK
The Python SDK lives in the public repo github.com/aforoai/SDKs under aforo-metering-sdks/python.
View on GitHub Download ZIPgit clone https://github.com/aforoai/SDKs.git

Install & Quickstart#

Add Aforo metering to your Python service. FastAPI, Django, and Flask middleware plus a manual track() client.

The only hard dependency is httpx>=0.25. AforoClient enqueues into a ring buffer and a background daemon thread flushes batches.

terminal
# Not yet on PyPI — clone and install from source:
git clone https://github.com/aforoai/SDKs.git
cd SDKs/aforo-metering-sdks/python
pip install -e .            # or: pip install -e ".[fastapi]"

Manual Tracking

usage.py
import os
from aforo import AforoClient

client = AforoClient(api_key=os.environ["AFORO_API_KEY"])

client.track(
    customer_id="cust_1",      # who is billed
    metric_name="api_calls",   # what you're metering
    quantity=1,
)

# Force a synchronous flush when you need delivery confirmed:
result = client.flush()        # FlushResult(sent=..., failed=...)
client.shutdown()              # also registered via atexit

Framework Middleware

middleware.py
# FastAPI / Starlette
from aforo.middleware.fastapi import AforoMeteringMiddleware
app.add_middleware(AforoMeteringMiddleware, api_key=os.environ["AFORO_API_KEY"])

# Flask
from aforo.middleware.flask import AforoMetering
AforoMetering(app, api_key=os.environ["AFORO_API_KEY"])

# Django settings.py
MIDDLEWARE = [..., "aforo.middleware.django.AforoMeteringMiddleware"]
AFORO_API_KEY = os.environ["AFORO_API_KEY"]

Each adapter reads the customer id from X-Customer-Id (falling back to X-Api-Key); a request with no resolvable customer id is not metered.

See also