Skip to navigation

onRequest

View as MarkdownOpen in Claude

Passes a request body to onSwmlRequest() and returns its result: an object of SWML modifications, or void to render with the default behavior. A null or missing body is passed on as an empty object.

The agent’s request handler calls onSwmlRequest() directly, not onRequest. Overriding onRequest doesn’t change how served requests render. Override onSwmlRequest() to customize them, and call onRequest when you want the modifications for a request body you supply.

onRequest returns an object of SWML modifications to merge into the rendered document. To replace the entire SWML document for a request instead, use setOnRequestCallback() on SWMLService.

Parameters

requestData
SwmlRequestData | null

The request body. Call details sit under call, for example call.call_id and the caller’s address in call.from.

callbackPath
string | null

Optional routing-callback path, passed on to onSwmlRequest().

Returns

Record<string, unknown> | void (or a Promise of these) — An object of SWML modifications to merge before rendering, or void for default rendering.

Example

import { AgentBase, type SwmlRequestData } from '@signalwire/sdk';
class PersonalizedAgent extends AgentBase {
override onSwmlRequest(rawData: SwmlRequestData): Record<string, unknown> {
const caller = rawData.call?.from ?? 'unknown';
return { global_data: { caller_number: caller } };
}
}
const agent = new PersonalizedAgent({ name: 'personalized', route: '/' });
// Preview the modifications for a sample request body
const modifications = await agent.onRequest({
call: {
call_id: 'sample-call-id',
project_id: 'sample-project-id',
space_id: 'sample-space-id',
from: '+15551234567',
},
});
console.log(modifications); // { global_data: { caller_number: '+15551234567' } }