Make a call with Node.js

You can quickly see how the Voice API works by calling yourself using the API.

In this guide you will learn how to:

Learn how to:
  1. Set up your Node.js application
  2. Call your phone number

What you need to know before you start

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

Set up your Node.js application

Create a new node app with npm.

Copy
Copied
npm init

Accept the defaults for the application.

Add the fetch package with npm to generate the necessary dependencies.

Copy
Copied
npm install 'cross-fetch'

Create your file

Create a new file named index.js in the project and paste the provided "Make a phone call" code into the file.

Note:

This tutorial uses basic authentication for testing purposes. We recommend using a signed request for authentication in a production environment. You can follow the steps in this guide, but use the code samples from here to use request signing authentication instead.

Make a phone call

// Find your application key and secret at dashboard.sinch.com/settings/access-keys
// Find your Sinch numbers at dashboard.sinch.com/numbers/your-numbers/numbers
const APPLICATION_KEY = "";
const APPLICATION_SECRET = "";
const SINCH_NUMBER = "";
const LOCALE = "";
const TO_NUMBER = "";

const basicAuthentication = APPLICATION_KEY + ":" + APPLICATION_SECRET;

const fetch = require('cross-fetch');
const ttsBody = {
  method: 'ttsCallout',
  ttsCallout: {
    cli: SINCH_NUMBER,
    destination: {
      type: 'number',
      endpoint: TO_NUMBER
    },
    locale: LOCALE,
    text: 'This is a call from sinch',
  }
};

fetch("https://calling.api.sinch.com/calling/v1/callouts", {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      Authorization: 'Basic ' + Buffer.from(basicAuthentication).toString('base64')
    },
    body: JSON.stringify(ttsBody)
  }).then(res => res.json()).then(json => console.log(json));

Fill in your parameters

Assign your values to the following parameters:

ParameterYour value
APPLICATION_KEYThe key found on your Sinch dashboard.
APPLICATION_SECRETThe secret found on your Sinch dashboard.
SINCH_NUMBERAny inbound number you've assigned to your application. Find the number on your Sinch dashboard by clicking on your app, navigating to the Voice and Video tab, and looking in the Inbound Numbers section.
LOCALEThe language and locale you want to use for the text-to-speech call. Locale is specified with a language code according to ISO 639, a dash and a country code according to ISO 3166-1 alpha-2. For example, American English is represented by en-US.
TO_NUMBERThe phone number that you want to call.

Save the file.

Call your phone number

Now you can execute the code and make your text-to-speech call. Run the following command:

Copy
Copied
node index.js

You should receive a phone call to the number you called with the message "This is a phone call from Sinch."

Next steps

Now that you know how to make a call, learn how to handle an incoming call.

Additional resources

Was this page helpful?

Make a phone call

// Find your application key and secret at dashboard.sinch.com/settings/access-keys
// Find your Sinch numbers at dashboard.sinch.com/numbers/your-numbers/numbers
const APPLICATION_KEY = "";
const APPLICATION_SECRET = "";
const SINCH_NUMBER = "";
const LOCALE = "";
const TO_NUMBER = "";

const basicAuthentication = APPLICATION_KEY + ":" + APPLICATION_SECRET;

const fetch = require('cross-fetch');
const ttsBody = {
  method: 'ttsCallout',
  ttsCallout: {
    cli: SINCH_NUMBER,
    destination: {
      type: 'number',
      endpoint: TO_NUMBER
    },
    locale: LOCALE,
    text: 'This is a call from sinch',
  }
};

fetch("https://calling.api.sinch.com/calling/v1/callouts", {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      Authorization: 'Basic ' + Buffer.from(basicAuthentication).toString('base64')
    },
    body: JSON.stringify(ttsBody)
  }).then(res => res.json()).then(json => console.log(json));