Dependency injection

FastAPI's dependency injection uses Depends to declare what a route needs (database session, current user, common parameters). The dependency is called by FastAPI, its results are provided to the function and can be overridden in tests.

Example

def get_db():
    ...

@app.get('/items')
def lister(db = Depends(get_db)): ...

Related concepts

The whole glossary