Skip to main content

Global Dependencies

When you need the same dependencies across many or all routes in your application, declaring them individually becomes tedious and error-prone. FastAPI allows you to declare global dependencies that automatically apply to multiple endpoints.

Application-Level Dependencies

You can add dependencies to your entire FastAPI application using the dependencies parameter:

How It Works

1

Dependencies are declared at app level

When you create your FastAPI() instance, you pass a list of dependencies.
2

Applied to every endpoint automatically

Every path operation in the application automatically has these dependencies applied.
3

Executed before each request

For every incoming request to any endpoint, these dependencies execute first.
4

No code duplication

You write the dependency declaration once and it applies everywhere.
This example is from the FastAPI source at docs_src/dependencies/tutorial012_py310.py. Both /items/ and /users/ endpoints require valid x-token and x-key headers without explicitly declaring them.

Router-Level Dependencies

For more granular control, you can apply dependencies to router groups:

Router Hierarchy

Routers can be nested, and dependencies accumulate:
Dependencies from parent routers are executed before dependencies from child routers, which are executed before path operation dependencies.

Common Use Cases

1. Global Authentication

Apply authentication to your entire API:

2. Database Connection Management

3. Request Logging

4. API Versioning

Combining Different Levels

You can combine global, router, and path operation dependencies:

Execution Order

Dependencies execute in this order:
  1. Application-level dependencies
  2. Router-level dependencies (parent to child)
  3. Path operation-level dependencies
  4. Function parameter dependencies

Excluding Specific Routes

If you need most routes to have a dependency but want to exclude some:

Dependencies with Return Values

Even global dependencies can return values, but you need to explicitly include them in path operations that need them:
Global dependencies are executed for every request, so keep them lightweight. Expensive operations should be cached or moved to path-specific dependencies.

Testing with Global Dependencies

Global dependencies can be overridden during testing:

Real-World Example: Microservice Gateway

Best Practices

1

Keep global dependencies lightweight

Global dependencies run on every request. Avoid expensive operations or move them to route-specific dependencies.
2

Use router groups for organization

Group related routes under routers with shared dependencies rather than applying everything globally.
3

Consider execution order

Remember that global dependencies execute first. Design your dependency chain accordingly.
4

Document global requirements

Make sure API documentation clearly shows what headers or parameters are required for all endpoints.
5

Use dependency overrides for testing

Take advantage of FastAPI’s dependency override system to make testing easier.
6

Monitor performance

Global dependencies impact every request. Monitor their performance and optimize as needed.

Scope Considerations

Global dependencies respect the same scoping rules as other dependencies:
The scope parameter (defined in fastapi/params.py:750) controls:
  • "request": Dependency executes and cleans up per request (default for generators)
  • "function": Dependency is scoped to the function call

Next Steps

Now that you understand global dependencies, explore:
  • Advanced dependency patterns and dependency override for testing
  • Using dependencies with background tasks
  • Creating custom dependency classes for complex scenarios
  • Security dependencies for OAuth2, JWT, and API keys