Skip to content

FastAPI

Dependency injection for FastAPI (all versions) is available via the first-party integration wireup provides, available in wireup.integration.fastapi_integration.

Features:

  • Automatically decorate Flask views and blueprints where the container is being used.
    • Eliminates the need for @container.autowire in views.
    • Views without container references will not be decorated.
    • Services must be annotated with Wire().
  • Can: Mix FastAPI dependencies and Wireup in views
  • Can: Autowire any FastAPI target with @container.autowire.
  • Cannot: Use FastAPI dependencies in Wireup service objects.

Tip

As FastAPI does not have a fixed configuration mechanism, you need to expose any configuration objects to the container using one of the two options:

  • By dumping all values in the parameter bag.
  • By registering the configuration object as a service using a factory function.

Examples

app = FastAPI()

@app.get("/random")
async def target(
    # Wire annotation tells wireup that this argument should be injected.
    random_service: Annotated[RandomService, Wire()],
    is_debug: Annotated[bool, Wire(param="env.debug")],

    # This is a regular FastAPI dependency.
    lucky_number: Annotated[int, Depends(get_lucky_number)]
):
    return {
      "number": random_service.get_random(), 
      "lucky_number": lucky_number,
      "is_debug": is_debug,
    }

# Initialize the integration.
# Must be called after all views have been registered.
# Pass to service_modules a list of top-level modules where your services reside.
wireup_init_fastapi_integration(app, service_modules=[services])

Api Reference