How to Send SMS with Node.js

2021-07-07 09:38:24
891
0
Blog picture

Voximplant Platform allows developers with any expertise level to send SMS messages in minutes. In this article, we’ll guide you through this development process. You can also watch a video version of this tutorial.

1. Set Up a Free Account

First, you need to create an account on Voximplant Platform. This process takes a minute and requires only email verification.

2. Purchase a Phone Number

Once you’re in the Voximplant system, you need to purchase a phone number to send text messages. Go to the “Numbers'' section to do so.

You can purchase phone numbers in 60 countries. Phone number prices differ depending on the country. For instance, a US phone number costs $1. Your promotional credit is enough to buy it so you don’t need to replenish your balance. You can check all the rates on the pricing page.

Some phone numbers may require verification with proof of address. Others may not support SMS. Always check the info button tips listed under the number selection to ensure your SMS will be sent immediately.

3. Enable Sending SMS

SMS is not enabled by default. You need to turn it on from the “My phone numbers” section. Сlick on the icon with three dots on the right to edit your phone number. Once SMS is enabled, you will see a checkbox popping up on the screen. That’s all you need to do to get your number set up.

4. Test Your Phone Number

This is completely optional and you can skip this step. If you want to ensure that your phone number works, you can test it manually using the “Two-way SMS” section. Once sent, you will get a notification that the SMS came through. You can also see it in the Two-way SMS history.

5. Load the API Client

Now let’s learn how to send SMS messages programmatically. Voximplant’s SMS service is controlled via the Management API. 

Go to the documentation and click on your preferred API client. In this guide, we’ll use Node.js but you can choose from Curl, Python, PHP, Go, .NET, Java, and C#. The steps are similar for all these languages.

6. Create a Service Account

The NPM package installer will say you need an API key so let’s go make one. Go back to the Voximplant control panel and move to Service accounts under the Settings. Click on the “Add” button and choose the developer role. After that, generate and download your key. Make sure you save it in a secure location – for instance, in your hard drive. If you lose the key, you will need to generate a new one.

7. Develop the SMS App  

Now when you have the API key, you can create your app. The next steps are as follows:

  1. Open your terminal
  2. Make a directory for your project. You can use NPM as instructed to install the Node.js API client.
  3. Link to your credentials. Optionally, you can do this inside your app but it’s usually the best practice to use environment variables. As an example, you can set the Voximplant underscore credentials variable to point to the key file you downloaded.
    const VoximplantApiClient = require('@voximplant/apiclient-nodejs').default;
    const client = new VoximplantApiClient();
    client.onReady = function () {
      // Send the SMS message with text "Test message" from the phone number 447443332211 to the phone number 447443332212.
      client.SMS.sendSmsMessage({
        source: '447443332211',
        destination: '447443332212',
        smsBody: 'Test message',
      })
        .then((ev) => console.log(ev))
        .catch((err) => console.error(err));
    };​
  4. Copy the “Send SMS” code sample. If you go back to the NPM page, you’ll see a ready-to-go SMS example. Just copy and paste it to your editor or IDE.
  5. Specify destination and source numbers. The SMS API is pretty simple. You only need to change the sender number you recently purchased and then the destination number.
  6. Enter the message body. Finally, you need the message body itself. For testing purposes, you can just keep the default here. Now, you can save and run this file with Node.js.
  7. Check if SMS is delivered. If you’re following along, it should’ve been delivered now. That's all you need to do to send SMS with Voximplant APIs.

Congratulations! Now you know how to send SMS with Voximplant. There’s so much more you can do. If you’re interested in continuing to explore, check out the links below. 

 

Sign Up for a free Voximplant developer account or talk to our experts

Add your comment

Name*
Email*
Message

Your comment has been added and will be published after moderation.

Recommended posts

Speech Technologies For Your Business – The Future of CX

Speech Technologies For Your Business – The Future of CX

Striving to offer the best service possible, contact centers are usually very quick to adopt new technologies and try out innovative solutions. Successful implementation of speech technologies can create a great competitive advantage for a company.