--- id: fastapi-injectable version: "1.6.0" license: MIT license_treatment: permissive maintenance: active --- # fastapi-injectable — Use FastAPI's Depends() anywhere — in CLI tools, Celery tasks, background workers, and more. No refactoring needed. License: permissive · Maintenance: active · Downloads: 83.5K/mo ## What it is and what it does fastapi-injectable is a lightweight decorator and function-based wrapper that extends FastAPI's Depends() system to work outside route handlers. It solves a long-standing limitation (FastAPI issue #1105) where developers had to duplicate dependency logic or introduce a second DI framework when moving from routes into CLI commands, background tasks, Celery jobs, or scheduled workers. The package provides three main entry points: the @injectable decorator for marking functions, get_injected_obj() for synchronous contexts, and async_get_injected_obj() for code running in an active event loop. It handles full async support, resource cleanup via generator dependencies, optional caching, and graceful shutdown. A bundled mypy plugin enables type-checking of @injectable calls. The single runtime dependency is fastapi itself. Use it for: - Reuse FastAPI route dependencies in CLI commands without refactoring or duplication. - Inject dependencies into Celery tasks or background workers using the same Depends() syntax as routes. - Access FastAPI app.state and registered dependencies in scheduled jobs (cron, APScheduler). - Write testable functions with dependency overrides using manual mock injection. - Handle async dependencies in Kafka consumers or streaming callbacks with async_get_injected_obj(). - Manage resource lifecycle (database connections, file handles) via generator-based dependencies in non-route contexts. ## Worth the install? AI-flagged interpretation of the facts above — verify before relying. Enables FastAPI's Depends() dependency injection system to work outside route handlers—in CLI tools, background workers, Celery tasks, and scheduled jobs—without duplicating dependency logic. Yes. The package is actively maintained, has no known vulnerabilities, and solves a real problem for FastAPI users who need dependency injection outside routes. Low install friction, permissive license, and production-stable status make it a straightforward choice. The main gotcha is cache management in loops—read the long-running worker documentation before deploying to background tasks. ## Install pip install fastapi-injectable uv add fastapi-injectable poetry add fastapi-injectable ## Installing fastapi-injectable Before you install: Low friction install with a single runtime dependency (fastapi). Active maintenance: last commit 2026-08-01, 294 repository stars, and production-stable status since 2024-12-18. License in practice: MIT license (permissive) — you can use, modify, and distribute this package freely in commercial and open-source projects with minimal restrictions. Quickstart: pip install fastapi-injectable from typing import Annotated from fastapi import Depends from fastapi_injectable import injectable @injectable def process_data(db: Annotated[Database, Depends(get_db)]) -> str: return db.query() result = process_data() Requires Python 3.10 or higher and FastAPI >=0.112.4,<1.0.0. Process-global dependency cache persists across calls in loops; use injectable_scope() or clear_dependency_cache() + cleanup_all_exit_stacks() to reset state per iteration in long-running workers. Verify before relying: - Whether the mypy plugin works correctly with all mypy versions and configurations beyond the documented setup. - Performance characteristics when dependency graphs are deeply nested or contain many generator-based dependencies. - Compatibility behavior with FastAPI versions at the upper boundary (0.112.4 through 1.0.0 pre-release). ## Package facts - License: MIT (permissive) - Python support: supports_current - Install friction: low - Maintenance: active - Downloads: 83.5K/month (top 15,000 on PyPI) - Known vulnerabilities: none known ## Tags fastapi dependency injection outside routes, use fastapi depends in cli, background worker dependency injection, celery task fastapi dependencies, reuse fastapi dependencies, decorator for fastapi depends, scheduled job dependency injection, dependency-injection, fastapi-extension, cli-tools [View on SkillFed](https://skillfed.io/packages/fastapi-injectable) · [View on PyPI](https://pypi.org/project/fastapi-injectable/)