{"record":{"id":"506fb6c7711b1170","repo":"tiangolo/fastapi","slug":"x-key-header-invalid-506fb6","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_py310.py","lineNumber":13,"sourceCode":"from fastapi import Depends, FastAPI, Header, HTTPException\n\napp = FastAPI()\n\n\nasync def verify_token(x_token: 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: 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":20,"githubUrl":"https://github.com/tiangolo/fastapi/blob/3e8d1526d83a90aaf7d6eb6dc682bf150f180b25/docs_src/dependencies/tutorial006_py310.py#L1-L20","documentation":"Legacy-style variant of error 15: verify_key on GET /items/ raises 400 when X-Key != 'fake-super-secret-key'. Runs after verify_token, so it fires only when the token is already valid.","triggerScenarios":"GET /items/ with valid X-Token but wrong/missing X-Key.","commonSituations":"Fixing the token and immediately hitting the key gate; sending only one of the two headers.","solutions":["Send X-Key: fake-super-secret-key alongside a valid X-Token.","Keep both headers in one auth helper.","Move to the Annotated variant."],"exampleFix":"// before\nGET /items/   X-Token: fake-super-secret-token\n// after\nGET /items/   X-Token: fake-super-secret-token   X-Key: fake-super-secret-key","handlingStrategy":"validation","validationCode":"import httpx\nresp = httpx.get('http://localhost:8000/items/', headers={'X-Token': 'fake-super-secret-token', 'X-Key': 'fake-super-secret-key'})","typeGuard":"def is_valid_x_key(value: object) -> bool:\n    return isinstance(value, str) and value == 'fake-super-secret-key'","tryCatchPattern":null,"preventionTips":["Always pair X-Key with a valid X-Token.","Keep both secrets in one config.","Migrate to the Annotated variant."],"tags":["fastapi","authentication","dependency","header","dependencies-tutorial"],"backgroundTag":null,"analyzedSha":"3e8d1526d83a90aaf7d6eb6dc682bf150f180b25","analyzedAt":"2026-08-11T02:34:52.986Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}