Dependency Injection (DI)
In the previous lesson, we organized our Employee Management System using APIRouter. Each resource (Employees, Departments, etc.) had its own router, making the application modular and easy to maintain. Now we’ll introduce another important FastAPI feature:Dependency Injection (DI)Rather than creating or retrieving required objects inside every endpoint, FastAPI can automatically provide (inject) them using
Depends().
In this lesson, we’ll use a simple in-memory employee list to understand the concept. Later, we’ll replace it with a real database session without changing the endpoint structure.
Goal
We will build an Employee Management System where:- Employee data is stored in one place.
- Endpoints do not access the data directly.
- FastAPI automatically injects the data into each endpoint using
Depends(). - Multiple endpoints can read and update the same shared data.
Application Architecture
Step 1. Create the FastAPI Application
Task
Create the main FastAPI application.Step 2. Create a Shared Data Provider
Task
Create a reusable function that provides employee data. Create a file named data.py.What does this accomplish?
- Employee data is stored in one place.
- Every endpoint can reuse it.
- The endpoint doesn’t need to know where the data comes from.
Step 3. Create the Employee Router
Task
Create a router responsible for employee operations.Step 4. Inject the Dependency
Task
Ask FastAPI to automatically provide employee data whenever an endpoint is executed.“Before executing this endpoint, callget_employees_data()and pass its result into theemployeesparameter.”
Step 5. Read Individual Employees
Task
Reuse the same dependency in another endpoint.Step 6. Add a New Employee
Task
Update the shared employee list.Step 7. Update an Employee
Task
Modify an existing employee.Step 8. Delete an Employee
Task
Remove an employee from the shared list.Step 9. Register the Router
Task
Connect the router to the FastAPI application.What Actually Happens?
Suppose the client sends:- Receives the request.
- Finds
Depends(get_employees_data). - Calls
get_employees_data(). - Receives the employee list.
- Injects it into the endpoint.
- Executes the endpoint.
- Returns the response.
Request Flow
Why Can We Update the List?
The dependency returns the same list object every time.EMPLOYEES list.
Therefore,
Shared Data Flow
Both endpoints receive the same shared list.Without Dependency Injection
Without DI, every endpoint retrieves the dependency itself.With Dependency Injection
Using DI, the endpoint simply declares what it needs.How FastAPI Thinks
When FastAPI sees:Why Is This Useful?
Today, the dependency returns a simple Python list.- a database session
- the current user
- application settings
- a logger
- an email service
- a repository
- a service object
Looking Ahead
Today’s dependency:Summary
- Dependency Injection (DI) allows FastAPI to automatically provide required objects to an endpoint.
Depends()tells FastAPI which dependency should be injected.- The endpoint never creates or retrieves the dependency itself.
- Multiple endpoints can reuse the same dependency.
- Because the dependency returns the same shared list, endpoints can both read and modify it.
- Later, the same pattern will be used to inject a database session using
Depends(get_db). - DI keeps code clean, reusable, and focused on business logic.