Dependency Injection in FastAPI

noun

say it: “dih-PEN-den-see in-JEK-shun in FAST-AY-PEE-EYE” /dɪˈpɛndənsi ɪnˈdʒɛkʃən ɪn ˈfæstˌeɪˌpiˌaɪ/

FastAPI calling the helper functions a route asks for with Depends() and passing their results in, such as a database connection or the current user.

What is Dependency Injection in FastAPI?

FastAPI's dependency injection runs helper functions before your endpoint and passes in their results, such as a database session or the current user.

In a sentence

  • “With dependency injection, the workout routes never open a connection themselves.”
  • “Tests swap the database through FastAPI's dependency injection.”

Level: Journeyman · More FastAPI terms at this level

Updated