Registry / web-framework / fastapi-health

fastapi-health

JSON →
library0.4.0pypypi✓ verified 86d ago

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-health
INSTALL
IMPORT
SIG · FASTAPI-HEALTH
F
fastapi-health
web-frameworkpythonv0.4.0
Install
3.8s avg
Import
1233ms
Disk
30MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.4.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 1.298s · 32MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 3.8s · import 1.168s · 32MB
30MB installed
● package 30MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

health
from fastapi_health import health
HealthRoute
This class is deprecated and should not be used.
from fastapi_health import HealthRoute
The `HealthRoute` class was deprecated in version 0.2.0 in favor of the `health()` function, which provides a more flexible and functional approach to defining health checks.

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.

from fastapi import FastAPI, Depends from fastapi_health import health import os app = FastAPI() def get_database_status(): # Simulate a database check db_conn_string = os.environ.get('DATABASE_URL', 'sqlite:///./test.db') # In a real app, this would attempt a connection or query is_connected = db_conn_string is not None # Simplified check return {"database": "online" if is_connected else "offline"} def get_cache_status(): # Simulate a cache check cache_host = os.environ.get('REDIS_HOST', 'localhost') is_reachable = cache_host == 'localhost' # Simplified check return is_reachable # Returns a boolean # Register the health endpoint with multiple conditions app.add_api_route("/health", health([ get_database_status, get_cache_status ])) # To run this example, save it as main.py and run: uvicorn main:app --reload # Then navigate to http://localhost:8000/health
Debug
Known issues
breakingThe `HealthRoute` class was removed/deprecated in version 0.2.0. Users on older versions upgrading to 0.2.0 or newer must migrate to using the `health()` function.
fix
Replace `HealthRoute` usage with `app.add_api_route("/health", health([your_condition_callable]))`. See quickstart for example.
affects: >=0.2.0
gotchaWhen using a mix of conditions that return `bool` and `dict`, if any condition returns `False`, the overall response status will default to `503` (Service Unavailable), even if other conditions return detailed 'online' dictionaries. The JSON body will still contain the results from all conditions.
fix
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.
affects: >=0.2.0
gotchaPerforming synchronous I/O operations (e.g., blocking database calls, network requests) directly within `health` condition callables can block FastAPI's event loop, degrading performance and responsiveness for all other API endpoints.
fix
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.
affects: All
Errors
Common errors & fixes
NameError: name 'HealthRoute' is not defined
Attempting to import or use the `HealthRoute` class after upgrading to `fastapi-health` version 0.2.0 or later, where it was removed/deprecated.
fix
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]))`.
TypeError: 'bool' object is not callable
Passing a boolean value directly to the `conditions` list instead of a callable (function) that returns a boolean or dictionary.
fix
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()])`.
Status code 503 instead of 200, even when some services are reported as 'online' in the health check response.
This often happens when `conditions` list contains a mix of callables where at least one returns `False` (boolean) while others return dictionaries indicating 'online' status. The presence of any `False` condition defaults the HTTP status to `503`.
fix
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.
Upgrade
Version history
0.4.0latest on PyPI · released Aug 20, 2021
Audit
Dependencies
fastapirequiredCore dependency for building web APIs. `fastapi-health` extends its functionality.
Agent activity
22 hits · last 30 days
node
16
OpenAI (training)
2
Amazon
1
Resources
fastapi-health — pip install fastapi-health · libregistry