Complete the code to import the dependency function from FastAPI.
from fastapi import [1]
The Depends function is used to declare dependencies in FastAPI path operations.
Complete the code to declare a dependency function that returns a string.
def get_token(): return [1]
The function should return a string, so the value must be in quotes.
Fix the error in the path operation to use the dependency correctly.
from fastapi import FastAPI, Depends app = FastAPI() def get_token(): return 'token123' @app.get("/items/") async def read_items(token: str = [1]): return {"token": token}
To declare a dependency, use Depends(get_token) so FastAPI knows to call the function and inject its result.
Fill both blanks to declare a dependency that extracts a query parameter and use it in the path operation.
from fastapi import FastAPI, Depends, Query app = FastAPI() def get_limit(limit: int = [1]): return limit @app.get("/items/") async def read_items(limit: int = [2]): return {"limit": limit}
The dependency function uses Query(10) to set a default query parameter. The path operation uses Depends(get_limit) to get the value from the dependency.
Fill all three blanks to create a dependency that checks a header and use it in a path operation.
from fastapi import FastAPI, Depends, Header, HTTPException app = FastAPI() def verify_token(x_token: str = [1]): if x_token != "fake-super-secret-token": raise HTTPException(status_code=401, detail="Invalid token") return x_token @app.get("/secure-data") async def secure_data(token: str = [2]): return {"token": [3]
The dependency function uses Header(...) to require the header. The path operation uses Depends(verify_token) to call the dependency. The returned token is then used in the response.