Python / Django

Instrument Django and DRF APIs.

Capture resolved URL patterns, DRF error codes and customer-aware outcomes with fail-safe Django middleware.

Django 4.2–6SDK 0.2Production ready
01

Package

Install one adapter.

Python Core and the compatible Django dependency are installed automatically. Django REST Framework remains optional.

Terminalbash
pip install apirelio-django
02

Middleware

Add Apirelio after authentication.

settings.pypython
# settings.py
import os

MIDDLEWARE = [
    # Authentication must run before the Apirelio resolver.
    "django.contrib.auth.middleware.AuthenticationMiddleware",
    "apirelio_django.ApirelioMiddleware",
]

APIRELIO = {
    "API_KEY": os.environ.get("APIRELIO_API_KEY", ""),
    "ENDPOINT": os.environ.get("APIRELIO_ENDPOINT", "https://apirelio.com"),
    "SERVICE": "billing-api",
    "ENVIRONMENT": "production",
    "RELEASE": os.environ.get("APIRELIO_RELEASE"),
    "INCLUDE_ROUTES": ("/api/**",),
    "EXCLUDE_ROUTES": ("/api/health/", "/api/internal/**"),
    "RESOLVE_CUSTOMER": "accounts.apirelio.resolve_customer",
}
03

B2B context

Resolve a stable customer.

A resolver can be a callable or dotted import path. It runs after the view, receives the completed HttpRequest and must return only bounded customer fields.

Customer resolverpython
# accounts/apirelio.py
def resolve_customer(request):
    account = getattr(request.user, "account", None)
    if account is None:
        return None

    return {
        "id": str(account.id),
        "name": account.name,
        "plan": account.plan,
    }
04

DRF support

No separate package required.

The same middleware instruments Django REST Framework views. For error responses it reads DRF's structured ErrorDetail.code, while never reading or sending the response body itself.

05

Lifecycle

Sync, async and fail-safe.

Synchronous and asynchronous Django handlers are supported. Capture only enqueues into a bounded background worker; process shutdown flushes pending events and analytics failures never alter the response or original exception.

Privacy boundary

Bodies, query strings, credentials, cookies, email addresses and client IP addresses are never included.