Complete the code to import the FastAPI class.
from fastapi import [1] app = [1]()
The FastAPI class is imported to create the app instance.
Complete the code to define the API key header name.
API_KEY_NAME = "[1]"
The API key is usually sent in a custom header like 'X-API-KEY'.
Fix the error in the dependency function to get the API key from headers.
from fastapi import Header, HTTPException async def get_api_key(api_key: str = Header(..., alias="[1]")): if api_key != "secret123": raise HTTPException(status_code=401, detail="Invalid API Key") return api_key
The Header parameter must match the header name 'X-API-KEY' to extract the API key correctly.
Fill both blanks to protect the endpoint with API key dependency.
from fastapi import Depends @app.get("/secure-data") async def secure_data(api_key: str = Depends([1])): return {"message": "Access granted with key: " + [2]
The endpoint uses Depends to call the get_api_key function, and the returned api_key is used in the response.
Fill all three blanks to create a complete FastAPI app with API key authentication.
from fastapi import FastAPI, Header, HTTPException, Depends app = FastAPI() API_KEY_NAME = "X-API-KEY" async def get_api_key(api_key: str = Header(..., alias="[1]")): if api_key != "secret123": raise HTTPException(status_code=401, detail="Invalid API Key") return api_key @app.get("/data") async def read_data(api_key: str = Depends([2])): return {"message": "Hello, your API key is " + [3]
The API key header name is 'X-API-KEY'. The dependency function is get_api_key. The returned api_key variable is used in the response.