appget_current_user that returns {'username': 'alice'}Depends(get_current_user) in two path operations: /users/me and /users/me/itemscurrent_user that receives the dependency resultJump into concepts and practice - no test required
appget_current_user that returns {'username': 'alice'}Depends(get_current_user) in two path operations: /users/me and /users/me/itemscurrent_user that receives the dependency resultapp and define a dependency function called get_current_user that returns the dictionary {'username': 'alice'}.Use app = FastAPI() to create the app. Define a function get_current_user that returns the dictionary {'username': 'alice'}.
/users/me that uses Depends(get_current_user) to get the current user. The function should accept a parameter called current_user and return it.Use @app.get('/users/me') decorator. Define a function with parameter current_user: dict = Depends(get_current_user) and return current_user.
/users/me/items that also uses Depends(get_current_user). The function should accept a parameter called current_user and return a dictionary with a key items and value as a list containing the string "item1".Use @app.get('/users/me/items') decorator. Define a function with parameter current_user: dict = Depends(get_current_user) and return {'items': ['item1']}.
Depends function is imported from fastapi and that both path operation functions have the parameter current_user typed as dict with = Depends(get_current_user).Check that Depends is imported from fastapi and used as a default value with type hint dict for current_user in both functions.
Depends() in FastAPI path operations?Depends()Depends() is used to declare dependencies that run shared code before the main path operation function executes.
This helps keep code clean by reusing common logic like authentication or database access.
Dependencies are declared by assigning a parameter to Depends() with the dependency function inside.
def read_item(item_id: int, user=Depends(get_current_user)): correctly uses user=Depends(get_current_user). Others have syntax errors or call the function directly.
/items/5?
from fastapi import FastAPI, Depends
app = FastAPI()
def get_token():
return "token123"
@app.get("/items/{item_id}")
def read_item(item_id: int, token: str = Depends(get_token)):
return {"item_id": item_id, "token": token}The get_token function returns "token123" and is injected into token parameter via Depends().
The path operation returns a dictionary with item_id and token keys, so the output includes the token string.
from fastapi import FastAPI, Depends
app = FastAPI()
def get_user():
return "user1"
@app.get("/profile")
def profile(user: str = Depends(get_user)):
return {"user": user}
@app.get("/dashboard")
def dashboard(user = Depends(get_user)):
return {"dashboard_user": user}The profile function declares user: str = Depends(get_user) with a type annotation.
dashboardThe dashboard function uses user = Depends(get_user) but lacks a type annotation, which FastAPI requires for dependencies.
You can call one dependency inside another to reuse logic and combine checks.
By calling the user extraction inside the active check dependency, you only need to use Depends() on the active check in routes.