Skip to content

Sending an SMS or Voice Message With Node.js

August 23, 2017

Telesign Team
telesign node

I’m pleased to announce the initial release of a Node.js SDK wrapper for Telesign’s APIs. In the last few years, the growth of JavaScript has surpassed everyone’s expectations. Many new products and tools on the bleeding edge of innovation have been created using JavaScript. As such, we’re excited to see how developers end up using the Node.js SDK in their own products.In this release, developers will be able to access the Messaging, Voice, PhoneID, App Verify and Score APIs. In addition to the Node.js SDK, Telesign already provides C#, Java, Python, and Ruby SDKs.

Sending an SMS Message Using Node.js

To demonstrate how to use the Node.js SDK, let’s run the example code snippet that sends an SMS message. Make sure you have Node.js installed before proceeding. (A video tutorial of this demonstration is available at the end of this post.)In order to get started with the API, you will first need to obtain your Customer ID and API Key (see 1 in the figure below). If you already have a Telesign account, you can retrieve them from your account dashboard within the portal. If you have not signed up yet, sign up here. If you are using a trial account, you will need to take the additional step of pre-verifying the telephone number you wish to send an SMS to. In order to do this, in the portal, select the Accounts tab and then select the Test Numbers option (see 2 in the figure below).

TelePortal screenshot

Figure: You can obtain your Customer ID and API Keys from the portal. You will additionally also need to add test number(s) if you have a trial account. Once you have signed up, the easiest way to see the code running is to clone the github repo and then run one of the existing examples.In order to clone the repo and go to the examples directory, let’s type in the following commands into the terminal or command prompt window:

git clone https://github.com/Telesign/node_telesign

cd node_telesign/examples/messagingNext, let's edit the 1_send_message.js file to update it for the Customer ID, the API Key you got from the Telesign portal. Additionally, don't forget to change the telephone number to your number.const TelesignSDK = require('../../src/Telesign');

const customerId = "customer_id"; // Todo: find in portal.www.telesign.com

const apiKey = "dGVzdCBhcGkga2V5IGZvciBzZGsgZXhhbXBsZXM="; // Todo: find in portal.www.telesign.com

const rest_endpoint = "https://rest-api.www.telesign.com";

const timeout = 10*1000; // 10 secs

const client = new TelesignSDK( customerId,

   apiKey,

   rest_endpoint,

   timeout

);

const phoneNumber = "phone_number";

const message = "You're scheduled for a dentist appointment at 2:30PM.";

const messageType = "ARN";

console.log("## MessagingClient.message ##");

function messageCallback(error, responseBody) {

   if (error === null) {

       console.log(`Messaging response for messaging phone number: ${phoneNumber}` +

           ` => code: ${responseBody['status']['code']}` +

           `, description: ${responseBody['status']['description']}`);

   } else {

       console.error("Unable to send message. " + error);

   }

}

client.sms.message(messageCallback, phoneNumber, message, messageType);

Let’s quickly discuss the contents of this code. Line 2 will cause the API to be loaded. Lines 5 and 6 contain your credentials. Some of our enterprise customers are given an alternate endpoint, if Telesign has given you one an alternate endpoint, you should change it in line 7. Line 10 instantiates the TelesignSDK class with the credentials, endpoints, and other optional parameters. Finally, line 33 calls the SDK to send an SMS.

In order to run the code, from within the examples directory, run the following command:

node 1_send_message.js

Bing! Congrats! You should have received an SMS that says “You’re scheduled for a dentist appointment at 2:30PM”. For further instructions on how to use the SDK, check out the README.md file on the github repository which contains the code for the Node.js SDK.

You gotta love how simple the SDK makes things. You can check out the npm repo here. Don’t forget to try the other examples that demonstrate Voice, Score, and PhoneID APIs. Additionally, share your feedback in comments section below!

Video Tutorial