0
0
FastAPIframework~10 mins

Global dependencies in FastAPI - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Global dependencies
Define dependency function
Declare global dependency in FastAPI app
Request comes in
FastAPI calls global dependency
Dependency returns value
Route handler uses dependency value
Response sent back
This flow shows how FastAPI calls a global dependency for every request before running the route handler.
Execution Sample
FastAPI
from fastapi import FastAPI, Depends

def get_token():
    return "token123"

app = FastAPI(dependencies=[Depends(get_token)])

@app.get("/")
async def read_root(token: str = Depends(get_token)):
    return {"token": token}
This code sets a global dependency get_token that runs on every request, and the route also uses it.
Execution Table
StepActionDependency CalledReturned ValueRoute Handler InputResponse
1Request received at '/'get_tokentoken123token='token123'{"token": "token123"}
2Request received at '/' againget_tokentoken123token='token123'{"token": "token123"}
3Request received at '/' third timeget_tokentoken123token='token123'{"token": "token123"}
💡 Requests end after response sent; global dependency runs each time before route handler.
Variable Tracker
VariableStartAfter 1After 2After 3
tokenNonetoken123token123token123
Key Moments - 2 Insights
Why does get_token run even if the route handler also depends on it?
Because get_token is declared as a global dependency, FastAPI runs it before every request regardless of route-specific dependencies, as shown in execution_table rows 1-3.
Does the global dependency value get reused between requests?
No, get_token runs fresh for each request, returning 'token123' each time, as seen in variable_tracker showing 'token123' after every request.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what value does get_token return at step 2?
A"token123"
B"token456"
CNone
D"error"
💡 Hint
Check the 'Returned Value' column at step 2 in the execution_table.
At which step does the route handler receive the token value 'token123'?
AStep 4
BStep 1
CStep 0
DStep 3
💡 Hint
Look at the 'Route Handler Input' column in the execution_table.
If the global dependency get_token returned a different value, how would the response change?
ARoute handler would not run
BResponse would stay the same
CResponse token value would change accordingly
DServer would crash
💡 Hint
See how the 'Response' column depends on the 'Returned Value' from get_token in the execution_table.
Concept Snapshot
Global dependencies in FastAPI run before every request.
Declare them in FastAPI() with dependencies=[Depends(your_func)].
They provide shared values or checks for all routes.
Route handlers can also depend on the same function.
Each request calls the global dependency fresh.
Useful for auth, logging, or common data.
Full Transcript
Global dependencies in FastAPI are functions that run before every request automatically. You declare them when creating the FastAPI app using the dependencies parameter with Depends. When a request comes in, FastAPI calls the global dependency function first, gets its return value, then passes that value to the route handler if it also depends on it. This happens for every request, so the dependency runs fresh each time. This is useful for things like authentication tokens or logging that you want to apply to all routes. The example code shows a get_token function returning a token string. The FastAPI app uses it as a global dependency and the route also depends on it. The execution table shows each request triggers get_token, which returns 'token123', then the route handler receives that token and returns it in the response. The variable tracker confirms the token value updates each request. This helps beginners see how global dependencies run independently of route-specific dependencies and run every time a request happens.