Skip to content

baldur.adapters.flask — Flask Adapter

init_flask(app, service_name=None) factory hook. Calls baldur.init() exactly once and registers before_request / after_request handlers that delegate to the framework-free helpers in baldur.api.middleware.

The adapter manages no host-app authentication — your app owns endpoint auth, and Baldur's own admin server is gated by key-based roles. Unlike the Django adapter, it registers no Django permission groups.

See also

Flask quickstart

flask

Flask adapter for Baldur.

Provides: - init_flask(app, service_name=None) — factory hook. Calls baldur.init() exactly once and registers before_request / after_request handlers that delegate to the framework-free helpers in baldur.api.middleware.

Install: pip install baldur-framework[flask]

Example

.. code-block:: python

from flask import Flask
from baldur.adapters.flask import init_flask

app = Flask(__name__)
init_flask(app)

The adapter intentionally stays thin — every decision lives in baldur.api.middleware. The wrapper only translates between Flask's request proxy and Baldur's RequestContext / ResponseContext.

Status: Public

init_flask

init_flask(
    app: Flask,
    service_name: str | None = None,
    rate_limit: int | None = None,
    window_seconds: int | None = None,
) -> None

Initialize Baldur for a Flask app.

Parameters:

Name Type Description Default
app Flask

The Flask application instance.

required
service_name str | None

Optional upstream identity. When supplied, CB pre-flight + post-response observation are enabled. When None, the CB hooks are no-ops (rate limit + backpressure still apply).

None
rate_limit int | None

Per-instance override for the middleware rate limit (requests per window). None falls back to RateLimitSettings.middleware_rate_limit (default 0 = disabled). Pass a positive integer to enable rate limiting only for this Flask app.

None
window_seconds int | None

Per-instance window size override. None falls back to RateLimitSettings.middleware_window_seconds.

None