Skip to main content

fastapi-health-check logo

fastapi-health-check

FastAPI health checks with separate liveness and readiness probes, a visual status page, and JSON responses.

PyPI Python versions CI Docs

Documentation  ·  Installation  ·  Features  ·  Issues


The /ht diagnostic report for the enterprise example with 14 healthy dependency checks

The built-in /ht diagnostic report — every registered check with its status, message and latency.

Installation

Install with uv:

uv add fastapi-ht

Install with pip:

pip install fastapi-ht

What the library provides

  • A base contract for advanced checks
  • A lightweight registry for collecting and running checks
  • Separate /health/live and /health/ready JSON endpoints
  • A combined /ht endpoint with HTML by default
  • JSON responses when the client sends Accept: application/json
  • A simple way to monitor any custom area of your system

Built-in checks

The package includes AppAliveCheck for application availability and RedisCheck for Redis connectivity.

RedisCheck reuses an async client supplied by the application and executes PING. The core package does not install a Redis client. Install and configure an async client such as redis in the application when this check is needed.

from redis.asyncio import Redis

from fastapi_health_check import HealthRegistry, RedisCheck


redis_client = Redis.from_url(redis_url)
registry = HealthRegistry([RedisCheck(redis_client)])

The default check name is redis. A custom name can distinguish multiple Redis deployments:

registry.register(RedisCheck(session_redis, name="session_cache"))

Redis failures are critical like every health check currently registered in HealthRegistry. Connection errors use a sanitized message and never expose credentials from the underlying client exception.

Databases, queues, external APIs, or any other monitored area are meant to be registered by the user. The package includes AppAliveCheck for application availability and PostgreSQLCheck for PostgreSQL connectivity.

PostgreSQLCheck reuses an async pool supplied by the application and executes SELECT 1. The core package does not install a PostgreSQL driver. Install and configure an async driver such as asyncpg in the application when this check is needed.

import asyncpg

from fastapi_health_check import HealthRegistry, PostgreSQLCheck


pool = await asyncpg.create_pool(database_url)
registry = HealthRegistry([PostgreSQLCheck(pool)])

The default check name is postgresql. A custom name can distinguish multiple databases:

registry.register(PostgreSQLCheck(reporting_pool, name="reporting_database"))

PostgreSQL failures are critical like every health check currently registered in HealthRegistry. Connection errors use a sanitized message and never expose credentials from the underlying driver exception.

Redis, queues, external APIs, or any other monitored area are meant to be registered by the user.

SQLAlchemy

Install the optional SQLAlchemy support:

uv add "fastapi-ht[sqlalchemy]"

SQLAlchemyCheck supports SQLAlchemy >=2.0,<3.0 and accepts an existing Engine, AsyncEngine, sessionmaker, or async_sessionmaker.

from sqlalchemy.ext.asyncio import create_async_engine

from fastapi_health_check import HealthRegistry, SQLAlchemyCheck


engine = create_async_engine(database_url)
registry = HealthRegistry([SQLAlchemyCheck(engine)])

The check executes SELECT 1 through the supplied engine or session factory. Synchronous SQLAlchemy operations run in a worker thread so health checks do not block the application event loop.

Quick start

from fastapi import FastAPI

from fastapi_health_check import AppAliveCheck, HealthRegistry, health_check, install_health_check


app = FastAPI()
registry = HealthRegistry()
registry.register(AppAliveCheck(), readiness=True, liveness=True)
registry.register(health_check("database", lambda: "connection ok"))
registry.register(health_check("redis", lambda: "cache reachable"))

install_health_check(app, registry)

The built-in status page is rendered as a self-contained system diagnostic; no frontend framework or external asset host is required.

This exposes three routes:

  • GET /health/live returns the liveness report as JSON
  • GET /health/ready returns the readiness report as JSON
  • GET /ht keeps the combined status page and content-negotiated JSON response

Liveness and readiness

Liveness answers whether the application process should be restarted. Keep this probe lightweight and independent of databases, caches, external APIs, and other dependencies. A failing liveness check returns 503.

