FastAPI's Depends: Let the Framework Handle Setup

Think of Depends as a pre-flight checklist for your API endpoints. You list required setup tasks, like getting a user or a database session, and FastAPI runs them for you. This is key for sharing logic like auth or database connections across many routes.
Why it exists
To solve the problem of repeated setup and teardown code in API endpoints. Without a dependency injection system, every endpoint needing a database connection or user authentication would have to contain the same boilerplate logic, making the code hard to maintain and test.
The mental model
Depends is like telling your function, "Before you can do your job, you need these things first." You define a "dependable" function that performs a task (e.g., gets a database session). Your main endpoint function (the "dependant") then lists this task using Depends. FastAPI sees this, runs the dependable, and passes its return value as an argument to your endpoint function. It's a system for delegating prerequisite tasks.
How it works
You import Depends from fastapi. In your path operation function's signature, you add a parameter with a default value of Depends(your_dependency_function). When a request comes in, FastAPI first calls your_dependency_function. The value returned by that function is then passed into your path operation function as the argument for that parameter. FastAPI also caches the result within the same request, so if multiple dependencies rely on the same sub-dependency, it's only run once.
When to use it
Use Depends for any logic that needs to be shared across multiple endpoints. Three common cases are: first, managing database connections (getting a session and closing it); second, handling security (validating a token and fetching the current user); and third, reusing complex parameter validation logic.
When not to use it
Avoid using Depends for logic that is truly unique to a single endpoint. If the setup code is simple and will never be reused, putting it directly inside the path operation function can be clearer. Overusing dependencies for trivial tasks can make the code harder to trace.
One canonical example
A common pattern is to get a database session. You define a function get_db() that creates a session, yields it, and then closes it in a finally block. Your endpoint function would then be defined like this: def get_items(db: Session = Depends(get_db)):. FastAPI runs get_db, injects the Session object as the db argument, and ensures the session is closed after the request is finished.
Interview question
For what primary purpose should a developer use FastAPI's Depends feature in an API endpoint?
- a.To implement custom error handling and logging for the endpoint.
- b.To specify the expected data format for the API's response.
- c.To manage shared setup tasks, such as database connections or user authentication.Correct
- d.To define the main business logic that processes the request data.
Why? this is the answer
The card states that Depends solves "repeated setup and teardown code" and is used for "managing database connections" and "handling security." It delegates prerequisite tasks, not the main business logic, response formatting, or error handling.
Just read this? Test yourself on what you have been reading.
Read the original → fastapi.tiangolo.com
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on fastapi — each one lists the topics its interview covers.
See open roles