Skip to main content
  • Node
  • Ruby
  • Python
  • PHP
  • .NET
  • Java
  • Go

Overview

This guide shows how to forward incoming SMS messages from a Plivo phone number to another number using Plivo APIs and Node.js.

How it works

When Plivo receives an SMS on your Plivo number, it makes an HTTP request to the “Message URL” configured in your Plivo application. Your server receives this request and must respond with an XML document that contains instructions for Plivo. In this case, the XML instructs Plivo to send a new message containing the original text to a different destination number.

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.

Create an Express server to forward messages

Create a file named forward_sms.js and paste this code into it. Remember to replace the <destination_number> placeholder with the actual number you want to forward messages to, in E.164 format (e.g., +12025551234).
const plivo = require('plivo');
const express = require('express');
const bodyParser = require('body-parser');
const app = express();

app.use(bodyParser.urlencoded({ extended: true }));

app.all('/forwardsms/', (request, response) => {
    const to_number = request.body.To || request.query.To;
    const text = request.body.Text || request.query.Text;

    const r = plivo.Response();
    const params = {
        'src': to_number, // The Plivo number the original message was sent to
        'dst': "<destination_number>", // The number to forward the message to
    };

    r.addMessage(text, params);

    response.type('application/xml');
    response.end(r.toXML());
});

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

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., Forward SMS). In the Message URL field, enter your server URL (e.g., https://<yourdomain>.com/forwardsms/) 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. The content of your message should be forwarded to the destination number you specified in the code.
I