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.

The router also registers the GET/POST /swaig endpoint alongside the root and routing-callback routes, so SWAIG tools hosted on the service are reachable when it is mounted this way.

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

HostAppRouter — A FastAPI APIRouter subclass with all endpoints registered. It adds no behavior; the subclass exists to give the return type a stable name.

Example

from fastapi import FastAPI
from signalwire import SWMLService
app = FastAPI()
service = SWMLService(name="ivr", route="/ivr")
service.add_verb("answer", {})
service.add_verb("play", {"url": "https://example.com/welcome.mp3"})
app.include_router(service.as_router(), prefix="/ivr")
# Run with: uvicorn myapp:app --host 0.0.0.0 --port 3000