0
0
FastAPIframework~10 mins

Shared dependencies in FastAPI - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Shared dependencies
Define dependency function
Declare dependency in endpoint
FastAPI calls dependency
Dependency returns value
Endpoint uses returned value
Response sent to client
FastAPI calls shared dependency functions before endpoints, passing their results to endpoints for reuse.
Execution Sample
FastAPI
from fastapi import FastAPI, Depends

app = FastAPI()

def common_dep():
    return "shared value"

@app.get("/items")
async def read_items(value: str = Depends(common_dep)):
    return {"value": value}
This code defines a shared dependency function and uses it in an endpoint to provide a value.
Execution Table
StepActionDependency CalledDependency ResultEndpoint ParameterResponse
1Request to /items receivedNoN/AN/AN/A
2FastAPI calls common_dep()Yes"shared value"N/AN/A
3common_dep() returnsYes"shared value"N/AN/A
4Endpoint read_items called with value="shared value"NoN/A"shared value"N/A
5Endpoint returns responseNoN/AN/A{"value": "shared value"}
💡 Request handled after dependency provides value and endpoint returns response
Variable Tracker
VariableStartAfter Step 2After Step 4Final
valueN/A"shared value""shared value""shared value"
Key Moments - 3 Insights
Why does FastAPI call the dependency function before the endpoint?
FastAPI calls the dependency first to get the value it needs to pass into the endpoint parameter, as shown in execution_table step 2 and 4.
Can the same dependency be used in multiple endpoints?
Yes, shared dependencies can be reused across endpoints to avoid repeating code, as the dependency function is independent and called each time.
What happens if the dependency returns a different value each time?
The endpoint receives the new value each time because FastAPI calls the dependency fresh on every request, shown by the dependency call in step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'value' when the endpoint read_items is called?
ANone
BN/A
C"shared value"
D"common_dep"
💡 Hint
Check the 'Endpoint Parameter' column at step 4 in the execution_table.
At which step does FastAPI call the dependency function?
AStep 1
BStep 2
CStep 4
DStep 5
💡 Hint
Look at the 'Dependency Called' column in the execution_table.
If the dependency returned "new value" instead, what would change in the execution_table?
ADependency Result and Endpoint Parameter would show "new value"
BResponse would be unchanged
CDependency Called would be No
DEndpoint would not be called
💡 Hint
Focus on the 'Dependency Result' and 'Endpoint Parameter' columns in the execution_table.
Concept Snapshot
FastAPI Shared Dependencies:
- Define a function to provide shared data
- Use Depends() in endpoint parameters
- FastAPI calls dependency before endpoint
- Dependency return value passed to endpoint
- Enables code reuse and cleaner endpoints
Full Transcript
In FastAPI, shared dependencies are functions that provide values or logic reused by multiple endpoints. When a request comes in, FastAPI first calls the dependency function to get its result. Then it passes this result as an argument to the endpoint function. This way, the endpoint can use shared data or logic without repeating code. The execution table shows the request flow: receiving the request, calling the dependency, returning its value, calling the endpoint with that value, and finally sending the response. Variables like 'value' hold the dependency's output and are tracked through the steps. Understanding this flow helps beginners see how FastAPI manages dependencies behind the scenes.