{"id":"ef76ae82a3cbe001","repo":"tiangolo/fastapi","slug":"x-key-header-invalid","errorCode":null,"errorMessage":"X-Key header invalid","messagePattern":"X-Key header invalid","errorType":"http","errorClass":"HTTPException","httpStatus":400,"severity":"error","filePath":"docs_src/dependencies/tutorial006_an_py310.py","lineNumber":15,"sourceCode":"from typing import Annotated\n\nfrom fastapi import Depends, FastAPI, Header, HTTPException\n\napp = FastAPI()\n\n\nasync def verify_token(x_token: Annotated[str, Header()]):\n    if x_token != \"fake-super-secret-token\":\n        raise HTTPException(status_code=400, detail=\"X-Token header invalid\")\n\n\nasync def verify_key(x_key: Annotated[str, Header()]):\n    if x_key != \"fake-super-secret-key\":\n        raise HTTPException(status_code=400, detail=\"X-Key header invalid\")\n    return x_key\n\n\n@app.get(\"/items/\", dependencies=[Depends(verify_token), Depends(verify_key)])\nasync def read_items():\n    return [{\"item\": \"Foo\"}, {\"item\": \"Bar\"}]\n","sourceCodeStart":1,"sourceCodeEnd":22,"githubUrl":"https://github.com/tiangolo/fastapi/blob/42a41db11f6882807ac3c057b942178d53b97438/docs_src/dependencies/tutorial006_an_py310.py#L1-L22","documentation":"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.","triggerScenarios":"GET /items/ with X-Token correct but X-Key missing or wrong.","commonSituations":"Client satisfies the token but forgets the key; only one of two required headers configured; proxy stripping one header.","solutions":["Send X-Key: fake-super-secret-key in addition to X-Token.","Bundle both headers in a shared request helper.","Audit that all required header dependencies are met."],"exampleFix":"# before\nheaders={\"X-Token\":\"fake-super-secret-token\"}\n# after\nheaders={\"X-Token\":\"fake-super-secret-token\",\"X-Key\":\"fake-super-secret-key\"}","handlingStrategy":"validation","validationCode":"headers = {\"X-Token\": \"fake-super-secret-token\", \"X-Key\": \"fake-super-secret-key\"}\nclient.get(\"/items/\", headers=headers)","typeGuard":"def has_both_headers(h: dict) -> bool:\n    return h.get(\"X-Token\") == \"fake-super-secret-token\" and h.get(\"X-Key\") == \"fake-super-secret-key\"","tryCatchPattern":null,"preventionTips":["Remember verify_key is required in addition to verify_token.","Bundle both headers in a shared helper.","Audit that proxies forward both custom headers."],"tags":["fastapi","authentication","dependencies","http-headers"],"analyzedSha":"42a41db11f6882807ac3c057b942178d53b97438","analyzedAt":"2026-08-04T19:23:32.007Z","schemaVersion":2}