Skip to main content

Overview

This guide shows how to receive incoming SMS messages on a Plivo 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” you configure in your Plivo Application. Your server receives this request, and you can process the data from the message (such as the sender’s number, the recipient’s number, and the text). Your server should respond with a 200 OK status code to acknowledge receipt.

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 receive messages

Create a file named receive_sms.js and paste this code into it. This server listens for POST requests at the /receive_sms/ endpoint and logs the message details to the console.
const express = require('express');
const bodyParser = require('body-parser');
const app = express();

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

app.all('/receive_sms/', (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}`);

    // A 200 OK response is sent by default
    response.send("Message received");
});

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., Receive SMS). In the Message URL field, enter your server URL (e.g., https://<yourdomain>.com/receive_sms/) 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. Your console where the Node.js app is running should print the details of the message.