Dependency injection with Depends in FastAPI
Answer: FastAPI has a very powerful but intuitive Dependency Injection system. Although you use Depends in the parameters of your function the same way you use Body, Query, etc, Depends works a bit differently. (S1)
Shared code
- This way you write shared code once and FastAPI takes care of calling it for your path operations. (S1)
- You don't have to create a special class and pass it somewhere to FastAPI to "register" it or anything similar. (S1)
- You just pass it to Depends and FastAPI knows how to do the rest. (S1)
Any callable works
- A "callable" in Python is anything that Python can "call" like a function. (S2)
- In FastAPI, you could use a Python class as a dependency. (S2)
- What FastAPI actually checks is that it is a "callable" (function, class or anything else) and the parameters defined. (S2)
- If you pass a "callable" as a dependency in FastAPI, it will analyze the parameters for that "callable", and process them in the same way as the parameters for a path operation function. (S2)
Claims
- id: C1 claim: FastAPI has a dependency injection system. source: S1 evidence: "FastAPI has a very powerful but intuitive Dependency Injection system."
- id: C2 claim: Shared code is written once. source: S1 evidence: "and then FastAPI takes care of calling the function with the correct parameters"
- id: C3 claim: Classes can be dependencies. source: S2 evidence: "you could use a Python class as a dependency."
- id: C4 claim: FastAPI checks for a callable. source: S2 evidence: "...is what FastAPI will actually use to know what is the dependency."
Sources
- [S1] Dependencies - FastAPI. FastAPI docs. https://fastapi.tiangolo.com/tutorial/dependencies/ . License: MIT (fastapi/fastapi LICENSE; Copyright (c) 2018 Sebastián Ramírez; https://github.com/fastapi/fastapi/blob/master/LICENSE). Accessed 2026-11-04.
- [S2] Classes as Dependencies - FastAPI. FastAPI docs. https://fastapi.tiangolo.com/tutorial/dependencies/classes-as-dependencies/ . License: MIT (fastapi/fastapi LICENSE; Copyright (c) 2018 Sebastián Ramírez; https://github.com/fastapi/fastapi/blob/master/LICENSE). Accessed 2026-11-04.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted).
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: FastAPI docs, Dependencies - FastAPI, MIT (fastapi/fastapi LICENSE; Copyright (c) 2018 Sebastián Ramírez; https://github.com/fastapi/fastapi/blob/master/LICENSE); FastAPI docs, Classes as Dependencies - FastAPI, MIT (fastapi/fastapi LICENSE; Copyright (c) 2018 Sebastián Ramírez; https://github.com/fastapi/fastapi/blob/master/LICENSE).
Cite this
iinventedlight. 2026. "Dependency injection with Depends in FastAPI." iinventedlight Library, iil:note:8t8grmwz, version of 2026-11-04. https://iinventedlight.com/library/notes/fastapi-dependencies/. License: CC-BY-4.0.
@misc{iil_note_8t8grmwz,
author = {{iinventedlight}},
title = {{Dependency injection with Depends in FastAPI}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-04},
url = {https://iinventedlight.com/library/notes/fastapi-dependencies/},
note = {Permanent ID iil:note:8t8grmwz. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com.
For language models and agents: start at /llms.txt. This page in Markdown: /library/notes/fastapi-dependencies.md.