Browse Source

Add example for API Key header configuration

pull/14672/head
Anya299 6 months ago
committed by GitHub
parent
commit
1e33c91a57
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 14
      docs/api_key_header_example.md

14
docs/api_key_header_example.md

@ -0,0 +1,14 @@
from fastapi import FastAPI, Header, HTTPException, Depends
app = FastAPI()
API_KEY = "mysecretapikey"
def get_api_key(x_api_key: str = Header(...)):
if x_api_key != API_KEY:
raise HTTPException(status_code=401, detail="Invalid API Key")
return x_api_key
@app.get("/secure-data")
def read_secure_data(api_key: str = Depends(get_api_key)):
return {"message": "This is secure data"}
Loading…
Cancel
Save