Readiness answers whether the application can serve traffic. Dependency checks belong here so an unavailable dependency returns 503 and the orchestrator can remove the instance from service without restarting it.

Checks belong to readiness by default:

registry.register(health_check("database", check_database))

Assign a check to liveness or both probes with registration options:

registry.register(process_check, readiness=False, liveness=True)
registry.register(AppAliveCheck(), readiness=True, liveness=True)

Probe paths can be configured independently while retaining /ht:

install_health_check(
    app,
    registry,
    path="/status",
    liveness_path="/livez",
    readiness_path="/readyz",
)

For Kubernetes, configure livenessProbe to request /health/live and readinessProbe to request /health/ready. Dependency failures then stop traffic to an unready pod without creating unnecessary restart loops. Kubernetes manifest settings are deployment-specific and outside this library's configuration.

Monitoring custom areas

If you want to monitor anything beyond the built-in app liveness check, the easiest option is the health_check() factory.

You can use it for:

  • databases
  • Redis or cache layers
  • background queues
  • external APIs
  • storage services
  • internal domain-specific dependencies

Synchronous checks

from fastapi_health_check import health_check

database_check = health_check("database", lambda: "connection ok")
redis_check = health_check("redis", lambda: "cache reachable")

Asynchronous checks

from fastapi_health_check import health_check


async def payments_api_check() -> str | None:
    return "payments API available"


payments_check = health_check("payments_api", payments_api_check)

Class-based checks for advanced cases

from fastapi_health_check import HealthCheck


class QueueCheck(HealthCheck):
    default_name = "queue"

    async def check(self) -> str | None:
        return "queue connected"

Use class-based checks when you want:

  • dependency injection through __init__
  • reusable state
  • more structured custom behavior

Local manual testing

The repository includes a local example application at src/examples/basic_app.py.

Run it with:

uv run uvicorn src.examples.basic_app:app --reload

Then open:

  • http://127.0.0.1:8000/ht for the HTML page
  • curl -H "Accept: application/json" http://127.0.0.1:8000/ht for JSON
  • curl http://127.0.0.1:8000/health/live for liveness
  • curl http://127.0.0.1:8000/health/ready for readiness

A larger example lives at src/examples/enterprise_app.py. It registers 14 dependency checks and exposes a small console that can fail, slow down or recover each one, which is what the screenshots above show:

uv run uvicorn src.examples.enterprise_app:app --reload

Download files

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

Source Distribution

fastapi_ht-0.5.0.tar.gz (322.9 kB view details)

Uploaded Source

Built Distribution

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

fastapi_ht-0.5.0-py3-none-any.whl (27.1 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_ht-0.5.0.tar.gz.

File metadata

  • Download URL: fastapi_ht-0.5.0.tar.gz
  • Upload date:
  • Size: 322.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for fastapi_ht-0.5.0.tar.gz
Algorithm Hash digest
SHA256 e446c56430420a3a754580b7f63ef7a38f5a06995abc99f2eb11beebec3cdd9a
MD5 2a4f8c6f658b6283dd9b4b5571a3bfcc
BLAKE2b-256 c9f6e7dd42a3279cb63f3f60a7b710288acb9aaef1b3362c6cd1792ac77b0c51

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastapi_ht-0.5.0.tar.gz:

Publisher: publish.yml on PinnLabs/fastapi-health-check

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fastapi_ht-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: fastapi_ht-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 27.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for fastapi_ht-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 72f99756e82cfdb1ccc754ae538b18585ec757f27d07422c2f2eee4f080ad7fc
MD5 cfc83e0d5deeb4b07d33bc04265a5246
BLAKE2b-256 0b45fd6ddd867aeeb303e0cf64fab32d8e15b8b4cccc72ab53663bd1d2366783

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastapi_ht-0.5.0-py3-none-any.whl:

Publisher: publish.yml on PinnLabs/fastapi-health-check

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

0.5.0 This release

2 files

0.4.0

2 files

0.3.0

2 files

0.2.0

2 files

0.1.1

2 files

0.1.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