Skip to content
Last updated

Retrieving delivery reports with the Python SDK

Note:

For testing purposes, we recommend you first send a message using the Getting Started Guide. Alternatively, you can test this method using the provided code sample.

Retrieve a delivery report for a batch

The get_for_batch method retrieves a delivery report by batch ID.

print(sinch_client.sms.delivery_reports.get_for_batch(send_batch_response.id))

When you run it, you should see an output like the following:

GetSMSDeliveryReportForBatchResponse(type='delivery_report_sms', batch_id='01HK7NV2211KBY6W6RR9XVYB1S',
total_message_count=1, statuses=[{'code': 400, 'count': 1, 'status': 'Queued'}], client_reference=None)

The method returns a Python object with a number of fields. Two are particularly important:

  • total_message_count shows how many messages were sent.
  • statuses gives a break down of the batch, showing how many messages belonged to a particular error code (refered to as code). This helps developers understand the proportion of messages that were delivered successfully versus those that failed.

The delivery report is a summary report by default, so recipients aren't shown. If you want to see the recipients, you can set the delivery report to full with the type_ parameter:

print(sinch_client.sms.delivery_reports.get_for_batch(batch_id=send_batch_response.id,
                                                type_="full",
                                                ))

The response should be something like:

GetSMSDeliveryReportForBatchResponse(type='delivery_report_sms', batch_id='01HK7P4ZD1293D7PYA5RJS0T7T',
total_message_count=1, statuses=[{'code': 400, 'count': 1, 'recipients': ['447987654321'], 'status': 'Queued'}], client_reference=None)

As you can see, statuses now contains an extra field, recipients, a list of the phone numbers the batch was sent to.

Delivery reports can also be filtered by status and code:

print(sinch_client.sms.delivery_reports.get_for_batch(batch_id=send_batch_response.id,
                                                type_="full",
                                                status="Queued,Dispatched,Delivered",
                                                code="400,401"
                                                ))

This example will show messages that are either delivered or in the process of being delivered. Filtering enables you to curate your delivery report analytics by only selecting a subset of the data.

See more delivery report tutorials