Back to Blog
Language How-Tos

How to Receive Incoming SMS with a Webhook in Twilio Using Node.js

Build an Express.js webhook endpoint that receives inbound Twilio SMS, parses the body, and responds with TwiML in Node.js.

DA
Danial A
Senior Twilio Consultant, Telphi Consulting
June 22, 2026
8 min read
Twilio
Node.js
JavaScript
How-To
Code
How to Receive Incoming SMS with a Webhook in Twilio Using Node.js

This tutorial builds an Express.js webhook that Twilio calls every time your number receives an inbound SMS message, parses the sender and body fields, and returns a valid TwiML response. It targets Node.js developers building two-way SMS applications where your server must process and reply to messages users send to your Twilio number. You will run a local Express server exposed through ngrok, configure your Twilio number's webhook, and send a test message to verify the full flow.

Prerequisites and Setup

Install the required packages with npm install twilio express dotenv and download ngrok from its website to create a public tunnel to your local server. Run ngrok http 3000 in a separate terminal to get a public HTTPS URL that Twilio can reach, then paste that URL followed by /sms into your Twilio number's Messaging webhook configuration. The Express middleware express.urlencoded({ extended: false }) is required to parse the form-encoded body that Twilio POSTs to your endpoint. Set the HTTP method to POST in the Twilio Console, which is the default for SMS webhooks.

npm install twilio express dotenv

# Start ngrok in a separate terminal
npx ngrok http 3000
# Paste https://xxxx.ngrok.io/sms into Twilio Console SMS webhook

The Complete Code

Express parses Twilio's POST body so you can read req.body.Body for the message text and req.body.From for the sender's number. The twilio.twiml.MessagingResponse class builds a valid TwiML XML response without string concatenation. Setting res.type('text/xml') before calling res.send() ensures Twilio does not log a content-type warning. The route must return a 200 status with the TwiML body or Twilio will consider the webhook delivery a failure and retry the request.

require('dotenv').config();
const express = require('express');
const twilio = require('twilio');

const app = express();
app.use(express.urlencoded({ extended: false }));

app.post('/sms', (req, res) => {
  const from = req.body.From || '';
  const body = req.body.Body || '';

  console.log('Inbound SMS from', from, ':', body);

  const twiml = new twilio.twiml.MessagingResponse();
  twiml.message('Got your message: "' + body.trim() + '". We will reply shortly.');

  res.type('text/xml');
  res.send(twiml.toString());
});

app.listen(3000, () => console.log('Webhook listening on port 3000'));

Testing and Debugging

Start the server with node app.js, confirm ngrok is running, and send a text from your personal phone to your Twilio number. Watch the terminal for the console log that shows the sender and body to confirm Twilio reached your endpoint. If Twilio reports a webhook delivery error in the Console, verify the ngrok tunnel is still running and that the URL in the Console matches the path /sms exactly. Return a Content-Type: text/xml header or Twilio may log a warning about an unexpected response format even if the message content is valid TwiML.

Taking It Further

Add Twilio request signature validation using twilio.validateExpressRequest() as middleware to verify each POST genuinely came from Twilio and reject spoofed requests. Store every inbound message in a database with the sender, body, and timestamp for auditing, analytics, and conversation threading. Add keyword detection inside the route handler to branch responses based on message content, which is the foundation of an SMS bot. For production deployment, replace ngrok with a stable HTTPS endpoint on a cloud platform and configure the URL permanently in the Twilio Console.

Talk to Telphi Consulting to build a production inbound SMS handler with signature validation, logging, and automated response logic.

Share this article:
0 views

Ready to Transform Your Business Communications?

Get a free consultation with our VoIP experts and discover how we can help you save costs, improve efficiency, and scale your business.

Comments (0)

Join the discussion and share your thoughts (AI-moderated for quality)

Protected by AI moderation

Be the first to comment

No comments yet. Share your thoughts below.