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, shared app state, 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.12
  • FastAPI ≥ 0.61

License

MIT © Rémi Alvergnat

CHANGELOG

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.2.0.tar.gz (135.6 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.2.0-py3-none-any.whl (19.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fastapi_standalone_di-0.2.0.tar.gz
  • Upload date:
  • Size: 135.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.26 {"installer":{"name":"uv","version":"0.11.26","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.2.0.tar.gz
Algorithm Hash digest
SHA256 04c3db09adfcdc76e60e2c21fe3ce5cdca434308f8b97c08a85aa6c30008026d
MD5 c5f0ce8920d6653ed593e40a5f7cba5f
BLAKE2b-256 cadba17d2854f5f8e921aa9f19fd3b6488d92068f1beb9cf7bdbbd6b81dbe3f9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fastapi_standalone_di-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 19.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.26 {"installer":{"name":"uv","version":"0.11.26","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.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 dd608660a3193e12b861fa910a2b1c2dc8d23f5fce7993319e7f688c652681be
MD5 59fe360d01954c2738479b28bad69a97
BLAKE2b-256 67f022be255413532aed47e792abee927ed6939b255c3e3e78c591cea07882de

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