Skip to main content

fastapi-standalone-di

Latest Version MIT License Build Status Codecov semantic-release

Use FastAPI's dependency injection outside of any web/ASGI context.

FastAPI ships a powerful dependency injection system — Depends, sub-dependencies, yield teardown, per-resolution caching — but it is tightly coupled to the request/response cycle. fastapi-standalone-di reuses that exact machinery so you can resolve and invoke your dependencies from plain Python: CLI scripts, workers, cron jobs, tests — no HTTP server required.

Install

pip install fastapi-standalone-di
# or
uv add fastapi-standalone-di

Usage

Resolve any callable that uses Depends(), exactly as FastAPI would:

import asyncio

from fastapi import Depends

from fastapi_standalone_di import FastAPIContainer


def get_settings() -> dict[str, str]:
    return {"db_url": "postgres://localhost/app"}


class Database:
    def __init__(self, settings: dict[str, str] = Depends(get_settings)) -> None:
        self.url = settings["db_url"]


async def main() -> None:
    async with FastAPIContainer() as container:
        db = await container.get(Database)
        print(db.url)  # postgres://localhost/app


asyncio.run(main())

Sub-dependencies, yield teardown, caching, scopes, dependency overrides, registrable interfaces, per-package binding discovery, shared app state, application-lifetime singletons, standalone Request/Response stubs, and query/path/header/cookie parameters are all covered in the documentation.

Documentation

Full documentation is available at toilal.github.io/fastapi-standalone-di. The in-development docs (built from develop) are previewed at toilal.github.io/fastapi-standalone-di/dev/.

Requirements

  • Python ≥ 3.11
  • FastAPI ≥ 0.61

License

MIT © Rémi Alvergnat

CHANGELOG

v0.10.0 (2026-07-09)

Features

  • Wire @provides factory functions in auto_bindings (#55, 4c4f5ff)

v0.9.0 (2026-07-09)

Features

  • Wire lazy @singleton implementations in auto_bindings (#54, 1903d90)

v0.8.0 (2026-07-09)

Features

  • Pre-filter modules by AST before importing in auto_bindings (#53, 61f1ea1)

v0.7.0 (2026-07-09)

Bug Fixes

  • Detect cyclic dependency resolution instead of deadlocking (#50, 90d3e06)

Features

  • Discover @singleton implementations in auto_bindings (#48, 9468319)

v0.6.0 (2026-07-08)

Features

  • Auto_bindings() to wire interfaces to implementations by convention (#46, c7705de)

  • Auto_bindings() — wire interfaces to implementations by convention (#46, c7705de)

v0.5.1 (2026-07-08)

Bug Fixes

  • Bind the passed package's own module in register_bindings (#44, 8ee830f)

  • Wire a package's own binding module in register_bindings (#44, 8ee830f)

v0.5.0 (2026-07-08)

Features

  • Make lazy singletons usable as ASGI route dependencies (#42, bebe3e8)

v0.4.1 (2026-07-08)

Bug Fixes

  • Patch fastapi.params.Depends in place for registrable support (#40) (#41, 08927a5)

v0.4.0 (2026-07-08)

Features

  • AppState-backed application singletons (ASGI + standalone) (#39, a87d11f)

v0.3.0 (2026-07-07)

Features

  • Discover per-package di.py modules to auto-register bindings (#37, 02fc05b)

v0.2.0 (2026-07-05)

Features

Changelog entries are generated automatically by python-semantic-release from Conventional Commits on release.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastapi_standalone_di-0.10.0.tar.gz (170.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastapi_standalone_di-0.10.0-py3-none-any.whl (36.7 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_standalone_di-0.10.0.tar.gz.

File metadata

  • Download URL: fastapi_standalone_di-0.10.0.tar.gz
  • Upload date:
  • Size: 170.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.28 {"installer":{"name":"uv","version":"0.11.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for fastapi_standalone_di-0.10.0.tar.gz
Algorithm Hash digest
SHA256 d795014d28e91344dbddf4b850d259ee26b67f4cbdedcd3361623d866811b3c5
MD5 019d6d4e42f101a3c7681bf543eece30
BLAKE2b-256 4866bc5de43878d19687683fd07e180326c104063805a73ef24535bb6c0d4361

See more details on using hashes here.

File details

Details for the file fastapi_standalone_di-0.10.0-py3-none-any.whl.

File metadata

  • Download URL: fastapi_standalone_di-0.10.0-py3-none-any.whl
  • Upload date:
  • Size: 36.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.28 {"installer":{"name":"uv","version":"0.11.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for fastapi_standalone_di-0.10.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fea4723dddb78fa8af4ec10c7f127386cc251cee4befb9f44c37dd36c445b104
MD5 aaa320ef9d59e69c548228d650250bb9
BLAKE2b-256 ead927d4ec7f5fd4bca3691e11c904f2903061b65d778d2a4df829c6d19369cc

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page