Skip to main content

Overview

This guide shows how to receive delivery reports for your SMS messages using Plivo’s APIs and Node.js. Delivery reports are webhooks Plivo sends to your application to notify you of the status of inbound or outbound messages.

How it works

  • Outbound Messages: When you send an SMS using Plivo’s API, you can include a url parameter. Plivo will send a POST request to this URL with the status of the message (sent, delivered, failed, etc.).
  • Inbound Messages: When you receive an SMS on a Plivo number, Plivo sends the message details to the Message URL configured on your Plivo Application.
In both cases, your application should be set up to receive these POST requests, log the data, and return a 200 OK response to acknowledge receipt.

Prerequisites


Create a server to receive delivery reports

Create a file named delivery_reports.js and paste this code into it. This Express server has a single endpoint that logs the body of incoming POST requests to the console.
const express = require('express');
const bodyParser = require('body-parser');
const app = express();

app.use(bodyParser.urlencoded({ extended: true }));
app.set('port', (process.env.PORT || 5000));

app.all('/delivery_report/', (request, response) => {
    console.log("Delivery Report Received:");
    console.log(request.body);
    response.status(204).send(); // Acknowledge receipt
});

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

Test

  1. Run your server: node delivery_reports.js.
  2. Expose your local server to the internet using ngrok: ngrok http 5000.
For Outbound Reports:
  • Use the following script to send a message. Replace the placeholders and use your ngrok URL for the url parameter.
    const plivo = require('plivo');
    const client = new plivo.Client("<auth_id>", "<auth_token>");
    
    client.messages.create({
        src: "<sender_id>",
        dst: "<destination_number>",
        text: "Hello, this is a test for delivery reports!",
        url: "https://<your-ngrok-url>.ngrok.io/delivery_report/"
    }).then(response => console.log(response));
    
  • Check your terminal. You should see the delivery status updates logged by your server.
For Inbound Reports:
  • Create a Plivo Application with the Message URL set to https://<your-ngrok-url>.ngrok.io/delivery_report/.
  • Assign a Plivo number to the application.
  • Send an SMS to your Plivo number. The message details will appear in your terminal.