| # app/dependencies.py | |
| import os | |
| from fastapi import Header, HTTPException, Depends | |
| def require_api_key(x_api_key: str = Header(...)): | |
| """ | |
| Simple header-based API key check. | |
| The key is read from the environment variable API_KEY, which you set in | |
| Settings β Secrets on your Hugging Face Space. | |
| """ | |
| expected_key = os.getenv("API_KEY") | |
| if not expected_key: | |
| raise HTTPException(status_code=500, detail="API_KEY not configured") | |
| if x_api_key != expected_key: | |
| raise HTTPException(status_code=403, detail="Invalid API Key") | |
| return True | |