Sinch Node.js SDK for Voice API
The Sinch Voice API Node.js SDK allows you to quickly interact with the Voice API from inside your Node.js applications. The fastest way to get started with the SDK is to check out our getting started guides. There you'll find all the instructions necessary to download, install, set up, and start using the SDK.
Syntax
When using the Node.js SDK, the code representing requests and queries sent to and responses received from the Voice API are structured similarly to those that are sent and received using the Verification API itself.
Note:
This guide describes the syntactical structure of the Node.js SDK for the Voice API, including any differences that may exist between the API itself and the SDK. For a full reference on Voice API calls and responses, see the Voice API Reference.
The code sample on the side of this page is an example of how to use the Node.js SDK to make a Text to speech phone call. The code is also displayed below, along with a Voice REST API call that accomplishes the same task, for reference:
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));
const {SinchClient} = require('@sinch/sdk-core');
const sinchClient = new SinchClient({
applicationKey: 'YOUR_application_key',
applicationSecret: 'YOUR_application_secret'
});
async function makeCall(){
const response = await sinchClient.voice.callouts.tts({
ttsCalloutRequestBody: {
method: 'ttsCallout',
ttsCallout: {
cli: 'YOUR_Sinch_number',
destination: {
type: 'number',
endpoint: 'YOUR_phone_number'
},
text: 'This is a test call from Sinch using the Node.js SDK.'
}
}
});
console.log(JSON.stringify(response));
}
makeCall();
This example highlights the following required to successfully make a Voice API call using the Sinch Node.js SDK:
Client
When using the Sinch Node.js SDK, you initialize communication with the Sinch backend by initializing the Node.js SDK's main client class. This client allows you to access the functionality of the Sinch Node.js SDK.
Initialization
To start using the SDK, you need to initialize the main client class with your credentials from your Sinch dashboard.
const {SinchClient} = require('@sinch/sdk-core');
const sinchClient = new SinchClient({
applicationKey: "YOUR_application_key",
applicationSecret: "YOUR_application_secret"
});
Note:
For testing purposes on your local environment it's fine to use hardcoded values, but before deploying to production we strongly recommend using environment variables to store the credentials, as in the following example:
.env
File
APPKEY="YOUR_application_key"
APPSECRET="YOUR_application_secret"
app.js
File
const {SinchClient} = require('@sinch/sdk-core');
const sinchClient = new SinchClient({
applicationKey: process.env.APPKEY,
applicationSecret: process.env.APPSECRET
});
Note:
If you are using the Node.js SDK for multiple products that use different sets of authentication credentials, you can include all of the relevant credentials in the same configuration object, as in the following example:
const {SinchClient} = require('@sinch/sdk-core');
const sinchClient = new SinchClient({
projectId: "YOUR_project_id",
keyId: "YOUR_access_key",
keySecret: "YOUR_access_secret",
applicationKey: "YOUR_application_key",
applicationSecret: "YOUR_application_secret"
});
Voice domain
The Sinch Node.js SDK organizes different functionalities in the Sinch product suite into domains. These domains are accessible through the client. For example,sinch.voice.[endpoint_category].[method()]
.Endpoint categories
In the Sinch Node.js SDK, Voice API endpoints are accessible through the client. The naming convention of the endpoint's representation in the SDK matches the API:
voice.callouts
voice.calls
voice.conferences
voice.applications
For example:
const response = await sinchClient.voice.callouts.tts({
ttsCalloutRequestBody: {
method: 'ttsCallout',
ttsCallout: {
cli: 'YOUR_Sinch_number',
destination: {
type: 'number',
endpoint: 'YOUR_phone_number'
},
text: 'This is a test call from Sinch using the Node.js SDK.'
}
}
});
voice.callouts
endpoint category
The voice.callouts
category of the Node.js SDK corresponds corresponds to the callouts endpoint. The mapping between the API operations and corresponding Node.js methods are described below:API operation | SDK method |
---|---|
Makes a Text-to-speech callout | tts() |
Makes a Conference callout | conference() |
Makes a Custom callout | custom() |
voice.calls
endpoint category
The voice.calls
category of the Node.js SDK corresponds corresponds to the calls endpoint. The mapping between the API operations and corresponding Node.js methods are described below:API operation | SDK method |
---|---|
Get information about a call | get() |
Manage call with callLeg | manageWithCallLeg() |
Updates a call in progress | update() |
voice.conferences
endpoint category
The voice.conferences
category of the Node.js SDK corresponds corresponds to the conferences endpoint. The mapping between the API operations and corresponding Node.js methods are described below:API operation | SDK method |
---|---|
Get information about a conference | get() |
Manage a conference participant | manageParticipant() |
Remove a participant from a conference | kickParticipant() |
Remove all participants from a conference | kickAll() |
voice.applications
endpoint category
The voice.applications
category of the Node.js SDK corresponds corresponds to the configuration endpoint. The mapping between the API operations and corresponding Node.js methods are described below:API operation | SDK method |
---|---|
Return all the numbers assigned to an application | listNumbers() |
Assign a number or list of numbers to an application | assignNumbers() |
Unassign a number from an application | unassignNumber() |
Return the callback URLs for an application | getCallbackUrls() |
Update the callback URL for an application | updateCallbackUrls() |
Returns information about a number | QueryNumber() |
Call events and SVAML
The Voice API uses call events and SVAML responses to dynamically control calls throughout their life cycle. The Node.js SDK has a number of builder and helper methods designed to aid in quickly constructing the correct SVAML responses for the various call events. The builder and helper methods available and their parameters are described in the example and tables below.
const iceResponse = new Voice.IceSvamletBuilder()
.setAction(Voice.iceActionHelper.hangup())
.addInstruction(Voice.iceInstructionHelper.say('Thank you for calling Sinch! This call will now end.', 'en-US'))
.build();
Call event | Builder method name |
---|---|
Answered Call Event | AceSvamletBuilder() |
Incoming Call Event | IceSvamletBuilder() |
Prompt Input Event | PieSvamletBuilder() |
Using these builder methods you can then build your SVAML responses using the following methods:
Actions:
Helper method name | Action |
---|---|
Voice.aceActionHelper.connectConf | connectConf |
Voice.aceActionHelper.continue | continue |
Voice.aceActionHelper.hangup | hangup |
Voice.aceActionHelper.runMenu | runMenu |
Voice.iceActionHelper.connectConf | connectConf |
Voice.iceActionHelper.connectMxp | connectMxp |
Voice.iceActionHelper.connectPstn | connectPstn |
Voice.iceActionHelper.connectSip | connectSip |
Voice.iceActionHelper.hangup | hangup |
Voice.iceActionHelper.park | park |
Voice.iceActionHelper.runMenu | runMenu |
Voice.pieActionHelper.connectConf | connectConf |
Voice.pieActionHelper.connectSip | connectSip |
Voice.pieActionHelper.continue | continue |
Voice.pieActionHelper.hangup | hangup |
Voice.pieActionHelper.runMenu | runMenu |
Instructions:
Helper method name | Action |
---|---|
Voice.aceActionHelper.playFiles | playFiles |
Voice.aceActionHelper.say | say |
Voice.aceActionHelper.setCookie | setCookie |
Voice.aceActionHelper.startRecording | startRecording |
Voice.iceActionHelper.answer | answer |
Voice.iceActionHelper.playFiles | playFiles |
Voice.iceActionHelper.say | say |
Voice.iceActionHelper.sendDtmf | sendDtmf |
Voice.iceActionHelper.setCookie | setCookie |
Voice.iceActionHelper.startRecording | startRecording |
Voice.pieActionHelper.playFiles | playFiles |
Voice.pieActionHelper.say | say |
Voice.pieActionHelper.sendDtmf | sendDtmf |
Voice.pieActionHelper.setCookie | setCookie |
Voice.pieActionHelper.startRecording | startRecording |
Voice.pieActionHelper.stopRecording | stopRecording |
Request and query parameters
Requests and queries made using the Node.js SDK are similar to those made using the Voice API. Path parameters, request body parameters, and query parameters that are used in the API are all passed as arguments to the corresponding Node.js method.
For example, consider this example in which theget()
method of the voice.calls
class is invoked:const response = await sinchClient.voice.calls.get({
callId: 'YOUR_call_id'
});
url = "https://calling.api.sinch.com/calling/v1/calls/id/" + callId
callId
would be included as a path parameter in the request. With the Node.js SDK, the callId
parameter is used in an object passed as an argument in the get()
method.Responses
Response fields match the API responses. They are delivered as Javascript objects.