Python / Flask

Instrument Flask APIs.

Capture resolved Flask URL rules, customer context and request outcomes through native lifecycle hooks.

Flask 2.3–3.xSDK 0.1Production ready
01

Package

Install one adapter.

Python Core and a compatible Flask version are installed automatically.

Terminalbash
pip install apirelio-flask
02

Flask extension

Register Apirelio during application setup.

app.pypython
import os

from flask import Flask
from apirelio_flask import install_apirelio

app = Flask(__name__)

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 stable identities.

Authenticate the request before Flask runs after_request, then resolve the current account and consuming API client from g or another request-local source.

Identity resolverspython
from flask import g

install_apirelio(
    app,
    # ...connection options...
    resolve_customer=lambda request: {
        "id": str(g.account.id),
        "name": g.account.name,
        "plan": g.account.plan,
    } if getattr(g, "account", None) else None,
    resolve_application=lambda request: {
        "id": str(g.api_client.id),
        "name": g.api_client.name,
    } if getattr(g, "api_client", None) else None,
)
04

Safe enrichment

Add bounded operational context.

Only scalar keys included in metadata_keys are retained by Python Core. Use stable error codes rather than exception messages.

View contextpython
from apirelio_flask import get_request_context

@app.post("/api/invoices")
def create_invoice():
    context = get_request_context()
    if context:
        context.add_metadata({"region": "eu-central"})
        context.set_error_code("PAYMENT_REQUIRED")
    return {"accepted": True}, 202
05

Lifecycle

Resolved, asynchronous and fail-safe.

The integration records the final status, duration and matched Flask URL rule. Events enter a bounded background queue; resolver and delivery 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.