Filter a list of delivery reports with Node.js

In this tutorial, we will show you how to filter a list of delivery reports using a query parameter.

In the Name a batch tutorial, we showed how to set a value for client_reference. Now we'll search for delivery reports containing that value.

Before you start

You must have:

What's covered?
  • Setting up a Sinch application in Node.js
  • Getting a filtered delivery report based on query values set when sending a message.
Goal

At the end of this tutorial, you will have queried a list of delivery reports to find a value set in client_reference.

Let's do this.

Set up your application

  1. Once your free Sinch account is setup and you have a virtual number, create a new folder for your app.
  2. Then start up a node app with npm.
    Copy
    Copied
    npm init
  3. Accept the defaults for the application.
  4. Add the fetch package with npm to generate the necessary dependencies.
    Copy
    Copied
    npm install node-fetch
Note:

The node fetch package requires us to use node modules. We'll use .mjs files instead of .js files.

Next we'll use the /delivery_reports endpoint to get a filtered list of reports that contain the value you previously entered for client_reference.

Create a file

Copy and paste the "Filtered delivery report" code into a new file called deliveryReport.mjs.

Filter a list of delivery reports

import fetch from 'node-fetch';

async function run() {
  const servicePlanId = 'YOUR_service_plan_id';
  const resp = await fetch(
    `https://us.sms.api.sinch.com/xms/v1/${servicePlanId}/delivery_reports`,
    {
      method: 'GET',
      headers: {
        Authorization: 'Bearer YOUR_API_token'
      },
      query: JSON.stringify({
        client_reference: 'YOUR_value_previously_set'
      })
    }
  );

  const data = await resp.text();
  console.log(data);
}

run();


Fill in your parameters

  1. Assign your values to the following parameters:
ParameterYour value
YOUR_service_plan_idThe service plan ID found on your Sinch Customer Dashboard. SMS > APIs > REST configuration
YOUR_API_tokenThe API token found on your Sinch Customer dashboard. SMS > APIs > REST configuration > Click Show to reveal your API token.
client_referenceInclude the reference you entered in the previous step. Example: client_reference: "Positive responses to X78695125856AB".
Note:

Since this is a GET request, we'll search based on query parameters rather a body.

  1. Save the file.

Run the code

  1. Run the following command in your terminal/command prompt to create a group:
    Copy
    Copied
    node deliveryReport.mjs

Successful response

The response should only include the batch that matches the value you entered. Notice the given name of the batch shows under client_reference.

Copy
Copied
{
  "count": 1,
  "page": 0,
  "page_size": 1,
  "delivery_reports": [
    {
      "applied_originator": "string",
      "at": "2022-08-24T14:15:22Z",
      "batch_id": "XXXXX6621VXXXXX19Z8PMXXXXX",
      "client_reference": "YOUR_ID_or_name_shows_here",
      "code": 0,
      "encoding": "GSM",
      "number_of_message_parts": 1,
      "operator": "35000",
      "operator_status_at": "2022-08-24T14:15:22Z",
      "recipient": "15551231234",
      "status": "Delivered",
      "type": "recipient_delivery_report_sms"
    }
  ]
}

Filtered results. Bam!

Next steps

Was this page helpful?

Filter a list of delivery reports

import fetch from 'node-fetch';

async function run() {
  const servicePlanId = 'YOUR_service_plan_id';
  const resp = await fetch(
    `https://us.sms.api.sinch.com/xms/v1/${servicePlanId}/delivery_reports`,
    {
      method: 'GET',
      headers: {
        Authorization: 'Bearer YOUR_API_token'
      },
      query: JSON.stringify({
        client_reference: 'YOUR_value_previously_set'
      })
    }
  );

  const data = await resp.text();
  console.log(data);
}

run();