Developer Quick-Start: Build a production-ready Node.js Express webhook to receive and reply to inbound SMS messages with Plivo's API, featuring two-way conversations and automated responses. Want to skip 10DLC vetting queues, carrier surcharges, and multi-step SDK boilerplate? You can send the same SMS directly via SMSRoute's REST API with instant no-KYC provisioning.
This comprehensive guide shows you how to build a production-ready Node.js application using Express to receive and reply to inbound SMS messages with Plivo's API. You'll learn how to handle incoming text messages, implement two-way SMS conversations, and create interactive messaging workflows that power customer support chatbots, automated reply systems, and real-time SMS interactions.
By the end of this tutorial, you'll have a secure Express webhook endpoint that receives SMS messages sent to your Plivo phone number, processes incoming message data, and automatically sends customized replies back to users—the essential foundation for building conversational SMS applications with Node.js and Plivo.
What You'll Build: Two-Way SMS System Overview
What We're Building: A Node.js Express server that acts as a webhook endpoint for Plivo. When an SMS message is sent to a designated Plivo phone number, Plivo will forward the message details to our Express application. The application will then process this information and instruct Plivo (via an XML response) to send a reply SMS back to the original sender.
Problem Solved: This enables applications to programmatically engage in two-way SMS conversations, moving beyond simple one-way outbound notifications. It allows businesses to receive and respond to customer inquiries, confirmations, support requests, or any interaction initiated via SMS.
Technologies Used:
- Node.js: A JavaScript runtime chosen for its event-driven, non-blocking I/O model, making it efficient for handling concurrent webhook requests typical of communication APIs.
- Express.js: A minimal and flexible Node.js web application framework providing robust features for building web and mobile applications, ideal for creating API endpoints and webhooks.
- Plivo: A cloud communications platform providing APIs for SMS, Voice, and more. We'll use their SMS API, Node.js SDK, and phone numbers. If you're new to Plivo, check out our Plivo basic SMS sending tutorial first.
- PlivoXML: Plivo uses XML documents (specifically, Plivo eXtensible Markup Language - PlivoXML) to control communication flows. Our Express app will generate PlivoXML to instruct Plivo on how to reply to messages.
- ngrok (for development): A utility to expose local development servers to the public internet, essential for testing Plivo webhooks which require a publicly accessible URL.
- dotenv: A module to load environment variables from a .env file into process.env, crucial for managing sensitive credentials securely.
For related SMS functionality, explore our guides on Plivo OTP and 2FA implementation and Plivo bulk SMS broadcasting.
System Architecture:
graph LR
A[User's Phone] -- Sends SMS --> B(Plivo Network);
B -- Receives SMS --> C(Plivo Platform);
C -- HTTP POST Request (Message Details) --> D{Your Express App (Webhook Endpoint)};
D -- Processes Request --> D;
D -- Generates PlivoXML Reply --> E(HTTP Response with XML);
E -- Sent Back To --> C;
C -- Interprets XML --> C;
C -- Sends Reply SMS --> B;
B -- Delivers SMS --> A;
style D fill:#f9f,stroke:#333,stroke-width:2px
Prerequisites:
- A Plivo account (Sign up for free).
- Node.js and npm (or yarn) installed on your system. (Download Node.js)
- Basic understanding of JavaScript, Node.js, and REST APIs.
- A text editor or IDE (like VS Code).
- git installed (optional, for version control).
- ngrok installed for local development testing (Download ngrok).
Step 1: Node.js Project Setup for Plivo SMS Webhooks
Let's initialize our Node.js project and install the necessary dependencies.
- Create Project Directory: Open your terminal or command prompt and create a new directory for your project, then navigate into it.
mkdir plivo-sms-webhook
cd plivo-sms-webhook
- Initialize Node.js Project: Initialize the project using npm. The
-yflag accepts the default settings.
npm init -y
This creates a package.json file.
- Install Dependencies: We need
expressfor the web server,plivofor the Plivo Node.js SDK (specifically for generating XML responses easily and validation), anddotenvto manage environment variables. Modern versions of Express include body-parsing capabilities, sobody-parseris often not needed separately.
npm install express plivo dotenv --save
- Set Up Project Structure: Create a basic structure for clarity.
mkdir src
mkdir src/middleware
touch src/server.js
touch src/middleware/validatePlivoWebhook.js
touch .env
touch .gitignore
- src/server.js: This will contain our main application code.
- src/middleware/validatePlivoWebhook.js: Will contain our webhook validation logic.
- .env: This file will store sensitive information like API keys and phone numbers (it should not be committed to version control).
- .gitignore: Specifies intentionally untracked files that Git should ignore.
- Configure
.gitignore: Addnode_modulesand.envto your.gitignorefile to prevent committing them to version control.
# .gitignore
node_modules/
.env
npm-debug.log*
yarn-debug.log*
yarn-error.log*
- Set Up Environment Variables: Open the
.envfile and add placeholders for your Plivo credentials and configuration. We'll fill these in later.
.env
# .env
# Plivo Credentials (Find these in your Plivo Console dashboard)
PLIVO_AUTH_ID=YOUR_PLIVO_AUTH_ID
PLIVO_AUTH_TOKEN=YOUR_PLIVO_AUTH_TOKEN
# Your Plivo Phone Number (Must be SMS-enabled and in E.164 format, e.g., +14155551234)
PLIVO_NUMBER=YOUR_PLIVO_NUMBER_E164
# Port for the local Express server
PORT=3000
# A secret for validating webhook authenticity (Generate a strong random string)
PLIVO_WEBHOOK_SECRET=YOUR_STRONG_RANDOM_SECRET
Important: These YOUR_... values are placeholders. You must replace
them with your actual Plivo credentials, number, and a unique secret you generate.
- Why
.env?: Storing sensitive data like API keys directly in code is a security risk. Environment variables allow you to keep credentials separate from your codebase, making it safer and easier to manage different configurations (development, production). The dotenv package makes using a .env file convenient during development. In production, you'll typically set these environment variables directly on your hosting platform. PLIVO_WEBHOOK_SECRET: This is not your Auth Token. It's a secret you define, which you will also configure in the Plivo console later. Plivo will use this secret to generate a signature for incoming webhooks, allowing your application to verify that the request genuinely came from Plivo.
Step 2: Implementing Inbound SMS Reception with Express
Now, let's write the basic Express server code to listen for incoming POST requests from Plivo.
- Basic Server Setup (
src/server.js): Opensrc/server.jsand add the following code:
require('dotenv').config();
const express = require('express');
const plivo = require('plivo');
const PORT = process.env.PORT || 3000;
const PLIVO_WEBHOOK_SECRET = process.env.PLIVO_WEBHOOK_SECRET;
const app = express();
app.use(express.urlencoded({ extended: true }));
app.post('/webhook/plivo/sms', (request, response) => {
console.log("Received Plivo SMS Webhook Request:");
console.log("Headers:", request.headers);
console.log("Body:", request.body);
const from_number = request.body.From;
const to_number = request.body.To;
const text = request.body.Text;
const message_uuid = request.body.MessageUUID;
console.log(`Message Received - From: ${from_number}, To: ${to_number}, Text: "${text}", UUID: ${message_uuid}`);
console.log("Acknowledging webhook receipt (no reply configured yet).");
response.status(200).send("Webhook received successfully.");
});
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
console.log(`SMS Webhook Endpoint: http://localhost:${PORT}/webhook/plivo/sms`);
if (!PLIVO_WEBHOOK_SECRET || PLIVO_WEBHOOK_SECRET === 'YOUR_STRONG_RANDOM_SECRET') {
console.warn("⚠️ WARNING: PLIVO_WEBHOOK_SECRET is not set or is using the default value in .env. Please set a strong secret for webhook validation.");
}
});
require('dotenv').config(): Loads variables from .env. Must be called early.express.urlencoded({ extended: true }): This built-in Express middleware parses the incoming request body, which Plivo sends in application/x-www-form-urlencoded format, and makes it available as request.body. The extended: true option allows for rich objects and arrays to be encoded into the URL-encoded format, though Plivo's webhooks typically use simple key-value pairs.app.post('/webhook/plivo/sms', ...): Defines the route that will listen for Plivo's webhook requests. We use /webhook/plivo/sms as a clear, descriptive path.- Logging: We log the incoming headers and body for debugging. This is very helpful during development.
- Data Extraction: We pull the From, To, Text, and MessageUUID from request.body. These are standard parameters Plivo sends.
- Acknowledgement: Currently, we just send a 200 OK status. Plivo expects a timely response to its webhook; otherwise, it might consider the delivery failed.
- Run the Server Locally: Go back to your terminal (in the project root directory) and run:
node src/server.js
You should see: Server is running on port 3000
SMS Webhook Endpoint: http://localhost:3000/webhook/plivo/sms
(Optional Warning about PLIVO_WEBHOOK_SECRET) Keep this server running.
Step 3: Sending Automated SMS Replies with PlivoXML
Now, let's modify the webhook endpoint to generate a PlivoXML response that tells Plivo to send a reply message.
- Modify
src/server.js: Update the/webhook/plivo/smsroute handler as follows:
console.log(`Message Received - From: ${from_number}, To: ${to_number}, Text: "${text}", UUID: ${message_uuid}`);
try {
const r = new plivo.Response();
const reply_params = {
src: to_number,
dst: from_number
};
const reply_text = `Thank you for your message: "${text}". We received it!`;
r.addMessage(reply_text, reply_params);
const xmlResponse = r.toXML();
console.log("Generated PlivoXML Response:");
console.log(xmlResponse);
response.setHeader('Content-Type', 'application/xml');
response.status(200).send(xmlResponse);
} catch (error) {
console.error("Error generating PlivoXML response:", error);
response.status(500).send("Internal Server Error generating response.");
}
plivo.Response(): Creates an object helper from the Plivo SDK to construct valid PlivoXML.reply_params: Defines the src (source/sender) and dst (destination/recipient) for the reply message. Notice src is your Plivo number (to\_number from the incoming message) and dst is the original sender (from\_number from the incoming message).r.addMessage(reply_text, reply_params): Adds a <Message> XML element to the response. This element tells Plivo to send an SMS with the specified text and parameters.r.toXML(): Serializes the response object into a valid PlivoXML string.response.setHeader('Content-Type', 'application/xml'): Crucial step. You must tell Plivo that the response body is XML.response.status(200).send(xmlResponse): Sends the generated XML back to Plivo with a success status.try...catch: Basic error handling around the XML generation.
- Restart the Server: Stop the running server (Ctrl+C in the terminal) and restart it:
node src/server.js
Step 4: Connecting Your Webhook to Plivo's SMS Platform
Now we need to configure Plivo to send incoming SMS messages to our running application. Since our app is
running locally, we'll use ngrok to create a public URL.
- Start ngrok: Open a new terminal window (keep the Node server running in the first one). Navigate to where you installed ngrok (or ensure it's in your system's PATH). Start ngrok, telling it to forward to the port your Express app is listening on (default is 3000).
ngrok http 3000
ngrok will display output similar to this:
Session Status online
Account Your Name (Plan: Free)
Version x.x.x
Region United States (us)
Web Interface http://127.0.0.1:4040
Forwarding http://xxxxxxxx.ngrok.io -> http://localhost:3000
Forwarding https://xxxxxxxx.ngrok.io -> http://localhost:3000
Connections ttl opn rt1 rt5 p50 p90
0 0 0.00 0.00 0.00 0.00
Important: Note the https forwarding URL (e.g.,
https://xxxxxxxx.ngrok.io). This is your public URL. Use the https
version. You will need this URL soon. Keep ngrok running.
- Get Plivo Credentials and Number:
- Log in to your Plivo Console.
- Auth ID & Auth Token: Find these on the main dashboard page. Copy them and paste them into your .env file for PLIVO\_AUTH\_ID and PLIVO\_AUTH\_TOKEN.
- Plivo Number:
- If you don't have one, navigate to Phone Numbers -> Buy Numbers. Search for a number in your desired country that has SMS capability enabled. Purchase the number.
- If you have a number, go to Phone Numbers -> Your Numbers. Find the number you want to use. Ensure it's SMS-enabled.
- Copy the full number in E.164 format (e.g., +14155551234) and paste it into your .env file for PLIVO\_NUMBER.
- Webhook Secret: Choose a strong, random string (e.g., use a password generator or run openssl rand -hex 16 in your terminal) and paste it into your .env file for PLIVO\_WEBHOOK\_SECRET. Remember this secret.
- Create a Plivo Application: A Plivo Application acts as a container for configuration, including webhook URLs.
- In the Plivo Console, navigate to Messaging -> Applications -> XML.
- Click Add New Application.
- Application Name: Give it a descriptive name (e.g., NodeJS Express SMS Handler).
- Message URL: This is the crucial part. Paste your ngrok
httpsURL followed by your webhook path: [Your Ngrok HTTPS URL]/webhook/plivo/sms. For example: https://xxxxxxxx.ngrok.io/webhook/plivo/sms. - Method: Select POST.
- Default Number App: Set to No.
- Default Endpoint App: Set to No.
- Authentication -> Auth Token: Paste the PLIVO\_WEBHOOK\_SECRET you created in your .env file here. Do not paste your Plivo Auth Token.
- Authentication -> Auth Method: Select Header. (Plivo will send the signature in the X-Plivo-Signature-V3 header).
- Leave other fields (like Hangup URL, Fallback URL) blank for now.
- Click Create Application.
- Link Plivo Number to the Application:
- Navigate back to Phone Numbers -> Your Numbers.
- Click on the Plivo number you want to use for receiving SMS.
- In the number configuration settings:
- Application Type: Select XML Application.
- Plivo Application: Choose the application you just created (e.g., NodeJS Express SMS Handler) from the dropdown list.
- Click Update Number.
Configuration Check:
- Your Node.js server (node src/server.js) is running.
- ngrok http 3000 is running and shows an active session.
- Your .env file has the correct PLIVO\_AUTH\_ID, PLIVO\_AUTH\_TOKEN, PLIVO\_NUMBER, PORT, and PLIVO\_WEBHOOK\_SECRET.
- A Plivo Messaging Application is created with the correct ngrok https URL + /webhook/plivo/sms path set as the Message URL (Method: POST) and your PLIVO\_WEBHOOK\_SECRET configured for header authentication.
- Your Plivo Number is linked to this Plivo Application.
Step 5: Error Handling and Production Logging
Robust applications need proper error handling and logging.
- Enhance Logging: While
console.logis fine for simple development, consider a structured logger likepinoorwinstonfor production. They offer log levels, formatting, and easier integration with log management systems.
Example (Conceptual - using console for simplicity here):
const from_number = request.body.From;
const to_number = request.body.To;
const text = request.body.Text;
const message_uuid = request.body.MessageUUID;
const logPrefix = `[MsgUUID: ${message_uuid || 'N/A'}]`;
console.log(`${logPrefix} Received Plivo SMS Webhook Request`);
console.log(`${logPrefix} From: ${from_number}, To: ${to_number}, Text: "${text}"`);
try {
const r = new plivo.Response();
const reply_params = { src: to_number, dst: from_number };
const reply_text = `Thank you for your message: "${text}". We received it!`;
r.addMessage(reply_text, reply_params);
const xmlResponse = r.toXML();
console.log(`${logPrefix} Generated PlivoXML Response: ${xmlResponse}`);
response.setHeader('Content-Type', 'application/xml');
response.status(200).send(xmlResponse);
console.log(`${logPrefix} Successfully sent XML reply.`);
} catch (error) {
console.error(`${logPrefix} Error generating PlivoXML response:`, error);
response.status(500).send("Internal Server Error generating response.");
}
Adding a unique identifier like the MessageUUID to log lines helps trace a single request's
journey through your system.
- Basic Error Handling Strategy:
- Webhook Validation Errors: Handled in the Security section (return 403 Forbidden).
- PlivoXML Generation Errors: The try...catch block around plivo.Response() handles errors during XML creation. Respond with a generic 500 Internal Server Error to Plivo. Avoid sending detailed error messages back in the HTTP response for security reasons, but log them thoroughly on the server.
- Plivo API Errors (for outbound actions): If your application later makes outbound API calls to Plivo (not just replying via XML), handle potential errors (network issues, invalid credentials, rate limits) using try...catch around the Plivo SDK client calls and implement retries if appropriate.
- Unhandled Exceptions: Implement a global error handler in Express to catch unexpected errors.
app.use((err, req, res, next) => {
console.error("Unhandled Exception:", err);
res.status(500).send('Something broke!');
});
- Retry Mechanisms (for Plivo Webhooks): Plivo automatically retries sending a webhook if
it doesn't receive a timely
2xxresponse from your server (e.g., due to timeouts or 5xx errors).
- Idempotency: Ensure your webhook endpoint can safely handle the same message being delivered multiple times. Use the MessageUUID to check if you've already processed this specific message, especially if your handler performs actions beyond just sending an XML reply (like writing to a database). Store processed MessageUUIDs temporarily (e.g., in Redis or a database) with a short Time-To-Live (TTL).
Step 6: Storing SMS Messages in a Database (Optional)
For many real-world applications, you'll want to store message history or related data. While a full database implementation is beyond the scope of this basic webhook guide, here's a conceptual overview.
Why Store Data?
- Maintain conversation history.
- Track message delivery status (using Plivo's status callbacks - requires another webhook).
- Store user profiles or state related to SMS interactions.
- Perform analysis on message volume, response times, etc.
Technology Choice: PostgreSQL with Prisma ORM is a popular and robust choice for Node.js applications. Other options include MongoDB, MySQL, etc.
Conceptual Schema (using Prisma syntax):
Prisma
// schema.prisma (Conceptual example)
datasource db {
provider = "postgresql" // Or "mysql", "mongodb", etc.
url = env("DATABASE_URL") // Get from environment variables
}
generator client {
provider = "prisma-client-js"
}
model SmsMessage {
id String @id @default(cuid()) // Unique DB ID
plivoUuid String @unique // Plivo's MessageUUID - index this!
direction String // 'inbound' or 'outbound'
senderNumber String
receiverNumber String
messageText String? // Optional for status updates etc.
status String? // e.g., 'received', 'sent', 'delivered', 'failed' (align with Plivo statuses)
timestamp DateTime @default(now()) // Timestamp of event in *our* system
plivoTimestamp DateTime? // Timestamp from Plivo if available/needed
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@index([plivoUuid])
@@index([senderNumber])
@@index([receiverNumber])
@@index([timestamp])
@@index([status])
}
Implementation Steps (If adding DB):
- Install Prisma:
npm install prisma @prisma/client --save-dev - Initialize Prisma:
npx prisma init --datasource-provider postgresql - Define Schema: Update
prisma/schema.prisma. - Set
DATABASE_URL: Add your database connection string to.env. - Run Migrations:
npx prisma migrate dev --name initto create the tables. - Generate Client:
npx prisma generate - Use Prisma Client: Import
PrismaClientand use it in your webhook handler to save messages:
const { PrismaClient } = require('@prisma/client');
const prisma = new PrismaClient();
const message_uuid = request.body.MessageUUID;
const logPrefix = `[MsgUUID: ${message_uuid || 'N/A'}]`;
if (message_uuid) {
try {
const existingMessage = await prisma.smsMessage.findUnique({
where: { plivoUuid: message_uuid },
});
if (existingMessage) {
console.log(`${logPrefix} Message already processed (UUID: ${message_uuid}). Skipping DB write, sending reply.`);
return;
}
} catch (dbError) {
console.error(`${logPrefix} Database error checking for existing message:`, dbError);
}
} else {
console.warn(`${logPrefix} No MessageUUID received. Cannot perform idempotency check.`);
}
try {
await prisma.smsMessage.create({
data: {
plivoUuid: message_uuid || `no-uuid-${Date.now()}`,
direction: 'inbound',
senderNumber: from_number,
receiverNumber: to_number,
messageText: text,
status: 'received',
}
});
console.log(`${logPrefix} Inbound message saved to database.`);
} catch (dbError) {
console.error(`${logPrefix} Database error saving inbound message:`, dbError);
}
Step 7: Securing Your SMS Webhook with Signature Validation
Securing your webhook endpoint is critical.
- Webhook Signature Validation (Essential): Plivo signs incoming webhook requests using the
secret you provided (
PLIVO_WEBHOOK_SECRET) when setting up the Plivo Application. Your application must validate this signature to ensure the request is authentic and hasn't been tampered with.
- Create Validation Middleware (
src/middleware/validatePlivoWebhook.js):
const plivo = require('plivo');
const validatePlivoWebhook = (req, res, next) => {
const plivoSignature = req.header('x-plivo-signature-v3') || '';
const nonce = req.header('x-plivo-signature-v3-nonce') || '';
const webhookSecret = process.env.PLIVO_WEBHOOK_SECRET;
const protocol = req.protocol;
const host = req.get('host');
const path = req.originalUrl;
const fullUrl = `${protocol}://${host}${path}`;
console.log(`Validating webhook: URL='${fullUrl}', Nonce='${nonce}', Signature='${plivoSignature}'`);
if (!plivoSignature || !nonce || !webhookSecret) {
console.warn("Webhook validation failed: Missing signature, nonce, or secret.");
return res.status(403).send('Forbidden: Missing validation headers or secret.');
}
try {
const isValid = plivo.validateV3Signature(
fullUrl,
nonce,
plivoSignature,
webhookSecret
);
if (isValid) {
console.log("Webhook signature is valid.");
next();
} else {
console.warn("Webhook validation failed: Invalid signature.");
res.status(403).send('Forbidden: Invalid signature.');
}
} catch (error) {
console.error("Error during webhook validation:", error);
res.status(500).send('Internal Server Error during validation.');
}
};
module.exports = validatePlivoWebhook;
Important Note on URL Construction: Pay close attention to the URL construction
(fullUrl). If your application runs behind a reverse proxy or load balancer,
req.protocol, req.get('host'), and req.originalUrl might not reflect
the original URL Plivo used. You may need to configure your proxy to forward the correct headers (like
X-Forwarded-Proto, X-Forwarded-Host) and adjust the middleware to use them.
Incorrect URL calculation is a common cause of validation failures in production.
- Apply Middleware in
server.js:
const validatePlivoWebhook = require('./middleware/validatePlivoWebhook');
app.use(express.urlencoded({ extended: true }));
app.post('/webhook/plivo/sms', validatePlivoWebhook, (request, response) => {
const from_number = request.body.From;
const to_number = request.body.To;
const text = request.body.Text;
const message_uuid = request.body.MessageUUID;
const logPrefix = `[MsgUUID: ${message_uuid || 'N/A'}]`;
console.log(`${logPrefix} Webhook validated. Processing message...`);
console.log(`${logPrefix} From: ${from_number}, To: ${to_number}, Text: "${text}"`);
try {
const r = new plivo.Response();
const reply_params = { src: to_number, dst: from_number };
const reply_text = `Thank you for your message: "${text}". We received it!`;
r.addMessage(reply_text, reply_params);
const xmlResponse = r.toXML();
console.log(`${logPrefix} Generated PlivoXML Response: ${xmlResponse}`);
response.setHeader('Content-Type', 'application/xml');
response.status(200).send(xmlResponse);
console.log(`${logPrefix} Successfully sent XML reply.`);
} catch (error) {
console.error(`${logPrefix} Error generating PlivoXML response:`, error);
response.status(500).send("Internal Server Error generating response.");
}
});
Why Validate? Without validation, anyone could send fake requests to your webhook URL, potentially triggering unwanted actions, exhausting resources, or attempting to exploit your application. Signature validation ensures that only legitimate requests originating from Plivo (and signed with your secret) are processed.
Set up a Node.js Express server as a webhook endpoint for Plivo. When an SMS is sent to your Plivo number, Plivo forwards the message details to your Express app, which processes the information and uses PlivoXML to send a reply SMS back to the sender, creating a two-way conversation.
PlivoXML (Plivo eXtensible Markup Language) is used to control communication flows within the Plivo platform. Your Express app generates PlivoXML to instruct Plivo on how to handle incoming SMS messages and how to respond to them, enabling dynamic replies and other actions.
Node.js is well-suited for handling concurrent webhook requests due to its event-driven, non-blocking I/O model. This efficiency is crucial for real-time communication applications like SMS, where many messages might arrive simultaneously.
Always validate the Plivo webhook signature before processing any message content in your application. This crucial security step ensures the request genuinely originated from Plivo and hasn't been tampered with, protecting your application from malicious actors.
While this tutorial uses Express.js for its simplicity and wide usage, you can use other Node.js web frameworks. The core principle is setting up a webhook endpoint to receive HTTP requests from Plivo and responding with PlivoXML instructions.
ngrok creates a public, secure tunnel to your locally running Express server. This allows Plivo to send webhook requests to your development environment even if it's behind a firewall or not publicly accessible, essential for testing during development.
Store sensitive data like Plivo API keys and Auth Tokens as environment variables (e.g., in a
.env file during development, or set on your hosting platform in production). Use
dotenv package to load from .env. Avoid hardcoding credentials directly into your
code.
The express.urlencoded({ extended: true }) middleware parses incoming request bodies in
URL-encoded format (which Plivo uses for webhooks) and makes the data available in request.body,
allowing you to access message parameters.
Use try...catch blocks to handle PlivoXML generation errors and potential errors during outbound
Plivo API calls. Implement global error handlers in Express to catch any unhandled exceptions. Consider retry
mechanisms for outbound API calls and design for idempotency to handle webhook retries gracefully.
While the core webhook tutorial doesn't include a database, for production you might use PostgreSQL, MySQL, MongoDB, or other databases. Consider ORMs like Prisma for easier database interaction. Design a schema to log message details, status, and potentially user data or conversation context.
In your webhook handler, generate a PlivoXML response using plivo.Response() from the Plivo
Node.js SDK. Add a <Message> element specifying the reply text, the source number (your
Plivo number), and the destination number (sender of the original message). Ensure the response
Content-Type header is application/xml.
Find your Plivo Auth ID and Auth Token on the main dashboard page of the Plivo Console after logging into your account. These are essential for configuring both your webhook and for any outbound API calls your application might make.
Simpler Alternative: Send the Same SMS With SMSRoute (No-KYC, Direct Routes)
If you want to replace legacy aggregator SDK boilerplate, multi-week compliance queues, and opaque carrier surcharges with a single HTTP request, SMSRoute delivers global SMS over direct Tier-1 carrier interconnects with instant API access and transparent per-country pricing.
curl -X POST https://api.sendsmsnokyc.com/messages \
-H "x-api-key: $SMSROUTE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"to": ["+14155551234"],
"channel": ["sms"],
"body": "Your verification code is 482910"
}'
- Instant onboarding: No KYC paperwork or sales queues — grab an API key and fund with crypto (BTC/USDT) or card.
- Direct Tier-1 routes: Verify real handset delivery, DLR latency, and Sender ID preservation before launch via SMSRoute Live Carrier Testing.
- Transparent global pricing: Compare exact per-country termination rates on our live pricing page.
Ship Global SMS in One API Call
Skip legacy SDK sprawl and carrier bottlenecks. Send OTPs, alerts, and bulk campaigns across 190+ countries with SMSRoute.
Explore SMSRoute Developers →