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:
- Set all SMS API configuration settings.
- Node.js and a familiarity with how to create a new app.
- Completed the Name a batch tutorial and submitted a
client_reference
value.
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
- Once your free Sinch account is setup and you have a virtual number, create a new folder for your app.
- Then start up a node app with npm.
npm init
- Accept the defaults for the application.
- Add the fetch package with npm to generate the necessary dependencies.
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
- Assign your values to the following parameters:
Parameter | Your value |
---|---|
YOUR_service_plan_id | The service plan ID found on your Sinch Customer Dashboard. SMS > APIs > REST configuration |
YOUR_API_token | The API token found on your Sinch Customer dashboard. SMS > APIs > REST configuration > Click Show to reveal your API token. |
client_reference | Include 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.
- Save the file.
Run the code
- Run the following command in your terminal/command prompt to create a group:
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
.
{
"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
- Find other useful code samples in our API reference.
- Check out another tutorial.