Skip to main content

Introduction to Dependencies

FastAPI includes a powerful and intuitive Dependency Injection system that makes it easy to integrate components with your path operations and share common logic across your application.

What is Dependency Injection?

Dependency Injection is a design pattern where your code declares the components it needs to work, and the framework automatically provides them. In FastAPI, this is handled through the Depends() function.
Dependency Injection helps you:
  • Reduce code duplication
  • Share logic across multiple path operations
  • Manage database connections and sessions
  • Enforce authentication and authorization
  • Validate and process common parameters

Your First Dependency

Let’s start with a simple example. Imagine you have common query parameters used across multiple endpoints:

How It Works

1

Define the dependency function

Create a function with the parameters you need. This function can be async or regular def:
2

Use Depends() in your path operation

Pass the dependency function to Depends() as a parameter in your path operation:
3

FastAPI handles the rest

When a request comes in, FastAPI:
  1. Calls the dependency function with the correct parameters from the request
  2. Gets the result from the dependency
  3. Passes that result to your path operation function

The Depends Function

The Depends class is defined in fastapi/params.py:747 as:

Key Features

  • dependency: The callable (function or class) that will be executed
  • use_cache: Whether to cache the result during a request (default: True)
  • scope: Controls when the dependency is executed and cleaned up
By default, dependencies are cached per request. If the same dependency is used multiple times in a single request, it’s only executed once and the result is reused.

Automatic Type Conversion and Validation

Just like path operation parameters, dependency parameters benefit from automatic:
  • Type conversion
  • Data validation
  • Automatic documentation
  • Editor support with autocompletion
If validation fails (e.g., skip is not an integer), FastAPI automatically returns a 422 error with details about what went wrong.

When to Use Dependencies

Dependencies are perfect for:

1. Shared Query Parameters

2. Database Sessions

3. Authentication

4. Rate Limiting

Dependencies Can Be Async or Sync

FastAPI automatically handles both:
Sync dependencies in async path operations run in an external thread pool, so they don’t block the async event loop.

The Dependant Model

Internally, FastAPI uses a Dependant model (defined in fastapi/dependencies/models.py:32) to represent dependencies:
This model tracks all the parameters and sub-dependencies, enabling FastAPI to build a complete dependency graph and execute everything in the correct order.

Next Steps

Now that you understand the basics of dependencies, you can learn about:
  • Using classes as dependencies for more complex logic
  • Creating sub-dependencies (dependencies that depend on other dependencies)
  • Applying dependencies at the path operation decorator level
  • Setting up global dependencies for your entire application