as_router

View as MarkdownOpen in Claude

Create a FastAPI APIRouter for this service. Use this to mount the service into an existing FastAPI application rather than running a standalone server with serve(). The router handles both GET and POST requests at the root path and any registered routing callback paths.

Use as_router() for production deployments where you want to control the ASGI application lifecycle, or when hosting multiple services on the same FastAPI instance via AgentServer.

Returns

APIRouter — A FastAPI router with all endpoints registered.

Example

1from fastapi import FastAPI
2from signalwire import SWMLService
3
4app = FastAPI()
5
6service = SWMLService(name="ivr", route="/ivr")
7service.add_verb("answer", {})
8service.add_verb("play", {"url": "https://example.com/welcome.mp3"})
9
10app.include_router(service.as_router(), prefix="/ivr")
11
12# Run with: uvicorn myapp:app --host 0.0.0.0 --port 3000