FastAPI Depends
ELI5 — The Vibe Check
FastAPI's Depends is dependency injection made stupid simple. Need database access? Depends. Need the current user? Depends. Need to validate a token? Depends. You declare what a route needs, and FastAPI automatically provides it. It's like a waiter who already knows your order.
Real Talk
FastAPI's Depends() is a dependency injection system that resolves function parameters at request time. Dependencies can be nested, cached per-request, and used for authentication, database sessions, pagination, and shared logic. They support async, yield-based cleanup, and are fully integrated with OpenAPI documentation.
Show Me The Code
async def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
@app.get('/users')
async def list_users(db: Session = Depends(get_db)):
return db.query(User).all()
When You'll Hear This
"Use Depends to inject the database session into every route handler." / "Our auth middleware is just a Depends that extracts the user from the JWT."
Related Terms
Authentication (AuthN)
Authentication is proving you are who you say you are.
Dependency Injection
Instead of your UserService creating its own DatabaseConnection (tight coupling), you pass the database in from outside: new UserService(db).
FastAPI
FastAPI is a Python framework that's both blazing fast and auto-generates documentation for your API.
Middleware
Middleware is like a security checkpoint at an airport.