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:
What you need to know before you start
Before you can get started, you need the following already set up:
- Set all Voice API configuration settings.
- Node.js and a familiarity with how to create a new app.
Set up your Node.js application
Create a new node app with npm.
npm init
Accept the defaults for the application.
Add the fetch package with npm to generate the necessary dependencies.
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:
Parameter | Your value |
---|---|
APPLICATION_KEY | The key found on your Sinch dashboard. |
APPLICATION_SECRET | The secret found on your Sinch dashboard. |
SINCH_NUMBER | Any 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. |
LOCALE | The 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_NUMBER | The 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:
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.