Parsing an Incoming Twilio SMS Webhook with Node.js
Time to read: 3 minutes
When responding to an incoming SMS message, you have access to a variety of information about the message through the body of the webhook request that Twilio sends you.
Here is all the code you need to grab data from an incoming SMS webhook request from the Twilio API and respond with another text message:
Simply responding to a text message may only involve returning some TwiML, but sometimes you might want to programmatically make decisions based on things like the body of the message or the country the message was sent from. As in the example above, this requires you to access information contained in the HTTP request sent from Twilio.
Let's walk through how to access this information in more detail with Node.js and the Express framework.
Development Environment Setup
Let's start by making sure we have the right software installed and set up that we'll need to use for the rest of this post. Throughout this post, you will need:
- Node.js and npm installed (do this first if you haven't already)
- A free Twilio account and a Twilio phone number
- The npm modules for Express, the body-parser middleware, and the Twilio Node library
Here is a good guide to follow in general if you are going to be doing more with Twilio and Node.js and have any more questions.
To install these npm modules, navigate to the directory where you want this code to live and run the following command in your terminal to create an npm package for this project.
The --yes
argument just runs through all of the prompts that you would otherwise have to fill out or skip. Now that we have a package.json
for our app, let’s install the necessary libraries with the following shell commands:
After this you should be good to write some code!
Setting up an Express server to receive the incoming message
Open a file called index.js
in the same directory as your package.json
and add the following code to it:
This code sets up an Express app, defines a route on the app to handle incoming SMS messages, and then has it listen on port 3000 for incoming requests. In the /sms
route, the body-parser middleware is used to access data in the request body in order to grab the text of the message and the phone number it was sent from.
You can run this code with the following command:
Setting up your Twilio account
Before being able to respond to messages, you’ll need a Twilio phone number. You can buy a phone number here (it’s free if you’re using the number to test your code during development).
Your Express app will need to be visible from the internet in order for Twilio to send requests to it. We will use ngrok for this, which you’ll need to install if you don’t have it. In your terminal run the following command:
If you’ve just installed ngrok and that previous command didn’t work, you might have to run it like ./ngrok http 3000
from the directory that the ngrok executable is in.
This provides us with a publicly accessible URL to the Express app. Configure your phone number as seen in this image by adding your ngrok URL with "/sms" appended to it to the “Messaging” section:
You are now ready to receive a text message to your new Twilio number.
What else can I do?
So you're grabbing the text of the message and the number it was sent from, but there is more information in the body of that request that you can use. If you want to find out which country the message was sent from you can do that with req.body.FromCountry
. You can also grab the status of the message with req.body.SmsStatus
.
If you want to learn more thoroughly about Express and Twilio SMS check out the SMS and MMS Notifications tutorial or Appointment Reminders with Express.
Feel free to reach out if you have any questions or comments or just want to show off the cool stuff you’ve built.
- Email: sagnew@twilio.com
- Twitter: @Sagnewshreds
- Github: Sagnew
- Twitch (streaming live code): Sagnewshreds
Related Posts
Related Resources
Twilio Docs
From APIs to SDKs to sample apps
API reference documentation, SDKs, helper libraries, quickstarts, and tutorials for your language and platform.
Resource Center
The latest ebooks, industry reports, and webinars
Learn from customer engagement experts to improve your own communication.
Ahoy
Twilio's developer community hub
Best practices, code samples, and inspiration to build communications and digital engagement experiences.