onCall

View as MarkdownOpen in Claude

Register the inbound call handler. The provided function is called once for each calling.call.receive event on the subscribed contexts. The function receives a Call object with all call properties and control methods.

Only one call handler can be active at a time. Calling onCall() again replaces the previous handler.

Parameters

handler
CallHandlerRequired

An async function that receives a Call object. Signature: (call: Call) => void | Promise<void>.

Returns

void

Example

1import { RelayClient } from '@signalwire/sdk';
2
3const client = new RelayClient({
4 project: process.env.SIGNALWIRE_PROJECT_ID!,
5 token: process.env.SIGNALWIRE_TOKEN!,
6 contexts: ['default']
7});
8
9client.onCall(async (call) => {
10 console.log(`Received call: ${call.callId}`);
11 await call.answer();
12 const action = await call.play([{ type: 'tts', text: 'Hello!' }]);
13 await action.wait();
14 await call.hangup();
15});
16
17await client.run();