Making and Receiving Phone Calls
This guide is available for Relay V3 and for Relay V4. You are currently viewing the guide for Relay V4. To switch to the older Relay V3 guide, please use the selection field below:
This introductory guide will show you how to make and receive calls from your own Node.js application.
Obtaining and configuring a number
Log in to your SignalWire Space. From the Phone Numbers section, you can buy a new phone number. You will need at least one number to make and receive calls. After you have acquired a number, open its settings by clicking on "Edit Settings". Scroll down until you reach "Voice and Fax Settings", as shown in the next figure, and configure it to:
- handle incoming calls using a Relay application,
- forward the call to the "office" Relay context
What is a context?
Contexts are used to segregate messages and calls. If you associate a group of phone numbers to context A and a different group of numbers to context B, then from your code you can decide whether to receive events (such as incoming calls or messages) only for the numbers in A, only for the numbers in B, or for both. We are going to use "office" in this guide, but you can pick any name.
Installation of the SDK
First, you need to obtain the Realtime SDK from npm. From your terminal you can run the following command to install it:
- npm
- Yarn
- pnpm
npm install --save @signalwire/realtime-api@~3
yarn add @signalwire/realtime-api@~3
pnpm add @signalwire/realtime-api@~3
Then, include the package in JavaScript as follows:
import { Voice } from "@signalwire/realtime-api";
Making your first call
To make a call from Node.js you need to instantiate a Voice client, and then call one of its dialing methods.
- Relay V3
- Relay V4
import { Voice } from "@signalwire/realtime-api";
const client = new Voice.Client({
project: "your-project-id",
token: "your-api-token",
contexts: ["office"],
});
try {
const call = await client.dialPhone({
from: "+1xxx", // Must be a number in your SignalWire Space
to: "+1yyy",
timeout: 30,
});
console.log("The call has been answered!", call.id);
} catch (e) {
console.error(e);
}
We used the "office" context when initializing the Voice client. You use the contexts
array in the Client constructor to indicate which group of calls you want to receive. For the moment we are not listening for incoming calls, to the context doesn't have any effect. Still, it will be useful for later, when we are going to listen for incoming calls.
import { SignalWire } from "@signalwire/realtime-api";
const client = await SignalWire({
project: "your-project-id",
token: "your-api-token",
});
const voiceClient = client.voice;
try {
const call = await voiceClient.dialPhone({
from: "+1xxx", // Must be a number in your SignalWire Space
to: "+1yyy",
timeout: 30,
});
console.log("The call has been answered!", call.id);
} catch (e) {
console.error(e);
}
You also need to specify a Project ID and API token: find these in the API section of your space, as shown in the following figure. Make sure that your token has the "Voice" scope enabled.
Receiving incoming calls
Once a Client is initialized, you can listen for incoming calls on the selected contexts (in our example, just "office"). For example:
- Relay V3
- Relay V4
client.on("call.received", async (call) => {
console.log("Call received:", call.id, call.from, call.to);
try {
await call.answer();
console.log("Inbound call answered");
} catch (error) {
console.error("Error answering inbound call", error);
}
});
await voiceClient.listen({
topics: ["office"],
onCallReceived: async (call) => {
console.log("Call received:", call.id, call.from, call.to);
try {
await call.answer();
console.log("Inbound call answered");
} catch (error) {
console.error("Error answering inbound call", error);
}
},
});
We used the "office" topic when listening to the voice client's events.
The topics
array is used only listen to the phone numbers that you have
put in that specific topic from the SignalWire dashboard.
Your event handler receives a call object, which you can use to answer the call, to access fields such as call.from
and call.to
, or to call additional methods (playing audio, prompting for input, transferring the call, etc.)
Next steps
Congratulations! You can now make and receive calls with your Node.js application. You are now ready to explore the advanced guides in the Voice section from the left menu.