Example: Placing an outbound call

View as MarkdownOpen in Claude

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
24 call.addEventListener(CallEvents.Failed, () => VoxEngine.terminate());
25 call.addEventListener(CallEvents.Disconnected, () => {
26 if (hangupTimer) clearTimeout(hangupTimer);
27 VoxEngine.terminate();
28 });
29
30 call.addEventListener(CallEvents.Connected, async () => {
31 hangupTimer = setTimeout(() => {
32 Logger.write("===HANGUP_TIMER===");
33 call.hangup();
34 }, MAX_CALL_MS);
35
36 // Create client and connect to ElevenLabs Agents
37 voiceAIClient = await ElevenLabs.createAgentsClient({
38 xiApiKey: (await ApplicationStorage.get("ELEVENLABS_API_KEY")).value,
39 agentId: (await ApplicationStorage.get("ELEVENLABS_AGENT_ID")).value,
40 onWebSocketClose: (event) => {
41 Logger.write("===ElevenLabs.WebSocket.Close===");
42 if (event) Logger.write(JSON.stringify(event));
43 VoxEngine.terminate();
44 },
45 });
46
47 // Bridge media between the call and ElevenLabs Agents
48 VoxEngine.sendMediaBetween(call, voiceAIClient);
49
50 // ---------------------- Event handlers -----------------------
51 // Barge-in: keep conversation responsive
52 voiceAIClient.addEventListener(ElevenLabs.AgentsEvents.Interruption, () => {
53 Logger.write("===BARGE-IN: ElevenLabs.AgentsEvents.Interruption===");
54 voiceAIClient.clearMediaBuffer();
55 });
56
57 voiceAIClient.addEventListener(ElevenLabs.AgentsEvents.UserTranscript, (event) => {
58 const payload = event?.data?.payload || event?.data || {};
59 const text = payload.text || payload.transcript || payload.user_transcript;
60 if (text) {
61 Logger.write(`===USER=== ${text}`);
62 } else {
63 Logger.write("===USER_TRANSCRIPT===");
64 Logger.write(JSON.stringify(payload));
65 }
66 });
67
68 voiceAIClient.addEventListener(ElevenLabs.AgentsEvents.AgentResponse, (event) => {
69 const payload = event?.data?.payload || event?.data || {};
70 const text = payload.text || payload.response || payload.agent_response;
71 if (text) {
72 Logger.write(`===AGENT=== ${text}`);
73 } else {
74 Logger.write("===AGENT_RESPONSE===");
75 Logger.write(JSON.stringify(payload));
76 }
77 });
78
79 // Consolidated "log-only" handlers - key ElevenLabs/VoxEngine debugging events
80 [
81 ElevenLabs.AgentsEvents.ConversationInitiationMetadata,
82 ElevenLabs.AgentsEvents.AgentResponseCorrection,
83 ElevenLabs.AgentsEvents.ContextualUpdate,
84 ElevenLabs.AgentsEvents.AgentToolResponse,
85 ElevenLabs.AgentsEvents.VadScore,
86 ElevenLabs.AgentsEvents.Ping,
87 ElevenLabs.AgentsEvents.HTTPResponse,
88 ElevenLabs.AgentsEvents.WebSocketError,
89 ElevenLabs.AgentsEvents.ConnectorInformation,
90 ElevenLabs.AgentsEvents.Unknown,
91 ElevenLabs.Events.WebSocketMediaStarted,
92 ElevenLabs.Events.WebSocketMediaEnded,
93 ].forEach((eventName) => {
94 voiceAIClient.addEventListener(eventName, (event) => {
95 Logger.write(`===${event.name}===`);
96 if (event?.data) Logger.write(JSON.stringify(event.data));
97 });
98 });
99 });
100 } catch (error) {
101 Logger.write("===UNHANDLED_ERROR===");
102 Logger.write(error);
103 voiceAIClient?.close();
104 VoxEngine.terminate();
105 }
106});