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.

Example interface

fastapi-health-check example interface

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)

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

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.3.0.tar.gz (918.1 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.3.0-py3-none-any.whl (21.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fastapi_ht-0.3.0.tar.gz
  • Upload date:
  • Size: 918.1 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.3.0.tar.gz
Algorithm Hash digest
SHA256 27451ef96ed9353eef2241a197e69011f97450b656cd6f2473aca1616f5566d5
MD5 ca9019d76efffaf1f7afce4228820560
BLAKE2b-256 4c77b46e42b72bbe2e09eb39d56ad2c4521cff0e45ad9fc48b31021a763db1b5

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastapi_ht-0.3.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.3.0-py3-none-any.whl.

File metadata

  • Download URL: fastapi_ht-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 21.9 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.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 00fd566a85963332562faf586346663e1fdb116e3150347301530634da9102c9
MD5 28c63758cd1a8a3fc810fb09aa6ce6a2
BLAKE2b-256 3fdc858f44241c15ababfdfbc2644d6d43a5c5b8334398014ac8389be5f278fa

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastapi_ht-0.3.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

0.5.0

2 files

0.4.0

2 files

This release

0.3.0 This release

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