Example: Placing an outbound call

View as Markdown

This example starts a VoxEngine session, places an outbound PSTN call, and bridges audio to ElevenLabs Agents once the callee answers.

Jump to the Full VoxEngine scenario.

Prerequisites

  • Store your ElevenLabs API key in Voximplant ApplicationStorage under ELEVENLABS_API_KEY.
  • Store your ElevenLabs Agent ID in Voximplant ApplicationStorage under ELEVENLABS_AGENT_ID.
  • Ensure outbound calling is enabled for your Voximplant application and that your caller ID is verified.

Outbound call parameters

The example expects destination and caller ID in customData (read via VoxEngine.customData()):

Custom data example
1{"destination":"+15551234567","callerId":"+15557654321"}

Launch the routing rule

For quick testing, you can start this outbound scenario from the Voximplant Control Panel:

  1. Open your Voximplant application and go to the Routing tab.
  2. Select the routing rule that has this scenario attached.
  3. Click Run.
  4. Provide Custom data (max 200 bytes) with destination and callerId:
Custom data example
1{"destination":"+15551234567","callerId":"+15557654321"}

For production, start the routing rule via Management API startScenarios (pass rule_id, and pass the same JSON string in script_custom_data): https://voximplant.com/docs/references/httpapi/scenarios#startscenarios

Connect call audio

After the callee answers, the example creates an ElevenLabs.AgentsClient and bridges audio:

Connect call audio
1VoxEngine.sendMediaBetween(call, agentsClient);

Barge-in

Barge-in
1agentsClient.addEventListener(ElevenLabs.AgentsEvents.Interruption, () => {
2 agentsClient.clearMediaBuffer();
3});

Notes

See the VoxEngine API Reference for more details.

Full VoxEngine scenario

voxeengine-elevenlabs-outbound.js
1/**
2 * Voximplant + ElevenLabs Agents connector demo
3 * Scenario: place an outbound PSTN call and bridge it to ElevenLabs Agents.
4 */
5
6require(Modules.ElevenLabs);
7require(Modules.ApplicationStorage);
8
9const MAX_CALL_MS = 2 * 60 * 1000; // Maximum call duration of 2 minutes
10
11VoxEngine.addEventListener(AppEvents.Started, async () => {
12 let call;
13 let voiceAIClient;
14 let hangupTimer;
15
16 try {
17 // This can be provided when manually running a routing rule in the Control Panel,
18 // or via Management API using the `script_custom_data` parameter.
19 // example: {"destination": "+15551234567", "callerId": "+15557654321"}
20 const {destination, callerId} = JSON.parse(VoxEngine.customData());
21
22 call = VoxEngine.callPSTN(destination, callerId);
23 // Alternative outbound paths (uncomment to use):
24 // call = VoxEngine.callUser({username: destination, callerid: callerId});
25 // call = VoxEngine.callSIP(`sip:${destination}@your-sip-domain`, callerId);
26 // call = VoxEngine.callWhatsappUser({number: destination, callerid: callerId});
27
28 call.addEventListener(CallEvents.Failed, () => VoxEngine.terminate());
29 call.addEventListener(CallEvents.Disconnected, () => {
30 if (hangupTimer) clearTimeout(hangupTimer);
31 VoxEngine.terminate();
32 });
33
34 call.addEventListener(CallEvents.Connected, async () => {
35 hangupTimer = setTimeout(() => {
36 Logger.write("===HANGUP_TIMER===");
37 call.hangup();
38 }, MAX_CALL_MS);
39
40 // Create client and connect to ElevenLabs Agents
41 voiceAIClient = await ElevenLabs.createAgentsClient({
42 xiApiKey: (await ApplicationStorage.get("ELEVENLABS_API_KEY")).value,
43 agentId: (await ApplicationStorage.get("ELEVENLABS_AGENT_ID")).value,
44 onWebSocketClose: (event) => {
45 Logger.write("===ElevenLabs.WebSocket.Close===");
46 if (event) Logger.write(JSON.stringify(event));
47 VoxEngine.terminate();
48 },
49 });
50
51 // Bridge media between the call and ElevenLabs Agents
52 VoxEngine.sendMediaBetween(call, voiceAIClient);
53
54 // ---------------------- Event handlers -----------------------
55 // Barge-in: keep conversation responsive
56 voiceAIClient.addEventListener(ElevenLabs.AgentsEvents.Interruption, () => {
57 Logger.write("===BARGE-IN: ElevenLabs.AgentsEvents.Interruption===");
58 voiceAIClient.clearMediaBuffer();
59 });
60
61 voiceAIClient.addEventListener(ElevenLabs.AgentsEvents.UserTranscript, (event) => {
62 const payload = event?.data?.payload || event?.data || {};
63 const text = payload.text || payload.transcript || payload.user_transcript;
64 if (text) {
65 Logger.write(`===USER=== ${text}`);
66 } else {
67 Logger.write("===USER_TRANSCRIPT===");
68 Logger.write(JSON.stringify(payload));
69 }
70 });
71
72 voiceAIClient.addEventListener(ElevenLabs.AgentsEvents.AgentResponse, (event) => {
73 const payload = event?.data?.payload || event?.data || {};
74 const text = payload.text || payload.response || payload.agent_response;
75 if (text) {
76 Logger.write(`===AGENT=== ${text}`);
77 } else {
78 Logger.write("===AGENT_RESPONSE===");
79 Logger.write(JSON.stringify(payload));
80 }
81 });
82
83 // Consolidated "log-only" handlers - key ElevenLabs/VoxEngine debugging events
84 [
85 ElevenLabs.AgentsEvents.ConversationInitiationMetadata,
86 ElevenLabs.AgentsEvents.AgentResponseCorrection,
87 ElevenLabs.AgentsEvents.ContextualUpdate,
88 ElevenLabs.AgentsEvents.AgentToolResponse,
89 ElevenLabs.AgentsEvents.VadScore,
90 ElevenLabs.AgentsEvents.Ping,
91 ElevenLabs.AgentsEvents.HTTPResponse,
92 ElevenLabs.AgentsEvents.WebSocketError,
93 ElevenLabs.AgentsEvents.ConnectorInformation,
94 ElevenLabs.AgentsEvents.Unknown,
95 ElevenLabs.Events.WebSocketMediaStarted,
96 ElevenLabs.Events.WebSocketMediaEnded,
97 ].forEach((eventName) => {
98 voiceAIClient.addEventListener(eventName, (event) => {
99 Logger.write(`===${event.name}===`);
100 if (event?.data) Logger.write(JSON.stringify(event.data));
101 });
102 });
103 });
104 } catch (error) {
105 Logger.write("===UNHANDLED_ERROR===");
106 Logger.write(error);
107 voiceAIClient?.close();
108 VoxEngine.terminate();
109 }
110});