--- id: fastapi-injector version: "0.9.0" license: BSD license_treatment: permissive maintenance: aging --- # fastapi-injector — python-injector integration for FastAPI License: permissive · Maintenance: aging · Downloads: 117.6K/mo ## 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 above — 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 pip install fastapi-injector uv add fastapi-injector 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_current - Install friction: low - Maintenance: aging - Downloads: 117.6K/month (top 15,000 on PyPI) - Known vulnerabilities: none known ## Tags fastapi dependency injection, injector integration fastapi, constructor injection fastapi, fastapi ioc container, request-scoped dependencies fastapi, fastapi composition root, dependency inversion fastapi, dependency-injection, fastapi-integration, ioc-container [View on SkillFed](https://skillfed.io/packages/fastapi-injector) · [View on PyPI](https://pypi.org/project/fastapi-injector/)