Skip to content
Last updated

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.

Note

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

Step 1. Set up your Node.js application

To quickly get started setting up a simple client application using the Node SDK:

  1. If you haven't already, clone the sinch-sdk-node repository.

  2. Navigate to the examples/getting-started/voice/make-a-call folder.

  3. Open a command prompt or terminal and run the following command to install the necessary dependencies:

    npm install
  4. Rename the .env.example file to .env and open it. Using the app credentials from your Sinch Build Dashboard, populate the following fields with your values:

FieldDescription
SINCH_APPLICATION_KEYThe unique ID of your application.
SINCH_APPLICATION_SECRETThe secret for your application.
  1. Save the file.

  2. Navigate to examples/getting-started/voice/make-a-call/src/voice/voiceSample.js.

voiceSample.js
// Use this code to make a phone call using the Voice API and the Node SDK. 
/**
 * Class to place a demo Voice call using the Sinch Node.js SDK.
 */
export class VoiceSample {
  /**
   * @param { import('@sinch/sdk-core').VoiceService } voiceService - the VoiceService instance from the Sinch SDK containing the API methods.
   */
  constructor(voiceService) {
    this.voiceService = voiceService;
  }

  async start() {
    const caller = 'CALLER_NUMBER';
    const recipient = 'RECIPIENT_PHONE_NUMBER';

    const response = await this.voiceService.callouts.tts({
      ttsCalloutRequestBody: {
        method: 'ttsCallout',
        ttsCallout: {
          destination: {
            type: 'number',
            endpoint: recipient,
          },
          cli: caller,
          text: 'Hello, this is a call from Sinch. Congratulations! You just made your first call.',
        },
      },
    });

    console.log('Call ID:', response.callId);
  }
}

Modify your application

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

Set your Destination number parameter

In this example you want to call a phone number. Change the value of the recipientPhoneNumber 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.

Step 2. Call your phone number

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

node src/app.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