tiangolo/fastapi · error · HTTPException

X-Key header invalid

Error message

X-Key header invalid

What it means

Raised by the verify_key dependency (Annotated variant) when X-Key != "fake-super-secret-key", returning HTTP 400. It is the second of two header-based guards on GET /items/, demonstrating stacking multiple dependencies. Unlike verify_token it returns x_key, illustrating that dependencies may yield reusable values.

Source

Thrown at docs_src/dependencies/tutorial006_an_py310.py:15

from typing import Annotated

from fastapi import Depends, FastAPI, Header, HTTPException

app = FastAPI()


async def verify_token(x_token: Annotated[str, Header()]):
    if x_token != "fake-super-secret-token":
        raise HTTPException(status_code=400, detail="X-Token header invalid")


async def verify_key(x_key: Annotated[str, Header()]):
    if x_key != "fake-super-secret-key":
        raise HTTPException(status_code=400, detail="X-Key header invalid")
    return x_key


@app.get("/items/", dependencies=[Depends(verify_token), Depends(verify_key)])
async def read_items():
    return [{"item": "Foo"}, {"item": "Bar"}]

View on GitHub (pinned to 42a41db11f)

Solutions

  1. Send X-Key: fake-super-secret-key in addition to X-Token.
  2. Bundle both headers in a shared request helper.
  3. Audit that all required header dependencies are met.

Example fix

# before
headers={"X-Token":"fake-super-secret-token"}
# after
headers={"X-Token":"fake-super-secret-token","X-Key":"fake-super-secret-key"}
Defensive patterns

Strategy: validation

Validate before calling

headers = {"X-Token": "fake-super-secret-token", "X-Key": "fake-super-secret-key"}
client.get("/items/", headers=headers)

Type guard

def has_both_headers(h: dict) -> bool:
    return h.get("X-Token") == "fake-super-secret-token" and h.get("X-Key") == "fake-super-secret-key"

Prevention

When it happens

Trigger: GET /items/ with X-Token correct but X-Key missing or wrong.

Common situations: Client satisfies the token but forgets the key; only one of two required headers configured; proxy stripping one header.

Related errors


AI-assisted analysis of tiangolo/fastapi@42a41db11f (2026-08-04). Data as JSON: /data/errors/ef76ae82a3cbe001.json. Report an issue: GitHub.