***

title: register_routing_callback
slug: /reference/python/agents/agent-base/register-routing-callback
description: Register a callback for dynamic request routing based on SIP URIs or POST data.
max-toc-depth: 3
---------------------

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

[enable-sip-routing]: /docs/server-sdks/reference/python/agents/agent-base/enable-sip-routing

[ref-agentbase]: /docs/server-sdks/reference/python/agents/agent-base

Register a callback function for dynamic request routing. When a request arrives at
the specified path, the callback inspects the POST body and decides whether to route
the request to a different endpoint or let normal processing continue.

This is primarily used for SIP-based routing where the destination depends on the
incoming SIP URI. For simpler SIP routing based on agent name matching, use
[`enable_sip_routing()`][enable-sip-routing] instead.

<Note>
  The callback path is registered immediately but the actual FastAPI route is created
  when `serve()` is called or when `as_router()` generates the router. Register all
  callbacks before starting the server.
</Note>

## **Parameters**

<ParamField path="callback_fn" type="Callable[[Request, dict[str, Any]], Optional[str]]" required={true} toc={true}>
  A function that receives a FastAPI `Request` and the parsed JSON body as a `dict`.
  Return a route string to redirect the request (using HTTP 307 to preserve the POST
  method and body), or return `None` to continue with normal SWML document serving.
</ParamField>

<ParamField path="path" type="str" default="/sip" toc={true}>
  The URL path where this routing endpoint is created. The path is normalized to start
  with `/` and trailing slashes are stripped.
</ParamField>

## **Returns**

`None`

## **Example**

```python {15}
from signalwire import AgentBase
from signalwire.core.swml_service import SWMLService

agent = AgentBase(name="router", route="/")
agent.set_prompt_text("You are a helpful assistant.")

def route_sip_call(request, body):
    username = SWMLService.extract_sip_username(body)
    if username == "sales":
        return "/agents/sales"
    elif username == "support":
        return "/agents/support"
    return None

agent.register_routing_callback(route_sip_call, path="/sip")
agent.serve()
```