fastapi-dependency-injection

Implement FastAPI dependency injection with scopes, sub-dependencies, and cleanup.

187|20|Updated Nov 20, 2025
One-click install
npx skills add https://github.com/TheBushidoCollective/han --skill fastapi-dependency-injection
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: fastapi-dependency-injection
Source: https://github.com/TheBushidoCollective/han/tree/main/jutsu/jutsu-fastapi/skills/fastapi-dependency-injection
Command: npx skills add https://github.com/TheBushidoCollective/han --skill fastapi-dependency-injection

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill covers FastAPI's dependency injection system to build modular, testable APIs with reusable dependencies.

Core Features & Use Cases

  • Depends pattern: Reusable dependencies across endpoints.
  • Scoped lifetimes: Request-scoped vs singleton-like patterns.
  • Sub-dependencies: Build chains of dependencies for complex flows.

Quick Start

Define a get_db dependency and reuse it in multiple endpoints with Depends.

Frequently Asked Questions about fastapi-dependency-injection

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I structure reusable dependencies in FastAPI?

FastAPI's Depends pattern lets you define reusable dependencies as functions or classes, then inject them into endpoints by declaring them as parameters. Dependencies are executed once per request and their results are cached, reducing redundant database calls or authentication checks across multiple handlers.

What's the difference between request-scoped and application-scoped dependencies in FastAPI?

Request-scoped dependencies execute and reset for each HTTP request, ideal for database sessions or user context. Application-scoped dependencies persist across requests, suited for connection pools or configuration. FastAPI's dependency system manages both via the Depends declaration and generator-backed cleanup.

Can I chain dependencies together in FastAPI?

Yes, sub-dependencies let you build chains where one dependency depends on another. FastAPI resolves the full chain automatically, allowing complex flows like authentication → user lookup → database session without manual nesting or redundant execution.

How do I handle resource cleanup, like closing database connections, in FastAPI dependencies?

Use generator-backed dependencies with yield instead of return. Code after yield runs when the request ends, ensuring cleanup happens automatically. This pattern handles database disconnects, file closes, and external service teardown without try-finally boilerplate.

Do I need dependency injection for small FastAPI APIs?

Dependency injection becomes valuable once you have multiple endpoints sharing logic like database access or authentication. For single-endpoint prototypes it's optional, but it scales modularity and testability as your API grows, making it a best practice even early.

How do class-based dependencies differ from function-based ones in FastAPI?

Class-based dependencies use callable instances with __call__, offering statefulness and composability. Function-based dependencies are simpler for stateless logic. Both integrate identically with Depends; choose classes when you need initialization, validation, or method-based organization.