Skip to content

FastAPI Dependencies

fasthx.DependsHXRequest = Annotated[Request | None, Depends(get_hx_request)] module-attribute

Annotated type (dependency) for get_hx_request() for FastAPI.

fasthx.get_hx_request(request, hx_request=None)

FastAPI dependency that returns the current request if it is an HTMX one, i.e. it contains an "HX-Request: true" header.

Source code in fasthx/dependencies.py
def get_hx_request(
    request: FARequest, hx_request: Annotated[str | None, Header()] = None
) -> Request | None:
    """
    FastAPI dependency that returns the current request if it is an HTMX one,
    i.e. it contains an `"HX-Request: true"` header.
    """
    return request if hx_request == "true" else None