Skip to content

FastAPI Dependencies

fasthx.DependsHXRequest: TypeAlias = Request | None module-attribute

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: Request, hx_request: Annotated[str | None, Header()] = None
) -> RequestAlias | 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