Overview
TheDepends class is used to declare dependencies in FastAPI path operations. Dependencies are callable functions that can be reused across multiple endpoints and execute before the path operation function.
Signature
Parameters
Callable[..., Any] | None
default:"None"
The callable that will be called as a dependency. Can be a function, class, or any callable object. FastAPI will call this dependency and provide its return value to your path operation function.
bool
default:"True"
Whether to cache the result of the dependency within the same request. When
True, if the same dependency is used multiple times in a single request, it will only be executed once and the result will be cached. Set to False to execute the dependency every time it’s called.Literal['function', 'request'] | None
default:"None"
The scope in which the dependency operates. Can be:
"function": Dependency is executed per path operation function"request": Dependency is executed once per requestNone: Use default behavior
Usage
Basic Dependency
Dependency with Class
Disabling Cache
Nested Dependencies
See Also
- Security - Enhanced dependency for security and authentication
- FastAPI Dependencies Documentation