Python SDK

Flask, FastAPI, Django, and Starlette-compatible ASGI apps.

Add Nolax to a Flask, FastAPI or Django app with a few lines. Install only the extras you need.

Package: nolax-sdk 1.1

Ingest contract: v1.1

Supported frameworks

FrameworkStatus
FlaskSupported
FastAPISupported
DjangoSupported
Starlette (bare)Supported

Install

pip install "nolax-sdk[flask]"

Flask example

python
import os
from flask import Flask
from nolax import NolaxFlask

app = Flask(__name__)
nolax = NolaxFlask(api_key=os.environ['NOLAX_API_KEY'], environment='production')
nolax.init_app(app)

FastAPI example

python
from fastapi import FastAPI
from nolax import NolaxFastAPI

app = FastAPI()
app.add_middleware(NolaxFastAPI, api_key=os.environ['NOLAX_API_KEY'], environment='production')

Django example

Add the middleware to settings.py and set the key and environment. Every other option is a setting named NOLAX_ plus the option name in capitals.

python
# settings.py
MIDDLEWARE = [
  "django.middleware.security.SecurityMiddleware",
  "django.contrib.sessions.middleware.SessionMiddleware",
  # ... other middleware ...
  "nolax.middleware.NolaxDjango",
]

NOLAX_API_KEY = os.environ.get("NOLAX_API_KEY")
NOLAX_ENVIRONMENT = "production"

# Optional
NOLAX_BATCH_SIZE = 100
NOLAX_FLUSH_INTERVAL_MS = 2000

The Django middleware works from SDK 1.1. Earlier versions could not start it.

Config options

Flask and FastAPI take these as keyword arguments. Django reads them from settings.

OptionTypeDefaultNotes
api_keystrrequiredProject API key
environmentstrrequireddevelopment, staging or production
endpointstrhttps://server.nolax.net/ingestIngest URL, for local testing
batch_sizeint100Events that trigger a flush
flush_interval_msint2000Milliseconds between timed flushes
max_queue_sizeint10000Events held in memory. The oldest are dropped first
max_in_flightint4Accepted for parity with the other SDKs. Python sends from one thread
releasestrNoneRelease identifier, up to 64 characters
servicestrNoneService name, up to 64 characters
consumercallableNoneTakes the request, returns a consumer identifier
capture_ipboolTrueSend the client IP address
capture_user_agentboolTrueSend the User-Agent header
ip_headerstrNoneHeader that holds the client IP behind a proxy, for example x-forwarded-for
ignore_pathslist[]Strings or compiled patterns. Matching requests are never recorded
ignore_methodslist[]Methods that are never recorded
before_sendcallableNoneChange an event, or return None to drop it
dry_runboolFalseLog each batch with the key masked and send nothing. Also NOLAX_DRY_RUN=1
debugboolFalseVerbose logging through the nolax logger
on_errorcallableNoneCalled when a batch is dropped

Flushing on shutdown

Call flush() on the Flask or FastAPI integration object to send queued events before the process exits. The SDK also flushes when the interpreter shuts down. It restarts its timer in worker processes created by fork, so Gunicorn and uWSGI workers keep sending.

Troubleshooting

  • No events in FastAPI: call app.add_middleware(...) before the first request arrives.
  • No events in Django: check that NOLAX_API_KEY, NOLAX_ENVIRONMENT and the middleware are in settings.py.
  • payloadSize is 0 when the response has no Content-Length header.
  • ip shows your proxy: set ip_header to x-forwarded-for, and make sure only your proxy can set that header.

See also Batching and Retries and What the SDK Sends.