Dependencies in Path Operations
Sometimes you need to run dependencies for their side effects rather than their return values. FastAPI allows you to declare dependencies directly in the path operation decorator, perfect for validation, logging, authentication checks, and other tasks where you don’t need the result.The dependencies Parameter
Path operation decorators accept a dependencies parameter that takes a list of dependencies:
Key Characteristics
- Dependencies are executed before the path operation function
- Their return values are discarded (not passed to your function)
- If any dependency raises an exception, the path operation is not executed
- Multiple dependencies execute in the order they’re declared
When to Use Path-Level Dependencies
Usedependencies parameter when you need:
1. Authentication/Authorization Checks
You don’t need the token value in the path operation, you just need to verify it exists and is valid.
2. Rate Limiting
3. Logging and Monitoring
4. Input Validation
Multiple Dependencies
You can specify multiple dependencies, and they execute in order:Execution Flow
1
First dependency executes
verify_token checks the token. If it fails, an exception is raised and execution stops.2
Second dependency executes
verify_key checks the key. If it fails, an exception is raised and execution stops.3
Third dependency executes
log_access logs the access attempt.4
Path operation executes
Only if all dependencies succeed does the path operation function run.
Combining with Function-Level Dependencies
You can use both path-level and function-level dependencies together:Dependencies with Sub-Dependencies
Path-level dependencies can have their own sub-dependencies:Dependency Groups for Reusability
You can create reusable dependency lists:Error Handling
When a path-level dependency raises an exception, the path operation never executes:Path Operations vs. Routers
Dependencies at the path operation level only affect that specific endpoint. For broader application, see global dependencies.Real-World Example: API Gateway Pattern
Here’s a comprehensive example showing multiple path-level dependencies:Performance Considerations
Path-level dependencies are subject to the same caching rules as function-level dependencies. If the same dependency appears multiple times in a request (even at different levels), it’s only executed once by default.
Best Practices
1
Use for side effects only
If you need the return value, use function-level dependencies instead of path-level.
2
Keep dependencies pure
Path-level dependencies should be independent and not rely on order except when necessary.
3
Group related dependencies
Create reusable lists of related dependencies for consistency across endpoints.
4
Document the requirements
Make it clear in your API documentation which headers or parameters are needed by path-level dependencies.
5
Handle errors gracefully
Provide clear error messages when path-level dependencies fail so clients know what went wrong.
Next Steps
Learn about:- Global dependencies that apply to your entire application or router groups
- Advanced dependency patterns for testing and overrides
- Using dependencies with WebSocket connections