Example: Placing an outbound call

View as MarkdownOpen in Claude

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.

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

Notes

  • The example uses the Gemini Developer API (Gemini.Backend.GEMINI_API), not Vertex AI.
  • Audio is connected after Gemini.LiveAPIEvents.SetupComplete fires.

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 systemInstruction: {
18 parts: [{text: SYSTEM_PROMPT}],
19 },
20 inputAudioTranscription: {},
21 outputAudioTranscription: {},
22};
23
24VoxEngine.addEventListener(AppEvents.Started, async () => {
25 let voiceAIClient;
26 let call;
27
28 try {
29 // This can be provided when manually running a routing rule in the Control Panel,
30 // or via Management API using the `script_custom_data` parameter.
31 // example: {"destination": "+15551234567","callerId": "+15557654321"}
32 const {destination, callerId} = JSON.parse(VoxEngine.customData());
33
34 // Place the outbound call
35 call = VoxEngine.callPSTN(destination, callerId);
36
37 // Termination functions - add cleanup and logging as needed
38 call.addEventListener(CallEvents.Disconnected, VoxEngine.terminate);
39 call.addEventListener(CallEvents.Failed, VoxEngine.terminate);
40
41 call.addEventListener(CallEvents.Connected, async () => {
42 // call.record({ hd_audio: true, stereo: true }); // Optional: record the call
43
44 voiceAIClient = await Gemini.createLiveAPIClient({
45 apiKey: (await ApplicationStorage.get("GEMINI_API_KEY")).value,
46 model: "gemini-2.5-flash-native-audio-preview-12-2025",
47 backend: Gemini.Backend.GEMINI_API,
48 connectConfig: CONNECT_CONFIG,
49 onWebSocketClose: () => {
50 Logger.write(`===Gemini.WebSocket.Close===`);
51 VoxEngine.terminate();
52 },
53 });
54
55 voiceAIClient.addEventListener(Gemini.LiveAPIEvents.SetupComplete, () => {
56 VoxEngine.sendMediaBetween(call, voiceAIClient);
57 voiceAIClient.sendClientContent({
58 turns: [{role: "user", parts: [{text: "Say hello and ask how you can help."}]}],
59 turnComplete: true,
60 });
61 });
62
63 // Capture transcripts + handle barge-in
64 voiceAIClient.addEventListener(Gemini.LiveAPIEvents.ServerContent, (event) => {
65 const payload = event?.data?.payload || {};
66 if (payload.inputTranscription?.text) {
67 Logger.write(`===USER=== ${payload.inputTranscription.text}`);
68 }
69 if (payload.outputTranscription?.text) {
70 Logger.write(`===AGENT=== ${payload.outputTranscription.text}`);
71 }
72 if (payload.interrupted) {
73 Logger.write("===BARGE-IN=== Gemini.LiveAPIEvents.ServerContent");
74 voiceAIClient.clearMediaBuffer();
75 }
76 });
77
78 // Log all Gemini events for illustration/debugging
79 [
80 Gemini.LiveAPIEvents.SetupComplete,
81 Gemini.LiveAPIEvents.ServerContent,
82 Gemini.LiveAPIEvents.ToolCall,
83 Gemini.LiveAPIEvents.ToolCallCancellation,
84 Gemini.LiveAPIEvents.ConnectorInformation,
85 Gemini.LiveAPIEvents.Unknown,
86 Gemini.Events.WebSocketMediaStarted,
87 Gemini.Events.WebSocketMediaEnded,
88 ].forEach((eventName) => {
89 voiceAIClient.addEventListener(eventName, (event) => {
90 Logger.write(`===${event.name}===`);
91 if (event?.data) Logger.write(JSON.stringify(event.data));
92 });
93 });
94 });
95 } catch (error) {
96 Logger.write("===SOMETHING_WENT_WRONG===");
97 Logger.write(error);
98 voiceAIClient?.close();
99 VoxEngine.terminate();
100 }
101});