The Sinch Node.js SDK allows you to quickly interact with the Voice API from inside your Node.js applications. 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 Voice API.
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.
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 below is an example of how to use the Node.js SDK to make a Text to speech phone call. We've also provided an example that accomplishes the same task using the REST API.
// eslint-disable-next-line no-unused-vars
import { Voice, VoiceService } from '@sinch/sdk-core';
/** @param {VoiceService} voiceService */
export const execute = async (voiceService) => {
const recipientPhoneNumber = 'the_phone_number_to_call';
const callingNumber = 'the_calling_number';
/** @type {Voice.TtsCalloutRequestData} */
const requestData = {
ttsCalloutRequestBody: {
method: 'ttsCallout',
ttsCallout: {
destination: {
type: 'number',
endpoint: recipientPhoneNumber,
},
cli: callingNumber,
locale: 'en-US/male',
text: 'Hello, this is a call from Sinch.',
},
},
};
const response = await voiceService.callouts.tts(requestData);
console.log(`Callout response: \n${JSON.stringify(response, null, 2)}`);
};
This example highlights the following required to successfully make a Voice API call using the Sinch Node.js SDK:
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.
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"
});
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
});
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"
});
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()]
.
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.'
}
}
});
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() |
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() |
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() |
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() |
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 | Methods |
---|---|---|
Answered Call Event | AceSvamletBuilder() | setAction() or addInstruction() |
Incoming Call Event | IceSvamletBuilder() | setAction() or addInstruction() |
Prompt Input Event | PieSvamletBuilder() | setAction() or addInstruction() |
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 |
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 the get()
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
When using the Voice API, 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.
Response fields match the API responses. They are delivered as Javascript objects.