{"record":{"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/3e8d1526d83a90aaf7d6eb6dc682bf150f180b25/docs_src/dependencies/tutorial006_an_py310.py#L1-L22","documentation":"Raised (400) by the verify_key dependency on GET /items/. The X-Key header must equal 'fake-super-secret-key'. Unlike verify_token, verify_key returns x_key (a usable dependency value), but the route does not consume it. verify_key runs after verify_token, so the token check must pass first.","triggerScenarios":"GET /items/ with a correct X-Token but a missing/wrong X-Key header. Because verify_token precedes it, you only reach this line when the token is valid.","commonSituations":"Clients fix the token error and then hit the key error on the next attempt; or a client sends only one of the two required headers.","solutions":["Send X-Key: fake-super-secret-key together with a valid X-Token.","Configure both headers in a shared auth helper on the client.","Store both expected secrets in one config object to keep them in sync."],"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":["Send X-Key whenever you send a valid X-Token.","Pair both headers in a shared auth helper.","Test that both headers are present before release."],"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"}