Building with Twilio

How to Receive and Respond to Text Messages with Node.js, Express

Vincent Collis
2 min readMay 13, 2021

For starters what is Twilio? How does it work? And why do you need it?

Twilio is an American cloud communications platform as a service company based in San Francisco, California. Twilio allows software developers to programmatically make and receive phone calls, send and receive text messages, and perform other communication functions using its web service APIs.

Effective communication is essential to the success of any business. This is where Twilio saves the day. Twilio is the best API to build tools to help businesses stay in touch with their customers and keep abreast of how to meet their expectations. The smartest way for businesses to interact with their customers is through sites and applications, then the smartest thing to do is to resort to a CPaaS solution such as Twilio.

Twilio solutions are easy to integrate into the project using their API. Twilio targets communications platforms that need applications to send phone calls, send text messages, all around the world at a super low cost.

Now instead of theory crafting with you on how Twilio works, I'm going to walk you through step by step how to build a basic text messages app with Twilio's API in Node.js & Express!

Set up your Twilio account

First things first, go to https://www.twilio.com/ and set up your account. In order to send & respond to messages, you will need to purchase a Twilio phone number. No worries it's only $1 per month for the number. Twilio also allows you to use a number for free if it is just for testing purposes.

Our application will also need to be exposed to the internet in order to send and receive Twilio request. In order to do this, we will use ngrok. Once it is installed run this command:

ngrok http 3000

This will give you a publicly accessible URL to the app. Configure your phone number on Twilio web UI with your ngrok forwarding URL with /sms appended.

It should look something like this:

HTTP://78f9dsa6f8.ngrok.io/sms

Now your app is able to send and receive text messages from your Twilio number.

Install your dependencies

Make sure you have Node.js & npm installed.

Open your project directory and install the necessary libraries

npm install twilio@3.8.1 --save
npm install express@4.16.2 --save

Build our App

Then create a new file and name it index.js. Add the following code:

const express = require('express');
const MessagingResponse = require('twilio').twiml.MessagingResponse;

const app = express();

app.get('/', (req, res) => {
res.send('Hello World!');
});

app.post('/sms', (req, res) => {

// Start our TwiML response.
const twiml = new MessagingResponse();

// Add a text message.
const msg = twiml.message('Check out this dope owl!');

// Add a picture message.
msg.media('https://demo.twilio.com/owl.png');

res.writeHead(200, {'Content-Type': 'text/xml'});
res.end(twiml.toString());
});

app.listen(3000, () => {
console.log('Example app listening on port 3000!');
});

Now for the magic! Run your code with:

node index.js

Send a text message to your Twilio number and... BOOM you should get a response!

--

--