router

View as MarkdownOpen in Claude

Return an APIRouter exposing this gateway, ready to hand to include_router. It serves POST / for the four browser methods and OPTIONS / for the CORS preflight.

The router returns chat bodies with StreamingResponse; the public endpoint may already have buffered the response it received from the AI chat service. A newly minted handle is returned in the X-Chat-Handle header.

Parameters

None.

Returns

fastapi.APIRouter.

Example

Mount it on an app you already run:

import os
from fastapi import FastAPI
from signalwire.ai_chat import ChatGateway
app = FastAPI()
gateway = ChatGateway(
config_url="https://bayview-taxi.example.com/swml",
key="pk_your_publishable_key",
secret=os.environ["SIGNALWIRE_CHAT_GATEWAY_SECRET"],
)
app.include_router(gateway.router(), prefix="/chat")

An agent already serves a FastAPI app, so it can host the gateway too. Use mount(), which keeps the new routes ahead of the agent’s catch-all:

import os
agent = MyAgent()
gateway = ChatGateway(
config_url="https://bayview-taxi.example.com/swml",
secret=os.environ["SIGNALWIRE_CHAT_GATEWAY_SECRET"],
)
agent.mount(gateway.router(), prefix="/chat")
agent.run()

To let the widget move a conversation to a phone call and back, mount a HandoffRouter at the same prefix.