> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/fastapi/fastapi/llms.txt
> Use this file to discover all available pages before exploring further.

# Header

> API reference for the FastAPI Header parameter function

# Header

Declare a header parameter for a path operation. Header parameters are extracted from HTTP request headers.

```python theme={null}
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}
```

## Signature

```python theme={null}
def Header(
    default: Any = Undefined,
    *,
    convert_underscores: bool = True,
    alias: str | None = None,
    title: str | None = None,
    description: str | None = None,
    gt: float | None = None,
    ge: float | None = None,
    lt: float | None = None,
    le: float | None = None,
    min_length: int | None = None,
    max_length: int | None = None,
    pattern: str | None = None,
    examples: list[Any] | None = None,
    deprecated: bool | str | None = None,
    include_in_schema: bool = True,
    json_schema_extra: dict[str, Any] | None = None,
) -> Any
```

## Parameters

<ParamField path="default" type="Any" default="Undefined">
  Default value if the parameter field is not set.
</ParamField>

<ParamField path="convert_underscores" type="bool" default="True">
  Automatically convert underscores to hyphens in the parameter field name. For example, `user_agent` becomes `user-agent`.
</ParamField>

<ParamField path="alias" type="str | None" default="None">
  An alternative name for the parameter field. This will be used to extract the data and for the generated OpenAPI.
</ParamField>

<ParamField path="title" type="str | None" default="None">
  Human-readable title for the parameter.
</ParamField>

<ParamField path="description" type="str | None" default="None">
  Human-readable description for the parameter.
</ParamField>

<ParamField path="gt" type="float | None" default="None">
  Greater than validation. If set, the value must be greater than this. Only applicable to numbers.
</ParamField>

<ParamField path="ge" type="float | None" default="None">
  Greater than or equal validation. If set, the value must be greater than or equal to this. Only applicable to numbers.
</ParamField>

<ParamField path="lt" type="float | None" default="None">
  Less than validation. If set, the value must be less than this. Only applicable to numbers.
</ParamField>

<ParamField path="le" type="float | None" default="None">
  Less than or equal validation. If set, the value must be less than or equal to this. Only applicable to numbers.
</ParamField>

<ParamField path="min_length" type="int | None" default="None">
  Minimum length for strings.
</ParamField>

<ParamField path="max_length" type="int | None" default="None">
  Maximum length for strings.
</ParamField>

<ParamField path="pattern" type="str | None" default="None">
  RegEx pattern for strings.
</ParamField>

<ParamField path="discriminator" type="str | None" default="None">
  Parameter field name for discriminating the type in a tagged union.
</ParamField>

<ParamField path="strict" type="bool | None" default="None">
  If True, strict validation is applied to the field.
</ParamField>

<ParamField path="multiple_of" type="float | None" default="None">
  Value must be a multiple of this. Only applicable to numbers.
</ParamField>

<ParamField path="allow_inf_nan" type="bool | None" default="None">
  Allow `inf`, `-inf`, `nan`. Only applicable to numbers.
</ParamField>

<ParamField path="max_digits" type="int | None" default="None">
  Maximum number of allowed digits for numbers.
</ParamField>

<ParamField path="decimal_places" type="int | None" default="None">
  Maximum number of decimal places allowed for numbers.
</ParamField>

<ParamField path="examples" type="list[Any] | None" default="None">
  Example values for this field.
</ParamField>

<ParamField path="deprecated" type="bool | str | None" default="None">
  Mark this parameter field as deprecated. It will affect the generated OpenAPI (visible at `/docs`).
</ParamField>

<ParamField path="include_in_schema" type="bool" default="True">
  Whether to include this parameter field in the generated OpenAPI.
</ParamField>

<ParamField path="json_schema_extra" type="dict[str, Any] | None" default="None">
  Any additional JSON schema data.
</ParamField>

## Examples

### Basic Header Parameter

```python theme={null}
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}
```

<Note>
  By default, `user_agent` will automatically be converted to `User-Agent` when reading from headers due to `convert_underscores=True`.
</Note>

### Disable Automatic Conversion

```python theme={null}
@app.get("/items/")
async def read_items(
    strange_header: Annotated[str | None, Header(convert_underscores=False)] = None
):
    return {"strange_header": strange_header}
```

### Required Header

```python theme={null}
@app.get("/items/")
async def read_items(
    x_token: Annotated[str, Header()]
):
    return {"X-Token": x_token}
```

<Note>
  A header parameter is required when there is no default value.
</Note>

### Multiple Header Values

```python theme={null}
@app.get("/items/")
async def read_items(
    x_token: Annotated[list[str] | None, Header()] = None
):
    return {"X-Token values": x_token}
```

<Note>
  This allows receiving multiple headers with the same name. For example, multiple `X-Token` headers.
</Note>

### Header with Validation

```python theme={null}
@app.get("/items/")
async def read_items(
    x_token: Annotated[str, Header(min_length=10, max_length=100)]
):
    return {"X-Token": x_token}
```

### Custom Header Name with Alias

```python theme={null}
@app.get("/items/")
async def read_items(
    token: Annotated[str | None, Header(alias="X-API-Key")] = None
):
    return {"token": token}
```

<Note>
  Use `alias` when you want to use a different variable name in Python than the actual header name.
</Note>

## Common Use Cases

### Authentication Token

```python theme={null}
@app.get("/users/me")
async def read_user(
    authorization: Annotated[str, Header()]
):
    return {"token": authorization}
```

### Content Type

```python theme={null}
@app.post("/items/")
async def create_item(
    content_type: Annotated[str | None, Header()] = None
):
    return {"Content-Type": content_type}
```

### Custom Headers

```python theme={null}
@app.get("/items/")
async def read_items(
    x_request_id: Annotated[str | None, Header()] = None,
    x_correlation_id: Annotated[str | None, Header()] = None
):
    return {
        "X-Request-ID": x_request_id,
        "X-Correlation-ID": x_correlation_id
    }
```

<Warning>
  Header names are case-insensitive according to the HTTP specification, but FastAPI will convert them for consistency.
</Warning>
