Skip to content

FastAPI

wireup.integration.fastapi

Attributes

inject = inject_from_container_unchecked(get_request_container, hide_annotated_names=True) module-attribute

Inject dependencies into request-time FastAPI helpers.

Use this for non-route functions that still run during a request lifecycle, such as custom decorators, dependency helper functions, and middleware helpers.

Classes

Functions

get_app_container(app)

Return the container associated with the given application.

This is the instance created via wireup.create_async_container. Use this when you need the container outside of the request context lifecycle.

get_request_container()

When inside a request, returns the scoped container instance handling the current request.

This is what you almost always want.It has all the information the app container has in addition to data specific to the current request.

request_factory()

Provide the current request as a dependency.

setup(container, app, *, class_based_handlers=None, middleware_mode=False)

Integrate Wireup with FastAPI.

Setup performs the following: * Injects dependencies into HTTP and WebSocket routes. * Closes the Wireup container upon app shutdown using the lifespan context.

Parameters:

Name Type Description Default
container AsyncContainer

An async container created via wireup.create_async_container.

required
app FastAPI

The FastAPI application to integrate with. Best practice is to call setup after routes are added. If setup is called earlier, ensure FastAPI lifespan runs before handling requests (for example, use TestClient as a context manager in tests).

required
class_based_handlers Optional[Iterable[Type[_ClassBasedHandlersProtocol]]]

A list of class-based handlers to register. These classes must have a router attribute of type fastapi.APIRouter. Warning: Do not include these with fastapi directly.

None
middleware_mode bool

If True, the container is exposed in fastapi middleware. Note, for this to work correctly, there should be no more middleware added after the call to this function. For more details, visit: https://maldoinc.github.io/wireup/latest/integrations/fastapi/

False

websocket_factory()

Provide the current WebSocket as a dependency.