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:

1import os
2
3from fastapi import FastAPI
4from signalwire.ai_chat import ChatGateway
5
6app = FastAPI()
7
8gateway = ChatGateway(
9 config_url="https://bayview-taxi.example.com/swml",
10 key="pk_your_publishable_key",
11 secret=os.environ["SIGNALWIRE_CHAT_GATEWAY_SECRET"],
12)
13
14app.include_router(gateway.router(), prefix="/chat")

An agent already serves a FastAPI app, so it can host the gateway too:

1import os
2
3agent = MyAgent()
4gateway = ChatGateway(
5 config_url="https://bayview-taxi.example.com/swml",
6 secret=os.environ["SIGNALWIRE_CHAT_GATEWAY_SECRET"],
7)
8
9agent.get_app().include_router(gateway.router(), prefix="/chat")
10agent.run()