0
0
FastAPIframework~10 mins

Shared dependencies across routers in FastAPI - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Shared dependencies across routers
Define Dependency Function
Create Router A with Dependency
Create Router B with Dependency
Include Routers in Main App
Request to Router A or B
Dependency Injected Automatically
Endpoint Executes Using Dependency
This flow shows how a single dependency function is defined once and shared across multiple routers, which are then included in the main FastAPI app. When a request hits any router, the shared dependency is automatically injected.
Execution Sample
FastAPI
from fastapi import FastAPI, APIRouter, Depends

def common_dep():
    return "shared data"

router_a = APIRouter(dependencies=[Depends(common_dep)])
router_b = APIRouter(dependencies=[Depends(common_dep)])

app = FastAPI()
app.include_router(router_a)
app.include_router(router_b)
Defines a shared dependency and attaches it to two routers so both use it automatically.
Execution Table
StepActionDependency CalledRouterResult
1Define common_dep functionNoN/AFunction ready
2Create router_a with Depends(common_dep)Norouter_aRouter ready with dependency
3Create router_b with Depends(common_dep)Norouter_bRouter ready with dependency
4Include router_a and router_b in FastAPI appNoappApp ready with routers
5Request to router_a endpointYesrouter_acommon_dep runs, returns 'shared data'
6Request to router_b endpointYesrouter_bcommon_dep runs, returns 'shared data'
7Request to app root (no dependency)NoappNo dependency called
ExitNo more requestsN/AN/AExecution ends
💡 No more requests to routers, execution stops
Variable Tracker
VariableStartAfter Step 5After Step 6Final
common_dep resultN/A"shared data""shared data""shared data"
router_a dependenciesDepends(common_dep)Depends(common_dep)Depends(common_dep)Depends(common_dep)
router_b dependenciesDepends(common_dep)Depends(common_dep)Depends(common_dep)Depends(common_dep)
Key Moments - 2 Insights
Why does common_dep run on requests to both router_a and router_b?
Because both routers include Depends(common_dep) in their dependencies list, so FastAPI injects it automatically on requests to any endpoint in those routers (see execution_table rows 5 and 6).
Does common_dep run when requesting the main app root without routers?
No, because the main app root does not have the dependency attached, so common_dep is not called (see execution_table row 7).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the result of common_dep when a request hits router_b?
A"router_b"
B"shared data"
CNone
DError
💡 Hint
Check execution_table row 6 where router_b request calls common_dep returning 'shared data'
At which step does the dependency get injected for the first time?
AStep 5
BStep 2
CStep 4
DStep 1
💡 Hint
Look at execution_table row 5 where the first request triggers dependency injection
If we remove Depends(common_dep) from router_a, what changes in the execution table?
AStep 6 no longer calls common_dep
BStep 7 calls common_dep
CStep 5 no longer calls common_dep
DNo changes
💡 Hint
Refer to variable_tracker and execution_table rows 5 and 6 about which routers have the dependency
Concept Snapshot
Shared dependencies in FastAPI allow you to define a function once
and attach it to multiple routers using Depends().
When a request hits any router with that dependency,
FastAPI runs the dependency automatically.
This avoids repeating code and keeps routers clean.
Include routers in the main app to activate them.
Full Transcript
In FastAPI, you can create a dependency function that provides shared data or logic. This function is defined once, for example common_dep, which returns 'shared data'. You then create routers, like router_a and router_b, and attach this dependency to them using the dependencies parameter with Depends(common_dep). When you include these routers in the main FastAPI app, any request to endpoints inside these routers will automatically call common_dep before running the endpoint code. This means the shared dependency runs for both routers without repeating code. If you request the main app root without routers, the dependency does not run. This setup helps keep your code organized and efficient by sharing common logic across multiple routers.