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

CallHandler — the same handler, returned to support decorator-style usage (e.g., TypeScript 5 decorators or manual composition).

Example

import { RelayClient } from '@signalwire/sdk';
const client = new RelayClient({
project: process.env.SIGNALWIRE_PROJECT_ID!,
token: process.env.SIGNALWIRE_API_TOKEN!,
contexts: ['default']
});
client.onCall(async (call) => {
console.log(`Received call: ${call.callId}`);
await call.answer();
const action = await call.play([{ type: 'tts', text: 'Hello!' }]);
await action.wait();
await call.hangup();
});
await client.run();