Skip to main content

z4j-fastapi

PyPI version Python License

The FastAPI framework adapter for z4j.

Adds the z4j agent to a FastAPI app through z4j_lifespan(...) passed to FastAPI(lifespan=...). Register engines by passing their native handles (celery_app=, rq_app=, huey=, and so on); installing an adapter package alone does not select it. Dramatiq is the exception only when its process-global broker already has registered actors.

Compatibility

  • FastAPI 0.109.1+ (no upper cap)
  • Python 3.11+

Pair with an engine adapter (z4j-celery, z4j-rq, z4j-dramatiq, z4j-huey, z4j-arq, z4j-taskiq); each engine adapter carries its own upstream floor.

Full per-adapter matrix at https://z4j.dev/reference/compatibility/.

What it ships

  • One-line install, pass z4j_lifespan(...) to FastAPI(lifespan=...) and the agent connects on the next uvicorn worker boot
  • Explicit engine registration, supports multiple native engine handles in one process; adapter installation without the corresponding handle is skipped
  • Loop-safe TaskIQ discovery, taskiq_broker= attaches z4j middleware; TaskIQ broker startup records the actual owner loop
  • Per-task metadata from supporting engine adapters; z4j-celery, z4j-rq, and z4j-dramatiq expose @z4j_meta rather than this framework package defining one
  • Service-user safe, auto-relocates the local outbound buffer to $TMPDIR/z4j-{uid} when $HOME is unwritable (uvicorn under a service account, distroless images, etc.)

Install

pip install z4j-fastapi z4j-celery z4j-celerybeat

Wire it into your app:

from fastapi import FastAPI
from myproject.celery import app as celery_app
from z4j_fastapi import z4j_lifespan

# reads Z4J_TOKEN, Z4J_HMAC_SECRET, Z4J_BRAIN_URL, Z4J_PROJECT_ID from env
app = FastAPI(lifespan=z4j_lifespan(celery_app=celery_app))

Pass explicit config as kwargs (z4j_lifespan(brain_url=..., token=..., project_id=..., hmac_secret=...)) instead of env vars if you prefer. Apps that already own their lifespan can wrap it via inner_lifespan=; apps that cannot use lifespan at all can call install_z4j(app) instead.

With taskiq_broker=, both installation forms attach z4j's middleware but do not assume that FastAPI's ambient loop owns the supplied broker. TaskIQ's later broker startup binds the real owner loop. Until that binding occurs, TaskIQ submit and result probes fail closed. Attach before host startup; if the broker was already started elsewhere, use the direct adapter API and pass its verified live broker_loop explicitly.

Mint the agent from the dashboard's Agents page and retain both values it shows: the bearer token and the HMAC secret.

Reliability

  • Agent startup and delivery failures are logged and isolated from FastAPI request handlers and worker code; capture hooks make no brain network request inline.
  • Engine event queues and the SQLite outbound buffer are bounded. Queue overflow drops new events and buffer pressure evicts oldest rows; both losses are logged.

Documentation

Full docs at z4j.dev/frameworks/fastapi/.

License

Apache-2.0, see LICENSE.

Links

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

z4j_fastapi-1.10.0.tar.gz (25.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

z4j_fastapi-1.10.0-py3-none-any.whl (21.2 kB view details)

Uploaded Python 3

File details

Details for the file z4j_fastapi-1.10.0.tar.gz.

File metadata

  • Download URL: z4j_fastapi-1.10.0.tar.gz
  • Upload date:
  • Size: 25.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.14.7

File hashes

Hashes for z4j_fastapi-1.10.0.tar.gz
Algorithm Hash digest
SHA256 1024d831d61c7e89d941b3f03cee9a2210f7c21ddf47b155ac2817eb33ab4d0f
MD5 59fcdca8e604d6e212ea631bb91a7e34
BLAKE2b-256 71b2cd0ef375129ce5495984cc4433ae1fe169d474cd6c39ce711a06d1886fed

See more details on using hashes here.

File details

Details for the file z4j_fastapi-1.10.0-py3-none-any.whl.

File metadata

  • Download URL: z4j_fastapi-1.10.0-py3-none-any.whl
  • Upload date:
  • Size: 21.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.14.7

File hashes

Hashes for z4j_fastapi-1.10.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b7e46457bc29e0f4deb16b92afe60bdbd5bf30ef38a89560399283ca9fc4e5bc
MD5 f3b189f236e384c4dd95f33db60c3684
BLAKE2b-256 cfe0beae20fcea5bc7dde45db31767a6c2af795e1daed9940bf364400349596f

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

1.10.0 This release

2 files

1.9.1

2 files

1.9.0

2 files

1.8.0

2 files

1.7.0

2 files

1.6.9

2 files

1.6.8

2 files

1.6.7

2 files

1.6.0

2 files

1.5.0

2 files

1.4.0

2 files

1.3.0

2 files

1.2.0

2 files

1.1.2

2 files

1.1.0

2 files

1.0.4

2 files

1.0.3

1 file

1.0.2

2 files

1.0.1

2 files

1.0.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page