FastAPI Health is a Python library that provides a straightforward way to implement health check endpoints in FastAPI applications. It is currently at version 0.4.0 and offers dynamic health checks using custom conditions (callables) and handlers. The library is actively maintained with recent updates focusing on flexibility and dependency handling.
pip install fastapi-healthVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to set up a basic health check endpoint using `fastapi-health`. It includes two simulated condition checks: one returning a dictionary for detailed status and another returning a boolean. The `health()` function dynamically creates a route that aggregates the results of these conditions.
Replace `HealthRoute` usage with `app.add_api_route("/health", health([your_condition_callable]))`. See quickstart for example.Be mindful of expected HTTP status codes. If you need a `200` status with some components failing, consider custom `success_handler` and `failure_handler` or ensure all conditions return dictionaries where failure states are explicitly noted within the dictionary, or handle status codes explicitly using `success_status` and `failure_status` parameters.
Ensure that any I/O-bound health checks are `async def` functions and use `await` for their operations. If a synchronous library must be used, wrap it with `run_in_threadpool` or `asyncio.to_thread` to execute it in a separate thread.
Update your code to use the `health()` function instead: `from fastapi_health import health`, and then `app.add_api_route("/health", health([your_condition_callable]))`.Ensure all items in the `conditions` list passed to `health()` are actual callable functions, not their return values. For example, use `health([check_db_status])` instead of `health([check_db_status()])`.
If all services are critical, accept the `503`. If partial failures should yield a `200` with detailed info, ensure all conditions return dictionaries, or implement custom `success_handler` and `failure_handler` to control the status code and response body more precisely. Alternatively, set `failure_status=200` if you always want a 200 response.