Example: Placing an outbound call

View as Markdown

For the complete documentation index, see llms.txt.

Overview

This example starts a VoxEngine session, places an outbound PSTN call, and connects the callee to Gemini Live API when the session is ready.

⬇️ Jump to the Full VoxEngine scenario.

Gemini 3.1 Flash Live Preview

This page reflects the current gemini-3.1-flash-live-preview flow from Google’s Live API docs: https://ai.google.dev/gemini-api/docs/models/gemini-3.1-flash-live-preview

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 Gemini API key in Voximplant ApplicationStorage under GEMINI_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

Alternate outbound destinations

This example uses VoxEngine.callPSTN(...) for PSTN dialing. You can also route outbound calls to other destination types in VoxEngine:

  • SIP (VoxEngine.callSIP): dial a SIP URI to reach a PBX, carrier, SIP trunk, or other SIP endpoint.
  • WhatsApp (VoxEngine.callWhatsappUser): place a WhatsApp Business-initiated call (requires a WhatsApp Business account and enabled numbers).
  • Voximplant users (VoxEngine.callUser): calls another app user inside the same Voximplant application such as web SDK, mobile SDK, or SIP user.

Relevant guides:

Notes

  • The example uses the Gemini Developer API (Gemini.Backend.GEMINI_API), not Vertex AI.
  • The current sample uses gemini-3.1-flash-live-preview.
  • Audio is connected after Gemini.LiveAPIEvents.SetupComplete fires.
Gemini 2.5 compatibility

If you are adapting an older 2.5 outbound sample, use sendRealtimeInput(...) for the initial prompt on 3.1. The current 3.1 sample also uses thinkingConfig: { thinkingLevel: "minimal" }.

See the VoxEngine API Reference for more details.

Full VoxEngine scenario

voxeengine-gemini-place-outbound-call.js
1/**
2 * Voximplant + Gemini Live API connector demo
3 * Scenario: place an outbound PSTN call and bridge it to Gemini Live API.
4 */
5
6require(Modules.Gemini);
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// -------------------- Gemini Live API settings --------------------
15const CONNECT_CONFIG = {
16 responseModalities: ["AUDIO"],
17 thinkingConfig: {thinkingLevel: "minimal"},
18 systemInstruction: {
19 parts: [{text: SYSTEM_PROMPT}],
20 },
21 inputAudioTranscription: {},
22 outputAudioTranscription: {},
23};
24
25VoxEngine.addEventListener(AppEvents.Started, async () => {
26 let voiceAIClient;
27 let call;
28
29 try {
30 // This can be provided when manually running a routing rule in the Control Panel,
31 // or via Management API using the `script_custom_data` parameter.
32 // example: {"destination": "+15551234567","callerId": "+15557654321"}
33 const {destination, callerId} = JSON.parse(VoxEngine.customData());
34
35 // Place the outbound call
36 call = VoxEngine.callPSTN(destination, callerId);
37 // Alternative outbound paths (uncomment to use):
38 // call = VoxEngine.callUser({username: destination, callerid: callerId});
39 // call = VoxEngine.callSIP(`sip:${destination}@your-sip-domain`, callerId);
40 // call = VoxEngine.callWhatsappUser({number: destination, callerid: callerId});
41
42 // Termination functions - add cleanup and logging as needed
43 call.addEventListener(CallEvents.Disconnected, VoxEngine.terminate);
44 call.addEventListener(CallEvents.Failed, VoxEngine.terminate);
45
46 call.addEventListener(CallEvents.Connected, async () => {
47 // call.record({ hd_audio: true, stereo: true }); // Optional: record the call
48
49 voiceAIClient = await Gemini.createLiveAPIClient({
50 apiKey: (await ApplicationStorage.get("GEMINI_API_KEY")).value,
51 model: "gemini-3.1-flash-live-preview",
52 backend: Gemini.Backend.GEMINI_API,
53 connectConfig: CONNECT_CONFIG,
54 onWebSocketClose: () => {
55 Logger.write(`===Gemini.WebSocket.Close===`);
56 VoxEngine.terminate();
57 },
58 });
59
60 voiceAIClient.addEventListener(Gemini.LiveAPIEvents.SetupComplete, () => {
61 VoxEngine.sendMediaBetween(call, voiceAIClient);
62 voiceAIClient.sendRealtimeInput({
63 text: "Say hello and ask how you can help.",
64 });
65 });
66
67 // Capture transcripts + handle barge-in
68 voiceAIClient.addEventListener(Gemini.LiveAPIEvents.ServerContent, (event) => {
69 const payload = event?.data?.payload || {};
70 if (payload.inputTranscription?.text) {
71 Logger.write(`===USER=== ${payload.inputTranscription.text}`);
72 }
73 if (payload.outputTranscription?.text) {
74 Logger.write(`===AGENT=== ${payload.outputTranscription.text}`);
75 }
76 if (payload.interrupted) {
77 Logger.write("===BARGE-IN=== Gemini.LiveAPIEvents.ServerContent");
78 voiceAIClient.clearMediaBuffer();
79 }
80 });
81
82 // Log all Gemini events for illustration/debugging
83 [
84 Gemini.LiveAPIEvents.SetupComplete,
85 Gemini.LiveAPIEvents.ServerContent,
86 Gemini.LiveAPIEvents.ToolCall,
87 Gemini.LiveAPIEvents.ToolCallCancellation,
88 Gemini.LiveAPIEvents.ConnectorInformation,
89 Gemini.LiveAPIEvents.Unknown,
90 Gemini.Events.WebSocketMediaStarted,
91 Gemini.Events.WebSocketMediaEnded,
92 ].forEach((eventName) => {
93 voiceAIClient.addEventListener(eventName, (event) => {
94 Logger.write(`===${event.name}===`);
95 if (event?.data) Logger.write(JSON.stringify(event.data));
96 });
97 });
98 });
99 } catch (error) {
100 Logger.write("===SOMETHING_WENT_WRONG===");
101 Logger.write(error);
102 voiceAIClient?.close();
103 VoxEngine.terminate();
104 }
105});