Inspect request headers
Read client metadata such as User-Agent from incoming requests.
FastAPI · Function
Header declares an HTTP request-header parameter for a FastAPI path operation or dependency. It supports defaults, validation, metadata, aliases, and automatic underscore-to-hyphen conversion.
Header creates a request-parameter declaration for extracting and documenting an HTTP header in a path operation or dependency. FastAPI maps underscores in the Python parameter name to hyphens in the header name by default, while validation and metadata options configure the accepted value and generated schema.
Header(default=Undefined, *, default_factory=_Unset, alias=None, alias_priority=_Unset, validation_alias=None, serialization_alias=None, convert_underscores=True, title=None, description=None, gt=None, ge=None, lt=None, le=None, min_length=None, max_length=None, pattern=None, regex=None, discriminator=None, strict=_Unset, multiple_of=_Unset, allow_inf_nan=_Unset, max_digits=None, decimal_places=None, examples=None, example=_Unset, openapi_examples=None, deprecated=None, include_in_schema=True, json_schema_extra=None, **extra)| Name | Type | Description |
|---|---|---|
default | Any | The default value used when the parameter field is not set. |
default_factory | Callable[[], Any] | None | A callable that generates the default value. |
alias | str | None | An alternate external name for the parameter. |
convert_underscores | bool | Whether underscores in the Python name are converted to hyphens. |
**extra | Any | Validation, schema, and documentation options accepted by the request parameter declaration. |
Any
Annotated.from fastapi import Header.user_agent normally targets the user-agent header; disable this mapping with convert_underscores=False when required.from typing import Annotated
from fastapi import FastAPI, Header
app = FastAPI()
@app.get("/items/")
async def read_items(user_agent: Annotated[str | None, Header()] = None):
return {"User-Agent": user_agent}
Declares an optional request header and returns its value.
Read client metadata such as User-Agent from incoming requests.
Require or validate tokens and custom client headers.
Publish header requirements and constraints in generated OpenAPI documentation.
Header declares a path-operation or dependency parameter whose value comes from an HTTP request header. It also supplies validation and OpenAPI metadata through the common parameter options.
Follow-up: How does a snake_case Python name become an HTTP header name? By default, FastAPI converts underscores to hyphens; set convert_underscores=False to preserve underscores.
The default is Undefined, so a header can be required when no default or optional type/default is supplied. A default value or None makes the declaration optional according to the annotation and default.
Follow-up: What happens if Header is omitted entirely? FastAPI interprets the parameter as a query parameter, not a header.
The function exposes convert_underscores=True by default, along with options such as alias, length and numeric constraints, regular-expression patterns, examples, deprecation, and schema inclusion. These configure extraction, validation, and documentation.
Follow-up: When would alias be useful? Use it when the external header name should differ from the Python parameter name or automatic underscore conversion.
QueryPathCookieParamAnnotated