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

1import { AgentBase } from '@signalwire/sdk';
2
3class PersonalizedAgent extends AgentBase {
4 override async onRequest(
5 requestData?: Record<string, unknown> | null,
6 ): Promise<Record<string, unknown> | void> {
7 const caller = (requestData?.caller_id_number as string) ?? 'unknown';
8 return { global_data: { caller_number: caller } };
9 }
10}
11
12const agent = new PersonalizedAgent({ name: 'personalized', route: '/' });
13await agent.serve();