connectPhone

View as Markdown

connectPhone

  • connectPhone(params): Promise<Call>

Attempt to connect an existing call to a new outbound phone call. The two devices will hear each other. You can wait until the new peer is disconnected by calling disconnected.

Parameters

params
objectRequired

Object containing the parameters for connecting the call to a phone number.

to
stringRequired

The party you are attempting to call.

from
string

The party the call is coming from. Must be a SignalWire number or SIP endpoint that you own.

timeout
number

The time, in seconds, the call will ring before it is considered unanswered.

maxPricePerMinute
number

The maximum price in USD acceptable for the call to be created. If the rate for the call is greater than this value, the call will not be created. If not set, all calls will be created. Price can have a maximum of four decimal places, i.e. 0.0075.

ringback
VoicePlaylist

Ringback audio to play to the call leg. You can play audio, TTS, silence or ringtone. See VoicePlaylist.

callStateUrl
string

Webhook URL to which SignalWire will send call status change notifications. See the payload specifications under CallState.

callStateEvents
string[]Defaults to ["ended"]

An array of event names to be notified about. Allowed values are created, ringing, answered, and ended.

Returns

Promise<Call>

A promise that resolves to a Call object that you can use to control the new peer. The promise resolves only after the new peer picks up the call.

Example

In this example, we connect an inbound call to an outbound phone number. We play a ringback tone to the inbound call leg while waiting for the outbound phone number to answer. Once the outbound phone number answers, we wait for the peer to hangup and then hangup the inbound leg of the call.

1import { SignalWire, Voice} from "@signalwire/realtime-api";
2
3const client = await SignalWire({ project: "ProjectID Here", token: "Token Here" })
4
5const voiceClient = client.voice;
6// Ringback tone
7const ringback = new Voice.Playlist().add(
8 Voice.Playlist.Ringtone({
9 name: "it"
10 })
11);
12
13await voiceClient.listen({
14 topics: ["office"],
15 onCallReceived: async (call) => {
16
17 // Answer the call
18 call.answer();
19 // Connect the call to the device builder plan
20 let peer = await call.connectPhone({
21 // Replace the to parameter with a valid phone number
22 to: "+1XXXXXXXXXX",
23 from: call.from,
24 ringback: ringback
25 });
26
27 // Listen to peer state changes
28 await peer.listen({
29 onStateChanged: (state) => {
30 console.log("Peer state changed:", state.state);
31 }
32 })
33 // wait for peer to hangup
34 await peer.disconnected();
35 console.log("The peer hungup");
36 // hangup the call
37 call.hangup();
38 }
39});