registerRoutingCallback

View as MarkdownOpen in Claude

Register a callback for dynamic request routing. When a request arrives at path, the callback inspects the POST body and decides whether to redirect the request to a different route or let normal SWML serving continue.

This is primarily used for SIP-based routing where the destination depends on the incoming SIP URI. For routing on agent name alone, use enableSipRouting() instead.

Parameters

callback
RoutingCallbackRequired

Receives the parsed request body and, optionally, the request headers. Return a route string to redirect the request there (HTTP 307, preserving the POST body), or return null or undefined to serve this agent’s own SWML.

path
stringDefaults to /sip

HTTP path where the routing endpoint is registered. Normalized to start with /, with trailing slashes stripped.

Returns

AgentBase — Returns this for method chaining.

Example

import { AgentBase } from '@signalwire/sdk';
const agent = new AgentBase({ name: 'router', route: '/' });
agent.setPromptText('You are a helpful assistant.');
agent.registerRoutingCallback((body) => {
const username = AgentBase.extractSipUsername(body);
if (username === 'sales') return '/agents/sales';
if (username === 'support') return '/agents/support';
return null;
}, '/sip');
await agent.run();