onRequest

View as MarkdownOpen in Claude

Public hook called on every inbound request before the SWML document is rendered. The default implementation delegates to onSwmlRequest(). Override it to return a dictionary of SWML modifications, or void to render with the default behavior.

Most subclasses override onSwmlRequest() instead, which is the conventional per-request customization hook. onRequest is the lower-level WebMixin parity entry point.

This hook returns a dictionary 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
Record<string, unknown> | null

The parsed request body. Contains call metadata such as call_id and caller_id_number.

callbackPath
string | null

Optional callback path from the request.

Returns

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

Example

import { AgentBase } from '@signalwire/sdk';
class PersonalizedAgent extends AgentBase {
override async onRequest(
requestData?: Record<string, unknown> | null,
): Promise<Record<string, unknown> | void> {
const caller = (requestData?.caller_id_number as string) ?? 'unknown';
return { global_data: { caller_number: caller } };
}
}
const agent = new PersonalizedAgent({ name: 'personalized', route: '/' });
await agent.serve();