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 Deepgram Voice Agent once the callee answers.

⬇️ Jump to the Full VoxEngine scenario.

Prerequisites

  • Create a routing rule with this scenario attached (outgoing calls don’t use patterns) so you can run it from the Control Panel or trigger it via API: https://voximplant.com/docs/getting-started/basic-concepts/routing-rules
  • Have a PSTN destination number you can dial (E.164 format), for example your mobile number.
  • Configure a valid outbound callerId (for example, a rented Voximplant number or a verified caller ID): https://voximplant.com/docs/getting-started/basic-concepts/phone-numbers
  • Pass destination and callerId as routing rule custom data (this example reads them from VoxEngine.customData()): {"destination":"+15551234567","callerId":"+15557654321"}.
  • Store your Deepgram API key secret value in Voximplant ApplicationStorage under DEEPGRAM_API_KEY.
About outbound Caller ID

VoxEngine.callPSTN(...) requires a valid callback-capable caller ID (for example, a rented Voximplant number or a verified caller ID). See https://voximplant.com/docs/getting-started/basic-concepts/phone-numbers.

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

Place the outbound call

Outbound calls are placed with VoxEngine.callPSTN(number, callerid, parameters?).

In the full example, see VoxEngine.customData(), the destination / callerId parse, and the AppEvents.Started handler:

Place the call
1call = VoxEngine.callPSTN(destination, callerId);
2call.addEventListener(CallEvents.Connected, async () => {
3 // ...
4});

Session setup

As in the inbound example, the Deepgram Voice Agent session is configured via a settingsOptions object passed to Deepgram.createVoiceAgentClient(...).

In the full example, see SETTINGS_OPTIONS.agent:

  • listen: speech-to-text provider
  • think: LLM provider + prompt
  • speak: text-to-speech provider

Connect call audio

For outbound, it’s typically best to create the Voice Agent and bridge audio only after the callee answers (so the agent doesn’t speak into ringback).

In the example, the CallEvents.Connected handler does:

Create the client and bridge audio
1voiceAgentClient = await Deepgram.createVoiceAgentClient({ apiKey, settingsOptions: SETTINGS_OPTIONS });
2VoxEngine.sendMediaBetween(call, voiceAgentClient);

Barge-in

To keep the conversation interruption-friendly, the example listens for Deepgram.VoiceAgentEvents.UserStartedSpeaking and clears the media buffer:

Barge-in
1voiceAgentClient.addEventListener(Deepgram.VoiceAgentEvents.UserStartedSpeaking, () => {
2 voiceAgentClient.clearMediaBuffer();
3});

Events

The scenario logs a transcript example via Deepgram.VoiceAgentEvents.ConversationText and also logs a small set of lifecycle/debug events.

Available events are documented in the Voximplant references:

Notes

See the VoxEngine API Reference for more details.

Full VoxEngine scenario

voxeengine-deepgram-place-outbound-call.js
1/**
2 * Voximplant + Deepgram Voice Agent connector demo
3 * Scenario: place an outbound PSTN call and bridge it to Deepgram Voice Agent.
4 */
5
6require(Modules.Deepgram);
7require(Modules.ApplicationStorage);
8
9const SYSTEM_PROMPT = `
10You are Voxi, a helpful voice assistant for phone callers.
11Keep responses short and telephony-friendly (usually 1-2 sentences).
12`;
13
14// -------------------- Deepgram Voice Agent settings --------------------
15const SETTINGS_OPTIONS = {
16 tags: ["voximplant", "deepgram", "voice_agent_connector", "outbound_call_demo"],
17 agent: {
18 language: "en",
19 greeting: "Hi! This is Voxi from Voximplant. How can I help today?",
20 listen: {
21 provider: {
22 type: "deepgram",
23 model: "flux-general-en",
24 },
25 },
26 think: {
27 provider: {
28 type: "open_ai",
29 model: "gpt-4o-mini",
30 },
31 prompt: SYSTEM_PROMPT,
32 },
33 speak: {
34 provider: {
35 type: "deepgram",
36 model: "aura-2-cordelia-en",
37 },
38 },
39 },
40};
41
42VoxEngine.addEventListener(AppEvents.Started, async () => {
43 let call;
44 let voiceAIClient;
45
46 try {
47 // This can be provided when manually running a routing rule in the Control Panel,
48 // or via Management API using the `script_custom_data` parameter.
49 // example: {"destination": "+15551234567","callerId": "+15557654321"}
50 const {destination, callerId} = JSON.parse(VoxEngine.customData());
51
52 // Place the outbound call
53 call = VoxEngine.callPSTN(destination, callerId);
54
55 // Termination functions - add cleanup and logging as needed
56 call.addEventListener(CallEvents.Failed, ()=>VoxEngine.terminate());
57 call.addEventListener(CallEvents.Disconnected, ()=>VoxEngine.terminate());
58
59 call.addEventListener(CallEvents.Connected, async () => {
60 // Optional: record once connected
61 call.record({hd_audio: true, stereo: true});
62
63 // Create client and wire media after the callee answers
64 const apiKey = (await ApplicationStorage.get("DEEPGRAM_API_KEY")).value;
65 voiceAIClient = await Deepgram.createVoiceAgentClient({
66 apiKey,
67 settingsOptions: SETTINGS_OPTIONS,
68 });
69 VoxEngine.sendMediaBetween(call, voiceAIClient);
70
71 // Barge-in: keep conversation responsive
72 voiceAIClient.addEventListener(Deepgram.VoiceAgentEvents.UserStartedSpeaking, () => {
73 Logger.write("===BARGE-IN: Deepgram.VoiceAgentEvents.UserStartedSpeaking===");
74 voiceAIClient.clearMediaBuffer();
75 });
76
77 // Capture transcript
78 voiceAIClient.addEventListener(Deepgram.VoiceAgentEvents.ConversationText, (event) => {
79 const {role, text} = event?.data?.payload || {};
80 if (role && text) Logger.write(`===TRANSCRIPT=== ${role}: ${text}`);
81 });
82
83 // Consolidated "log-only" handlers - key Deepgram/VoxEngine debugging events
84 [
85 Deepgram.VoiceAgentEvents.Welcome,
86 Deepgram.VoiceAgentEvents.SettingsApplied,
87 Deepgram.VoiceAgentEvents.AgentThinking,
88 Deepgram.VoiceAgentEvents.AgentAudioDone,
89 Deepgram.VoiceAgentEvents.ConnectorInformation,
90 Deepgram.VoiceAgentEvents.HTTPResponse,
91 Deepgram.VoiceAgentEvents.Warning,
92 Deepgram.VoiceAgentEvents.Error,
93 Deepgram.VoiceAgentEvents.Unknown,
94 Deepgram.Events.WebSocketMediaStarted,
95 Deepgram.Events.WebSocketMediaEnded,
96 ].forEach((eventName) => {
97 voiceAIClient.addEventListener(eventName, (event) => {
98 Logger.write(`===${event.name}===`);
99 Logger.write(JSON.stringify(event));
100 });
101 });
102 });
103 } catch (e) {
104 Logger.write("===UNHANDLED_ERROR===");
105 Logger.write(e);
106 voiceAIClient?.close();
107 VoxEngine.terminate();
108 }
109});