Sending SMS with Twilio in a Node.js Application
Tags: node, twilio, sms, javascript
Published: 2020-03-31 22:59:28

With the increase in the number of mobile devices around the globe today, and numerous mobile applications available to us, SMS is becoming the de facto standard for verification.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/9yil1xwwfpdn2mn8p2nq.png)

Source [Statista](https://www.statista.com/statistics/330695/number-of-smartphone-users-worldwide/)

SMS comes in handy for verification purposes at various stages in our applications, this is especially a great option on mobile applications.
It should be noted that SMS is not only useful for verification purposes but can be essential across various business use cases such as operational/transactional alerts, order placements, etc. TextMagic has a comprehensive article on a number of SMS use cases  for businesses [here](https://www.textmagic.com/blog/use-cases-of-texting-for-small-businesses/)

Today, we are going to explore how to use Twilio for sending SMS in a Node.js application.

**Prerequisites**
To follow through this tutorial you must have the following:
```
Node JS
NPM
Postman
Code Editor (VsCode)
Twilio account
```

If you don’t have Node.js installed just head on to the official [Node.js](https://nodejs.org/en/) website to get a copy of Node.js for your platform. Once you install node.js you will automatically have npm installed.

Head over to [Twilio](https://www.twilio.com/try-twilio) now to get an account set up in less than 2 mins free of charge.

**Getting started**
To get started, create a new directory for the application by running the following command on the terminal:
```
cd desktop && mkdir send-sms-with-twilio && cd send-sms-with-twilio
```

The commands above
**cd desktop** - navigate to the desktop directory
**mkdir send-sms-with-twilio** - create a new directory named “send-sms-with-twilio”
**cd send-sms-with-twilio** - navigate into the newly created send-sms-with-twilio directory
Initialize the directory to create a package.json file by running
```npm init -y```

**Install dependencies**
For this project we will be using Express Framework for Node.js, twilio module, and dotenv to manage our secret twilio keys so let’s install them by running the following:
```
npm install express twilio dotenv body-parser
```
Open the newly created folder in your editor of choice.
Your folder structure should now look like the one below:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/wx7hpxfg512ppcq9zv9l.png)

Create a simple Express server as shown below in an **index.js** file:

{% gist https://gist.github.com/iMichaelOwolabi/7735ebdc8732243caa78a753e0c682a9 %} 

To demonstrate a simple use case of SMS in our application, we are going to create a simple user registration system and use Twilio to send an activation code to the user’s phone number.

N:B

**The purpose of this tutorial is to show how to send SMS using Twilio in our Node.js application hence all the best practices of user registration are not considered.**

In the index.js file, paste the following code:

{% gist https://gist.github.com/iMichaelOwolabi/040e32c9508f8b8f9507ec82dfca5b40 %}

Now that we can create a user successfully, let’s now integrate Twilio to help us send verification SMS to new users as soon as their account is created. Head on to Twilio site and create your account. After you successfully created an account your dashboard should look similar to the one below:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/x7naztbm4ey5v4ppwt3w.png)

**Get a Free Twilio trial number**
From your Twilio dashboard, click **Get a Trial Number** to get a free number which we’ll use as the sender when sending out the SMS to users. You will be able to customize your SENDER ID once you upgrade your account by subscribing to the Twilio platform but our trial number will suffice for the purpose of this tutorial.

**Integrating Twilio in our App**
Create a new file in the project directory and name it **twilio.js**. In the new file copy and paste the following code

{% gist https://gist.github.com/iMichaelOwolabi/f53a941ab6c423ea84e6609331a040ba %}

Create a .env file at the root of your project and add your twilio auth key, account sid and phone number just as below:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/smfp9xvenm6k66acdktu.png)

**NOTE:**
Depending on the format you’re getting the phone number from your user, you may need to format it to meet the international format for sending and receiving SMS. Read more on that topic [here](https://www.cm.com/blog/how-to-format-international-telephone-numbers/)

Now, in the **index.js** file, add the following code

{% gist https://gist.github.com/iMichaelOwolabi/3e69629b5574a13b401e8c193b99b854 %}

Now we are ready to send SMS in our node.js application so let us test our work

**Testing our application**

Just before we send our first SMS, we need to add a verified phone number to our Twilio account. This is so because we are on the trial version. So, from your Twilio dashboard, click on verify phone number and verify a phone.

You should have at least one verified phone number already if you set up your account properly.
Save your work and start the server by running **node index.js** on the terminal

Open up Postman and send a POST request to the /users endpoint with the right parameters

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/g7w6meacaqw21m5u7jrf.png)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/x0jy0unx0igupncc1jtj.png)

Congratulations! You’ve just successfully sent an SMS from a Node.js application.

Twilio has a lot more to offer than just sending SMS to get more of their offerings, kindly visit their official documentation [here](https://www.twilio.com/docs/all)

Complete code can be found here: [https://github.com/iMichaelOwolabi/send-sms-with-twilio](https://github.com/iMichaelOwolabi/send-sms-with-twilio)