Make a call with Node.js SDK

You can quickly see how the Voice API works by calling yourself using the API and the Node.js SDK.

In this guide you will learn how to:

  1. Set up your Node.js application
  2. Call your phone number
Note:

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

Set up your Node.js application

First we'll create a Node project using npm.

To create the project, do the following steps:

  1. Create a folder called voice-send
  2. Navigate into the folder you created and run the following command.
    Copy
    Copied
    npm init
    This command adds the package.json file. You will be prompted to provide values for the fields. For this tutorial, you can simply accept the default values and press enter at each stage.

You can install the Sinch Node.js SDK using either NPM or Yarn:

NPMYarn
Copy
Copied
npm install @sinch/sdk-core
Copy
Copied
yarn add @sinch/sdk-core

Create your file

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

Modify your application

The code provided includes placeholder parameters. You'll need to update the parameters detailed in the following subsections with your values.

Initialize the client

Before initializing a client using this SDK, you'll need your application key and application secret for your Verification app, found on your dashboard.

To start using the SDK, you need to initialize the main client class with your credentials from your Sinch dashboard.

Copy
Copied
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

Copy
Copied
APPKEY="YOUR_application_key"
APPSECRET="YOUR_application_secret" 

app.js File

Copy
Copied
const {SinchClient} = require('@sinch/sdk-core');

const sinchClient = new SinchClient({
    applicationKey: process.env.APPKEY,
    applicationSecret: process.env.APPSECRET
});

Destination number

In this example you want to call a phone number. Change the value of the YOUR_phone_number parameter to the phone number you verified in your dashboard in E.164 format.
Note:

When your account is in trial mode, you can only call your verified numbers. If you want to call any number, you need to upgrade your account!

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

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