Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Depends function basics
📖 Scenario: You are building a simple FastAPI web app that greets users. You want to use FastAPI's Depends function to inject a dependency that provides a greeting message.
🎯 Goal: Create a FastAPI app with a dependency function that returns a greeting message. Use Depends to inject this greeting into an endpoint that returns the message to the user.
📋 What You'll Learn
Create a dependency function called get_greeting that returns the string 'Hello, FastAPI!'.
Create a FastAPI app instance called app.
Create a GET endpoint at /greet that uses Depends(get_greeting) to receive the greeting message.
Return the greeting message as a JSON response with key message.
💡 Why This Matters
🌍 Real World
Using Depends in FastAPI helps organize code by separating concerns like authentication, database access, or configuration.
💼 Career
Understanding Depends is essential for building scalable and maintainable FastAPI applications in professional backend development.
Progress0 / 4 steps
1
Create the dependency function
Create a function called get_greeting that returns the string 'Hello, FastAPI!'.
FastAPI
Hint
Define a simple function with no parameters that returns the greeting string.
2
Create the FastAPI app instance
Import FastAPI from fastapi and create an app instance called app.
FastAPI
Hint
Use from fastapi import FastAPI and then app = FastAPI().
3
Create the GET endpoint using Depends
Import Depends from fastapi. Create a GET endpoint at /greet using @app.get("/greet"). Define a function greet that takes a parameter message which uses Depends(get_greeting).
FastAPI
Hint
Use @app.get("/greet") and a function with a parameter using Depends(get_greeting).
4
Return the greeting message as JSON
In the greet function, return a dictionary with key message and value message parameter to send JSON response.
FastAPI
Hint
Return a dictionary with the greeting message to send JSON.
Practice
(1/5)
1. What is the main purpose of the Depends function in FastAPI?
easy
A. To create HTML templates
B. To define database models
C. To inject dependencies automatically into path operation functions
D. To handle HTTP status codes
Solution
Step 1: Understand what Depends does
Depends is used to declare dependencies that FastAPI will automatically provide to your route functions.
Step 2: Identify the main use case
It helps inject reusable code like authentication, database sessions, or other shared logic into routes.
Final Answer:
To inject dependencies automatically into path operation functions -> Option C
Quick Check:
Depends injects dependencies = C [OK]
Hint: Depends injects reusable code into routes automatically [OK]
Common Mistakes:
Confusing Depends with database or template functions
Thinking Depends handles HTTP status codes
Assuming Depends creates models
2. Which of the following is the correct way to declare a dependency in a FastAPI route using Depends?
easy
A. def read_items(db=Depends(get_db)): pass
B. def read_items(db: Depends(get_db)): pass
C. def read_items(db: Depends = get_db): pass
D. def read_items(db=Depends): pass
Solution
Step 1: Recall Depends syntax
The correct syntax is to assign the parameter a default value of Depends with the dependency function inside.
Step 2: Match the correct option
def read_items(db=Depends(get_db)): pass uses db=Depends(get_db), which is the proper way to declare a dependency.
Final Answer:
def read_items(db=Depends(get_db)): pass -> Option A
Quick Check:
Depends usage = parameter=Depends(function) [OK]
Hint: Use parameter=Depends(function) to declare dependencies [OK]
Common Mistakes:
Using type annotation instead of default value for Depends
Passing Depends without parentheses
Assigning Depends without a function
3. Given the code below, what will be the output when accessing the /items/ endpoint?