In Context

The next-generation communications blog from Nexmo

< Back

How to Send SMS Messages with Node.js and Express

October 19, 2016 Published by

This is the first article in a series of “Getting Started with Nexmo and Node.js” tutorials.

The Nexmo SMS API allows you to send and receive a high volume of SMS anywhere in the world. Once you get your virtual phone number, you can use the API to manage outbound messages (“sending”) and inbound messages (“receiving”). In this article, you will learn how to send SMS messages with Node.js and express.

All the step-by-step articles I am going to post in this Getting Started series are written from my experiences as a new employee at Nexmo! Whenever I try a new thing, technical or not, I tend to write down how I did it whether I succeed or fail. Working with Nexmo APIs is not an exception – I have been writing down every step I took to work with each API from scratch. Now I am posting my notes with a bunch of screenshots to share with you, so I hope you find them helpful. So let’s walk through with me!

GitHub icon View the source code on GitHub

Prerequisites

Before starting this tutorial, make sure you have:

  • the basic understanding of JavaScript and Node.js
  • Node.js installed on your machine

Getting Your API Keys

To get started with the SMS API, sign up for a Nexmo account to get your virtual number, as well as your API credentials. Once you signed up, go to the Nexmo Dashboard to get your number on Numbers, and API key and secret on Settings section.

nexmo dashboard

Using the Nexmo REST API Client for Node.js

First, use npm to install nexmo, the REST API client for Node.js in your working directory:

$ npm install nexmo --save

Create a .js file, let’s call it index.js, and in the file, initialize a Nexmo instance with your credentials:

There are also optional params such as, applicationId, privateKey, and options object. You can find out more on the node-nexmo library repo on GitHub.

Send SMS Messages with Node.js

To send a message, use the nexmo.sms.sendSms function and pass your virtual number you are sending the message from, a recipient number, and the message to be sent.

Also, you can pass optional params, and a callback.

Let’s try hard-code the phone number (which should start with a country code, e.g. “15105551234”) and a message for now to try the API:

Let’s run this, and see if you get a SMS to your mobile phone.

$ node index.js

SMS sent via Nexmo on Android

I hope it worked! You have learned how to send an SMS message with Nexmo Node.js Library.

You can stop right here, or proceed to play with Express.js to be able to take the queries dynamically from POST requests!

Building a Bare Minimal SMS App with Express.js

Let’s write a very simple app using Express to send an SMS.

Install Express and body-parser as a dependency:

In the index.js, add the following code to start a server and listens on port 3000 for connections:

Now, wrap the nexmo.message.sendSms() with the Express post route method. Let’s set the type to 'unicode' so you can send some emoji too! Also, print out the success response at the callback.

Now, try sending an SMS to any mobile phone number (including Google Voice numbers) using your app.

In this tutorial, we are not going to create an HTML with the form UI where a user can fill out a phone number and message (I will write a full tutorial including a front-end code sometimes!), so let’s pretend we are sending data from a web interface by using Postman to make requests to your app. Postman is a good tool to have when you develop apps with REST APIs!

1. Launch Postman and Select POST, and enter http://localhost:3000/send.

2. At Headers, Set Content-Type: application/json

Send a post request to your app using postman

3. At Body, type a valid JSON with “toNumber” and its value (use your mobile phone number! To receive an SMS message!), also “message” and its value.
Send a post request to your app using postman

4. Press the blue Send button

Once you made a successfully POST to your app, you will get a text message to your phone from the virtual number!

SMS on Android

Also, you will see the response printed on your terminal.

Response from Nexmo SMS API

You can view the code sample used in this tutorial on GitHub.

In the next tutorial, you will learn how to receive SMS messages to your virtual number. Stay tuned!

References


Nexmo Logo

Tags: , , , ,

Categorised in: , , ,

This post was written by