skillfed

fastapi-injector

python-injector integration for FastAPI

fastapi-injector v0.9.0 117.6K downloads/30d#12,155 on PyPI
Permissive license BSD AGING released

What it is and what it does

fastapi-injector bridges the injector library (a mature Python dependency-injection container) with FastAPI's dependency system. It lets you define your application's object graph separately from your routes, then inject dependencies into handlers using the Injected marker. This is useful for layered architectures where domain logic (use cases, repositories) lives in inner layers and depends on abstract interfaces, while concrete implementations are bound and provided by the injector in the outer FastAPI layer.

The package includes a RequestScope for per-request caching (so the same instance is reused within a single request but fresh instances are created for new requests), middleware integration, support for both async and sync dependencies, and automatic cleanup of context-manager resources at request end. It requires Python 3.10 or later and depends only on injector.

Use it for:

  • Implement onion/hexagonal architecture where domain use cases depend on abstract ports and FastAPI routes inject concrete repository implementations.
  • Manage per-request database connections or transaction scopes by binding them to request scope and injecting them into multiple route handlers.
  • Decouple route handlers from concrete service implementations, making unit tests easier by swapping real services for mocks via injector bindings.
  • Share expensive resources (connection pools, caches) across multiple dependencies within a single request without passing them through function signatures.
  • Build modular FastAPI applications where different routers can depend on the same injected services without tight coupling.

Worth the install?

AI-flagged interpretation of the facts on this page — verify before relying

Integrates the injector dependency-injection library with FastAPI, enabling constructor-based injection of dependencies into route handlers and application code.

Yes, with conditions. Install if you need constructor-based dependency injection in FastAPI and are comfortable with a permissive BSD license and aging maintenance (308 days since last release). The package is stable and has no known vulnerabilities, but expect slower issue resolution. Skip if you prefer lighter-weight solutions or need active maintenance and rapid updates.

Install

fastapi-injector on PyPI

pip

pip install fastapi-injector

uv

uv add fastapi-injector

poetry

poetry add fastapi-injector

Installing fastapi-injector

Before you install

Low install friction; single runtime dependency on injector. Maintenance status is aging—last release was 308 days ago—so expect slower response to issues, though the package remains functional.

License in practice

BSD license is permissive; you may use, modify, and distribute this package freely in commercial and open-source projects with minimal restrictions.

Quickstart

pip install fastapi-injector

from fastapi import FastAPI
from injector import Injector
from fastapi_injector import attach_injector, Injected

inj = Injector()
app = FastAPI()
attach_injector(app, inj)

@app.get("/")
async def route(dep: SomeClass = Injected(SomeClass)):
    return dep.method()

Requires Python 3.10 or later; FastAPI must be installed separately or via extras (standard or slim).

Verify before relying

  • Whether request-scoped caching works correctly with nested async dependencies in production.
  • Performance impact of context-variable-based request scope on high-concurrency workloads.
  • Compatibility with recent FastAPI versions beyond what the aging maintenance signal suggests.

Package facts

License BSD (permissive)
Python support supports the current Python release (<4,>=3.10)
Install friction low — pure-Python wheel
Runtime dependencies 1 — injector
Maintenance aging — 308 days since the last release
First released
Downloads 117,641/month — #12,155 on PyPI (30-day window, as of 2026-08-14)
Known vulnerabilities none known (OSV.dev, checked 2026-08-14)

Evidence: fastapi_injector-0.9.0-py3-none-any.whl

Environment :: Web EnvironmentFramework :: FastAPIIntended Audience :: DevelopersTopic :: Software Development :: LibrariesTyping :: Typed

Tags

fastapi dependency injectioninjector integration fastapiconstructor injection fastapifastapi ioc containerrequest-scoped dependencies fastapifastapi composition rootdependency inversion fastapi
dependency-injectionfastapi-integrationioc-container

More Libraries packages