A shared dependency is a function or class that multiple routers use to provide common data or logic, like authentication or database access, so you don't repeat code.
Click to reveal answer
beginner
How do you apply a shared dependency to multiple routers in FastAPI?
You add the dependency to each router using the 'dependencies' parameter when creating the router, so all routes inside use it automatically.
Click to reveal answer
intermediate
Why use shared dependencies across routers instead of repeating them in each route?
It keeps code clean and consistent, makes maintenance easier, and ensures all routes share the same logic like security checks or database sessions.
Click to reveal answer
intermediate
Show a simple example of a shared dependency function used in two routers.
Example: Define a function 'get_current_user' that checks user info. Add it as a dependency in two routers with 'dependencies=[Depends(get_current_user)]'. Both routers then require the user info.
Click to reveal answer
advanced
Can shared dependencies in FastAPI routers have parameters or state?
Yes, dependencies can accept parameters or use classes with state. FastAPI handles calling them and injecting needed values automatically.
Click to reveal answer
What parameter do you use to add shared dependencies to a FastAPI router?
Adependencies
Bshared
Cinclude
Dinject
✗ Incorrect
The 'dependencies' parameter is used to add shared dependencies to a router.
Why is it better to use shared dependencies across routers?
ATo repeat code in every route
BTo avoid using dependencies
CTo keep code clean and consistent
DTo slow down the app
✗ Incorrect
Shared dependencies keep code clean and consistent by avoiding repetition.
Which FastAPI function helps inject dependencies into routes?
ADepends
BInject
CProvide
DUse
✗ Incorrect
'Depends' is the FastAPI function used to declare dependencies.
Can shared dependencies in FastAPI routers handle authentication?
ANo, dependencies are only for data
BNo, only for database
COnly for logging
DYes, they can handle authentication
✗ Incorrect
Shared dependencies can handle authentication logic for routers.
If you want all routes in a router to require a user check, where do you add the dependency?
AIn each route decorator
BIn the router's dependencies list
CIn the main app only
DIn the database connection
✗ Incorrect
Adding the dependency to the router's dependencies list applies it to all routes inside.
Explain how to share a dependency function across multiple FastAPI routers and why this is useful.
Think about common tasks like authentication or database access.
You got /4 concepts.
Describe a scenario where shared dependencies in FastAPI routers improve your app's structure and maintenance.
Consider security checks or user info retrieval.
You got /4 concepts.
Practice
(1/5)
1. What is the main benefit of using shared dependencies across routers in FastAPI?
easy
A. To disable dependency injection in routers
B. To make the API slower by adding extra layers
C. To force all routers to use the same path prefix
D. To avoid repeating the same dependency code in multiple routers
Solution
Step 1: Understand what shared dependencies do
Shared dependencies allow you to write a dependency once and apply it to many routers, avoiding repetition.
Step 2: Identify the benefit in code maintenance
This reduces duplicate code and makes the project easier to maintain and update.
Final Answer:
To avoid repeating the same dependency code in multiple routers -> Option D
Quick Check:
Shared dependencies = avoid repetition [OK]
Hint: Shared dependencies reduce repeated code across routers [OK]
Common Mistakes:
Thinking shared dependencies slow down the API
Confusing shared dependencies with path prefixes
Believing shared dependencies disable injection
2. Which is the correct way to apply a shared dependency to an APIRouter in FastAPI?
easy
A. router = APIRouter(shared_dependencies=[Depends(common_dep)])
B. router = APIRouter(dependencies=[Depends(common_dep)])
C. router = APIRouter(dependency=Depends(common_dep))
D. router = APIRouter(shared=Depends(common_dep))
Solution
Step 1: Recall the APIRouter parameter for dependencies
The correct parameter name is dependencies, which takes a list of Depends objects.
Step 2: Match the syntax to the correct option
router = APIRouter(dependencies=[Depends(common_dep)]) uses dependencies=[Depends(common_dep)], which is the correct syntax.
Final Answer:
router = APIRouter(dependencies=[Depends(common_dep)]) -> Option B
Quick Check:
dependencies param = correct syntax [OK]
Hint: Use 'dependencies' param with a list of Depends [OK]
Common Mistakes:
Using 'shared_dependencies' instead of 'dependencies'
Passing Depends directly without a list
Using wrong parameter names like 'dependency' or 'shared'
3. Given this code snippet, what will be printed when accessing /items/42?
A. Depends should be inside a list in dependencies parameter
B. verify_token must return a value
C. APIRouter cannot have dependencies
D. The route path must start with a slash
Solution
Step 1: Check the dependencies parameter type
The dependencies parameter expects a list of Depends objects, not a single Depends instance.
Step 2: Identify the correct syntax
It should be dependencies=[Depends(verify_token)] to be valid.
Final Answer:
Depends should be inside a list in dependencies parameter -> Option A
Quick Check:
dependencies param requires a list [OK]
Hint: Always wrap Depends in a list for dependencies param [OK]
Common Mistakes:
Passing Depends directly without list
Assuming dependencies param accepts single Depends
Ignoring syntax errors from wrong dependencies format
5. You want to apply a shared dependency that extracts a user from a token to two routers, router_a and router_b. Which approach correctly shares the dependency without repeating code?
hard
A. Define the dependency in each route function separately in both routers
B. Include get_current_user as a parameter in FastAPI() app initialization
C. Add the dependency to each router using the dependencies parameter with Depends(get_current_user)
D. Use global variables to store the user and access it in routes
Solution
Step 1: Understand how to share dependencies across routers
Using the dependencies parameter in APIRouter allows applying a dependency to all routes in that router.
Step 2: Apply the dependency to both routers
Adding dependencies=[Depends(get_current_user)] to both router_a and router_b shares the logic without repeating it in each route.
Final Answer:
Add the dependency to each router using the dependencies parameter with Depends(get_current_user) -> Option C
Quick Check:
Shared dependencies via router dependencies param [OK]
Hint: Use dependencies param on routers to share common logic [OK]
Common Mistakes:
Repeating dependency code in every route
Trying to add dependencies in FastAPI app instead of routers
Using global variables instead of dependency injection
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.