Skip to main content

Overview

This guide shows how to receive and reply to SMS text messages on a Plivo phone number using Plivo APIs and Node.js.

Prerequisites

To get started, you need a Plivo account — sign up if you don’t have one. You’ll also need a Plivo phone number that supports SMS. If you’re new to Plivo APIs, follow our instructions to set up a Node.js development environment and expose your web server to the internet.

Create an Express server to reply to messages

Create a file named reply_sms.js and paste this code into it. This server listens for incoming messages at the /replysms/ endpoint and replies with a thank you message using Plivo’s XML.
const plivo = require('plivo');
const express = require('express');
const bodyParser = require('body-parser');
const app = express();

app.use(bodyParser.urlencoded({ extended: true }));
app.use((req, response, next) => {
    response.contentType('application/xml');
    next();
});

app.set('port', (process.env.PORT || 3000));

app.all('/replysms/', (request, response) => {
    const from_number = request.body.From || request.query.From;
    const to_number = request.body.To || request.query.To;
    const text = request.body.Text || request.query.Text;
    console.log(`Message received - From: ${from_number}, To: ${to_number}, Text: ${text}`);

    const r = plivo.Response();
    const params = {
        'src': to_number,
        'dst': from_number,
    };
    const message_body = "Thank you, we received your request";
    r.addMessage(message_body, params);

    response.end(r.toXML());
});

app.listen(app.get('port'), () => {
    console.log('Node app is running on port', app.get('port'));
});

Create and configure a Plivo application

  1. Create an Application: Go to Messaging > Applications in the Plivo console and click Add New Application.
  2. Configure the URL: Give the application a name (e.g., Reply Incoming SMS). In the Message URL field, enter your server URL (e.g., https://<yourdomain>.com/replysms/) and set the method to POST. Click Create Application.
  3. Assign a Number: Navigate to the Numbers page and select the phone number you want to use. In the “Application Type” dropdown, select XML Application, and in the “Plivo Application” dropdown, select the app you just created. Click Update Number.

Test it out

Send a text message to your Plivo number. You should receive an automated reply saying, “Thank you, we received your request.”