01
Package
Install one adapter.
Python Core is installed automatically as a dependency.
Terminalbash
pip install apirelio-fastapi02
ASGI middleware
Register Apirelio before startup.
main.pypython
import os
from fastapi import FastAPI
from apirelio_fastapi import install_apirelio
app = FastAPI()
install_apirelio(
app,
api_key=os.environ.get("APIRELIO_API_KEY", ""),
endpoint=os.environ.get("APIRELIO_ENDPOINT", "https://apirelio.com"),
service="billing-api",
environment="production",
include_routes=("/api/**",),
exclude_routes=("/api/health", "/api/internal/**"),
)03
B2B context
Resolve a stable customer.
Run authentication middleware first and expose the current account through request.state. The resolver executes after route handling.
Customer resolverpython
install_apirelio(
app,
# ...connection options...
resolve_customer=lambda request: {
"id": str(request.state.user.company_id),
"name": request.state.user.company_name,
"plan": request.state.user.company_plan,
} if hasattr(request.state, "user") else None,
)04
Lifecycle
Automatic and fail-safe.
The middleware records the final status, duration and framework route template. Events enter a bounded background queue, and FastAPI shutdown flushes pending batches. Analytics failures never replace the original response or exception.
Privacy boundary
Request and response bodies, query strings, credentials, cookies and client IP addresses are never included.