1
2
3
4
5
6
7
8
9
10
from fastapi.security import APIKeyHeader

api_key_header = APIKeyHeader(name="api_key")

@app.get("/items/{item_id}", response_model=Item)
def read_item(item_id: int, api_key: str = Depends(api_key_header)):
if api_key != "my_secret_key":
raise HTTPException(status_code=403, detail="Invalid API key")
return items[item_id]