How to build a communication microservice to send text messages using Twilio and Express?

Reading Time: 7 minutes
How to build a communication microservice to send text messages using Twilio and Express?

Twilio is all about empowering communication in a convenient and timely manner. In this blog, we will demonstrate how to build a communication microservice to send text messages using Twilio and Express. Let’s get started! 🚀

Required Installations:

  • Node.js: It is a JavaScript runtime environment that executes JavaScript code outside the browsers.
  • Dotenv: This package is for loading the environment variables.
  • Twilio: This package will be used for sending text messages to users.

Hands-on

How to build a communication microservice to send text messages using Twilio and Express?

In this hands-on, we will see how we can build a communication microservice to send text messages using Twilio and Express to the end users’ mobile phones. We will begin with creating a new directory. Proceeding further, we will create a new Twilio account navigating to its dashboard. After completing the initial setup, we will then request a new Twilio phone number using which we will send the message. Noting down the credentials, we will move on to the newly created directory and initialize the project. Then we will install the required packages into the directory. Verifying the successful installations, we will create a .env file to store the credentials fetched from the Twilio account.

Finally, adding up the code in the file, we will start the server and check into the receiver’s phone if the end-user has received the text message with the assigned body in it. On success, we will demonstrate how we can proceed towards deleting the Twilio account to avoid unnecessary costs if you are following the same for a demo purpose.

Note: Please ensure that Node.js is properly installed onto the local machine or else it might display multiple errors while following the process.

Create a new directory on the local machine.

Open the newly created directory in the code editor.

Build a communication microservice to send text messages using Twilio and Express

Now, we need to create a Twilio account. To do so, click on the link below.

https://www.twilio.com/try-twilio

Enter the credentials and sign up for the trial account.

Build a communication microservice to send text messages using Twilio and Express

On success and post verification of email, you will then be navigated to the console dashboard as shown in the image below. Enter the product as Not sure yet and click on next.

Build a communication microservice to send text messages using Twilio and Express

You will be navigated to the page as shown below. Click on Get a Twilio phone number.

Build a communication microservice to send text messages using Twilio and Express

You will then be assigned a phone number.

Build a communication microservice to send text messages using Twilio and Express

If you scroll down on the same page, you’ll get to see the credentials that we will be using in our code.

Now, in the newly created dashboard, open git bash here.

Build a communication microservice to send text messages using Twilio and Express
Build a communication microservice to send text messages using Twilio and Express

Run the command:

npm init

To initialize the repository.

Build a communication microservice to send text messages using Twilio and Express

Keep pressing enter and you’ll see the JSON structure created.

Enter yes to confirm the structure.

Now open the repository and you’ll see a package.json file created in the directory.

Open the package.json file and you’ll see the JSON structure.

Run the command:

npm i --save dotenv

On successful installation, you will see the nodemodules directory and the package-lock.json file.

Open the package.json file and verify the installation.

Run the command:

npm i --save twilio

On success, you’ll see the screen as shown in the image below.

Build a communication microservice to send text messages using Twilio and Express

On success, you will see the package installation in the package.json file.

Now, install nodemon server so that the server auto restarts on each code change.

npm i --save-dev nodemon

On success, you will see the nodemon package installation under the devDependencies key.

Make the following changes in the script key so that we can start the server accordingly.

Build a communication microservice to send text messages using Twilio and Express

In the file structure, right-click and click on create the file.

Create a .env named file.

In the .env file, add the following details. For all the keys, you can add the values as the ones displayed on the Twilio account project that we created above.

Create and open the index.js file.

Build a communication microservice to send text messages using Twilio and Express

Import the Twilio and dotenv packages in the index.js file.

Initialize the config for dotenv.

Create a new function sendMessage with the Twilio parameters imported from the .env file. Then we need to initialize the Twilio object with the account SSID and the auth token as the parameters. For the reply variable, we’ll be initializing the body, the sender’s phone number, and the receiver’s phone number.

Build a communication microservice to send text messages using Twilio and Express

For adding the sender’s phone number, the phone number has to be verified on the Twilio account. Navigate to the below link:

https://console.twilio.com/us1/develop/phone-numbers/manage/verified

You can add more than 1 phone number to the list.

In case you wish to send the message to multiple phone numbers, all you have to do is initialize an array of phone numbers as shown in the image below.

Finally, call the sendMessage function passing the message as the parameter.

Save the changes and start the server using the below command.

Npm run dev

If the connection was successful and the message was sent successfully, you will see the following in the console.

Build a communication microservice to send text messages using Twilio and Express

Now, check on your mobile phone for the number you entered. You’ll see the following message on it.

IMG_20221107_125208

Since it was a test demo, to avoid unnecessary charges, you will have to delete what you created. Click on General settings.

Scroll down to the bottom and click on close.

Build a communication microservice to send text messages using Twilio and Express

Select all and click on Close account.

On success, you will see the following.

Conclusion

In this hands-on, we saw how we can build a communication microservice to send text messages using Twilio and Express to the end users’ mobile phones. We began with creating a new directory. Proceeding further, we created a new Twilio account navigating to its dashboard. Completing the initial setup, we then requested a new Twilio phone number using which we sent the message. Noting down the credentials, we moved on to the newly created directory and initialized the project.

Then we installed the required packages into the directory. Verifying the successful installations, we created a .env file to store the credentials fetched from the Twilio account.

Finally, adding up the code in the file, we started the server and checked into the receiver’s phone if the end user received the text message with the assigned body in it.

On success, we demonstrated how we can proceed towards deleting the Twilio account to avoid unnecessary costs if you are following the same for a demo purpose. We will come up with more such use cases in our upcoming blogs.

Meanwhile…

If you are an aspiring Express Lover and want to explore more about the above topics, here are a few of our blogs for your reference:

Stay tuned to get all the updates about our upcoming blogs on the cloud and the latest technologies.

Keep Exploring -> Keep Learning -> Keep Mastering 

At Workfall, we strive to provide the best tech and pay opportunities to kickass coders around the world. If you’re looking to work with global clients, build cutting-edge products and make big bucks doing so, give it a shot at workfall.com/partner today!

Back To Top