Skip to content

FastAPI Integration

Inversipy provides seamless FastAPI integration with the @inject decorator.

Setup

from fastapi import FastAPI
from inversipy import Container, Inject
from inversipy.fastapi import bind, inject

app = FastAPI()
container = Container()
container.register(Database)
container.register(Logger)
bind(app, container)

Route Handlers

Use @inject to auto-resolve dependencies. Parameters marked with Inject[T] are resolved from the container; normal parameters are handled by FastAPI:

@app.get("/users")
@inject
async def get_users(
    db: Inject[Database],
    logger: Inject[Logger],
    limit: int = 10,
):
    logger.info(f"Fetching {limit} users")
    return db.query("SELECT * FROM users LIMIT ?", limit)

The @inject decorator:

  • Identifies parameters marked with Inject[Type]
  • Resolves them from the container automatically
  • Leaves normal FastAPI parameters (query params, path params, body, etc.) unchanged
  • Works with both sync and async route handlers

Installation

pip install inversipy fastapi