> For a complete index of all SignalWire documentation pages, fetch https://signalwire.com/docs/llms.txt

# router

> Build the APIRouter that exposes the gateway to a browser.

[ref-chatgateway]: /docs/server-sdks/reference/python/agents/chat-gateway

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:

```python {11}
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:

```python {4}
import os

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

agent.get_app().include_router(gateway.router(), prefix="/chat")
agent.run()
```