FastAPI
Dependency injection for FastAPI is available in the wireup.integration.fastapi_integration
module.
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
Inject()
.
- Eliminates the need for
- Can: Mix FastAPI dependencies and Wireup in views
- Can: Autowire 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 configuration to the container. See configuration docs for more details.
Examples
app = FastAPI()
@app.get("/random")
async def target(
# Inject annotation tells wireup that this argument should be injected.
random_service: Annotated[RandomService, Inject()],
is_debug: Annotated[bool, Inject(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,
}
# Update container configuration.
# See Configuration page for more information.
container.params.update(Settings().model_dump())
# Initialize the integration.
# Must be called after 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])