{"id":"bfc9962b922f5972","repo":"tiangolo/fastapi","slug":"item-not-found","errorCode":null,"errorMessage":"Item not found","messagePattern":"Item not found","errorType":"http","errorClass":"HTTPException","httpStatus":404,"severity":"error","filePath":"docs_src/app_testing/app_b_an_py310/main.py","lineNumber":27,"sourceCode":"    \"foo\": {\"id\": \"foo\", \"title\": \"Foo\", \"description\": \"There goes my hero\"},\n    \"bar\": {\"id\": \"bar\", \"title\": \"Bar\", \"description\": \"The bartenders\"},\n}\n\napp = FastAPI()\n\n\nclass Item(BaseModel):\n    id: str\n    title: str\n    description: str | None = None\n\n\n@app.get(\"/items/{item_id}\", response_model=Item)\nasync def read_main(item_id: str, x_token: Annotated[str, Header()]):\n    if x_token != fake_secret_token:\n        raise HTTPException(status_code=400, detail=\"Invalid X-Token header\")\n    if item_id not in fake_db:\n        raise HTTPException(status_code=404, detail=\"Item not found\")\n    return fake_db[item_id]\n\n\n@app.post(\"/items/\")\nasync def create_item(item: Item, x_token: Annotated[str, Header()]) -> Item:\n    if x_token != fake_secret_token:\n        raise HTTPException(status_code=400, detail=\"Invalid X-Token header\")\n    if item.id in fake_db:\n        raise HTTPException(status_code=409, detail=\"Item already exists\")\n    fake_db[item.id] = item.model_dump()\n    return item\n","sourceCodeStart":9,"sourceCodeEnd":39,"githubUrl":"https://github.com/tiangolo/fastapi/blob/42a41db11f6882807ac3c057b942178d53b97438/docs_src/app_testing/app_b_an_py310/main.py#L9-L39","documentation":"Raised by GET /items/{item_id} after auth passes, when the requested item_id is not a key in the in-memory fake_db dict (which only contains \"foo\" and \"bar\"). FastAPI returns HTTP 404 with detail \"Item not found\". This is the standard resource-not-found response for a lookup miss.","triggerScenarios":"GET /items/{item_id} with a valid X-Token but an item_id not present in fake_db, e.g. /items/missing or /items/123.","commonSituations":"Requesting an item that was never created; a typo in the id; requesting an item that was deleted; using an id from a different environment's database.","solutions":["Use a known id (\"foo\" or \"bar\") or create the item first via POST /items/.","Check the 404 response and surface a user-facing \"not found\" message rather than retrying blindly.","Confirm item_id casing and that no leading/trailing whitespace was added."],"exampleFix":"# before\nclient.get(\"/items/missing\", headers={\"X-Token\":\"coneofsilence\"})\n# after\nclient.post(\"/items/\", json={\"id\":\"missing\",\"title\":\"Missing\"}, headers={\"X-Token\":\"coneofsilence\"})","handlingStrategy":"validation","validationCode":"# List valid ids before requesting\nvalid_ids = {\"foo\", \"bar\"}\nif item_id in valid_ids:\n    client.get(f\"/items/{item_id}\", headers=auth)","typeGuard":"def item_exists(item_id: str, known: set[str]) -> bool:\n    return item_id in known","tryCatchPattern":"resp = client.get(f\"/items/{item_id}\", headers=auth)\nif resp.status_code == 404:\n    # item does not exist; handle gracefully\n    ...","preventionTips":["Discover ids via a list endpoint before fetching.","Normalize ids (strip whitespace) before use.","Treat 404 as a normal control-flow outcome, not a crash."],"tags":["fastapi","not-found","httpexception","rest"],"analyzedSha":"42a41db11f6882807ac3c057b942178d53b97438","analyzedAt":"2026-08-04T19:23:32.007Z","schemaVersion":2}