Skip to content

FastAPI Depends

Medium — good to knowBackend

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."

Made with passive-aggressive love by manoga.digital. Powered by Claude.