Inject a request dependency
A dependency is a callable whose result FastAPI supplies to an endpoint. It can centralize parsing and validation that several routes share. Declaring it with Depends makes that relationship part of the application's request graph rather than a hidden global lookup.
This example reads a simple demonstration header, not a production identity system. Keep the dependency small and return its validated value. A dependency can raise HTTPException before the endpoint runs, so callers see one consistent error contract. Async dependencies also work in the browser without a thread pool.
from fastapi import FastAPI, Depends
app = FastAPI()
async def currency():
return "EUR"
@app.get("/currency")
async def read(code=Depends(currency)):
return {"code": code}
print(app.openapi()["paths"].keys())Your task
Implement build_app() with an async dependency requiring header X-Demo-Key equal to "lesson". GET /private returns {"ok":True}; absent or incorrect keys return 403 with detail "denied".
The explanation opens here once every check passes.