Handle an incoming call with Node.js SDK

Now that you know how to call yourself using the Voice API, learn how to handle incoming calls.

In this guide you will learn:

  1. How to set up your Node.js application
  2. How to start your web server and set up a tunnel
  3. About callbacks
  4. How to call your server
Note:

Before you can get started, you need the following already set up:

  • Set all Voice API configuration settings.
  • NPM and a familiarity with how to install packages.
  • Node.js and a familiarity with how to create a new app.
  • ngrok. You'll use ngrok to open a tunnel to your local server.

Set up your Node.js application

To quickly get started setting up a simple client application using the Node SDK:

  1. If you haven't already, clone the sinch-sdk-node-quickstart repository.
  2. Navigate to the /getting-started/voice/respond-to-incoming-call/server folder.
  3. Open a command prompt or terminal and run the following command to install the necessary dependencies:
    Copy
    Copied
    npm install
  4. Open the .env file. Using the Voice app credentials from your Sinch Customer Dashboard, populate the following fields with your values:
FieldDescription
SINCH_APPLICATION_KEYThe unique ID of your application.
SINCH_APPLICATION_SECRETThe secret for your application.
  1. Save the file.

Start your web server and set up a tunnel

  1. Start the server by executing the following command:
    Copy
    Copied
    npm start
  2. Open a tunnel to the server you just set up. We are using ngrok for this. If you don't have ngrok installed already, install it with the following command:
    Copy
    Copied
    npm install ngrok -g
  3. Open a terminal or command prompt and enter:
    Copy
    Copied
    ngrok http 3001
  4. You should see a screen in your console that shows the running tunnel. Copy the address ending in .ngrok.io.
  5. Navigate to your app on your dashboard. Under the Settings section, you'll see a field labeled "Callback URL." Enter your URL with /VoiceEvent added to the end and click Save. The complete URL should look something like https://3b58-75-118-121-186.ngrok-free.app/VoiceEvent.

Now your server is listening and your callback URL is configured, so you're almost ready to test everything and make a phone call. But before we do, let's take a closer look at callbacks. If you already know about callbacks, skip right to calling your Sinch phone number.

Understanding callbacks

Callbacks (also known as "webhooks") are the method that the Voice API uses to figure out what you want to do with a call. Basically, a callback is a request that the Sinch servers send to your server whenever something happens (otherwise known as an "event") in the call that requires some input from you. There are a few different types of events, but the two we are concerned about for this guide are the Incoming Call Event and the Disconnected Call Event.

An Incoming Call Event happens whenever someone calls one of your Sinch numbers. In essence, someone dials your number and so Sinch servers reach out to you and say "how do you want me to handle this call?"

Most callback events expect a response, depending on the event. The Incoming Call Event expects to receive back a SVAML object in response. You can read more about SVAML here, but just know that SVAML is a markup language Sinch developed to control calls.

The below diagram demonstrates exactly what's happening:

incoming call diagram

  1. Someone dials your Sinch number from a handset.
  2. The Sinch servers create an ICE and send a POST request to your server.
  3. Your server listens for the request and sends back a SVAML response to tell the Sinch server how to handle the call.

In this sample application, this is the SVAML object that you will use to respond to the callback (we've also provided the underlying JSON SVAML object for comparison):

SDKJSON
Copy
Copied
const instruction = 'Thank you for calling your Sinch number. You have just handled an incoming call.';

return new Voice.IceSvamletBuilder()
    .setAction(Voice.iceActionHelper.hangup())
    .addInstruction(Voice.iceInstructionHelper.say(instruction))
    .build();
Copy
Copied
{
  "instructions": [
    {
      "name": "say",
      "text": "Thank you for calling your Sinch number. You have just handled an incoming call.",
      "local": "en-US"
    }
  ],
  "action": {
    "name": "hangup"
  }
}

This SVAML object has two parts: instructions and an action. Instructions are things you want to be done on the call without changing the state of the call. In this case, we want to play a voice that reads out a text message. Actions are things you want to be done to the call to change its state in some way. In this case, we want to hang up and end the call.

And that's it! Now we can test.

Call your Sinch phone number

Look up the free Sinch number assigned to your app and call it using your phone. The call should be picked up by the Sinch servers and you should hear the text from the instruction. Now you know how to handle an incoming call.

Next steps

Learn more about the Voice API:

We'd love to hear from you!
Rate this content:
Still have a question?
 
Ask the community.