Verify the API key in request header (for use as Depends). Raises: HTTPException 401 if key is missing or invalid
(
x_api_key: str = Header(None, alias="X-API-Key"),
)
| 429 | |
| 430 | |
| 431 | async def verify_api_key( |
| 432 | x_api_key: str = Header(None, alias="X-API-Key"), |
| 433 | ) -> None: |
| 434 | """ |
| 435 | Verify the API key in request header (for use as Depends). |
| 436 | |
| 437 | Raises: |
| 438 | HTTPException 401 if key is missing or invalid |
| 439 | """ |
| 440 | if not x_api_key: |
| 441 | raise HTTPException( |
| 442 | status_code=status.HTTP_401_UNAUTHORIZED, |
| 443 | detail="API key required", |
| 444 | ) |
| 445 | |
| 446 | if not API_KEY: |
| 447 | raise HTTPException( |
| 448 | status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, |
| 449 | detail="BACKEND_API_KEY is not configured", |
| 450 | ) |
| 451 | |
| 452 | if x_api_key != API_KEY: |
| 453 | raise HTTPException( |
| 454 | status_code=status.HTTP_401_UNAUTHORIZED, |
| 455 | detail="Invalid API key", |
| 456 | ) |