Sinch Engage: Archive and Acknowledge Delivery Reports
See more Sinch Engage Examples
Fetch one batch of up to 100 pending delivery reports, save the entire batch in a private local inbox, then acknowledge only those record IDs. Acknowledged records stop appearing in subsequent polls. Run again to drain further batches; this is a queue, not page-number pagination.
This example treats a checked local archive write as acceptance for later processing. A production inbox should commit durable, atomic storage and deduplicate by account plus delivery_report_id before applying business effects. A failed acknowledgement can cause redelivery. Coordinate consumers: polling covers the account and acknowledgements affect other pollers. Protect the archive because it contains personal data.
Request delivery_report=true when sending. Several reports can describe the same message: deduplicate by delivery_report_id, not message_id, and use event timestamps when applying changes. Reports may carry an additional charge.
Sinch Engage is the renamed MessageMedia platform. This uses the Engage API, not Sinch Conversation API credentials or endpoints. Create an Engage API key/secret and place {"api_key":"YOUR_KEY","api_secret":"YOUR_SECRET"} in private sinch-engage.json. Select the account's regional HTTPS host: https://au.app.api.sinch.com, https://eu.app.api.sinch.com, or https://us.app.api.sinch.com. Basic authentication here means the API key/secret over verified TLS. Do not ship account credentials in a public mobile, browser, or desktop application.
Transport success and API success are checked separately. On HTTP 429, honor Retry-After before a bounded retry. Check account permissions and regional credentials for 401/403. A timeout or 5xx response after submitting a write can leave its outcome uncertain: reconcile before retrying, especially sends and resource creation. Metadata is correlation data, not an idempotency key. Inspect error bodies privately because they can contain phone numbers and message content.
Chilkat C++ Downloads
#include <CkJsonObject.h>
#include <CkHttp.h>
#include <CkStringBuilder.h>
#include <CkHttpResponse.h>
#include <CkFileAccess.h>
#include <CkCrypt2.h>
void ChilkatSample(void)
{
bool success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
// Store real credentials in private configuration, never in distributed client code.
CkJsonObject config;
config.put_Strict(true);
success = config.LoadFile("sinch-engage.json");
if (success != true) {
std::cout << "Could not load private sinch-engage.json." << "\r\n";
return;
}
if ((config.JsonTypeOf("api_key") != 1) || (config.StringOfEquals("api_key","",true) == true) || (config.JsonTypeOf("api_secret") != 1) || (config.StringOfEquals("api_secret","",true) == true)) {
std::cout << "The private configuration must contain api_key and api_secret." << "\r\n";
return;
}
// Choose the host for your Engage account: au, eu, or us. Do not guess from the recipient.
const char *apiHost = "https://au.app.api.sinch.com";
CkHttp http;
http.put_RequireSslCertVerify(true);
http.put_FollowRedirects(false);
http.put_ConnectTimeout(20);
http.put_ReadTimeout(30);
http.put_Login(config.stringOf("api_key"));
http.put_Password(config.stringOf("api_secret"));
http.put_BasicAuth(true);
http.SetRequestHeader("Accept","application/json");
CkStringBuilder url;
CkHttpResponse resp;
CkJsonObject result;
result.put_Strict(true);
url.Clear();
url.Append(apiHost);
url.Append("/v1/delivery_reports");
success = http.HttpNoBody("GET",url.getAsString(),resp);
// HttpJson/HttpNoBody also return false for HTTP 4xx/5xx responses.
if ((success != true) && (resp.get_StatusCode() == 0)) {
std::cout << "HTTP transport failed. Inspect diagnostics privately; do not blindly retry a write." << "\r\n";
return;
}
if ((resp.get_StatusCode() != 200)) {
std::cout << "Unexpected HTTP status: " << resp.get_StatusCode() << "\r\n";
if (resp.get_StatusCode() == 429) {
std::cout << "Retry-After: " << resp.getHeaderField("Retry-After") << "\r\n";
}
return;
}
success = result.Load(resp.bodyStr());
if (success != true) {
std::cout << "Expected a JSON response." << "\r\n";
return;
}
if (result.SizeOfArray("delivery_reports") < 0) {
std::cout << "Expected an array in the response." << "\r\n";
return;
}
int count = result.SizeOfArray("delivery_reports");
if (count == 0) {
std::cout << "No pending records." << "\r\n";
return;
}
if (count > 100) {
std::cout << "Unexpected batch size; nothing was acknowledged." << "\r\n";
return;
}
CkJsonObject confirmation;
int i = 0;
while ((i < count)) {
result.put_I(i);
confirmation.put_I(i);
if ((result.JsonTypeOf("delivery_reports[i].delivery_report_id") != 1) || (result.StringOfEquals("delivery_reports[i].delivery_report_id","",true) == true)) {
std::cout << "The response did not contain the expected ID." << "\r\n";
return;
}
confirmation.UpdateString("delivery_report_ids[i]",result.stringOf("delivery_reports[i].delivery_report_id"));
i = i + 1;
}
// Preserve the complete payload, including channel-specific and future fields.
CkFileAccess fac;
success = fac.DirEnsureExists("engage-inbox/delivery_reports");
if (success != true) {
std::cout << "Could not create the private inbox directory; no acknowledgement sent." << "\r\n";
return;
}
CkCrypt2 hash;
hash.put_HashAlgorithm("sha256");
hash.put_Charset("utf-8");
hash.put_EncodingMode("hex");
const char *digest = hash.hashStringENC(resp.bodyStr());
if (hash.get_LastMethodSuccess() != true) {
std::cout << "Could not name the archive; no acknowledgement sent." << "\r\n";
return;
}
CkStringBuilder archive;
archive.Append("engage-inbox/delivery_reports/");
archive.Append(digest);
archive.Append(".json");
success = result.WriteFile(archive.getAsString());
if (success != true) {
std::cout << "Could not save the records; no acknowledgement sent." << "\r\n";
return;
}
// Acknowledge only after the checked archive write succeeds.
url.Clear();
url.Append(apiHost);
url.Append("/v1/delivery_reports/confirmed");
success = http.HttpJson("POST",url.getAsString(),confirmation,"application/json",resp);
// HttpJson/HttpNoBody also return false for HTTP 4xx/5xx responses.
if ((success != true) && (resp.get_StatusCode() == 0)) {
std::cout << "HTTP transport failed. Inspect diagnostics privately; do not blindly retry a write." << "\r\n";
return;
}
if ((resp.get_StatusCode() != 202)) {
std::cout << "Unexpected HTTP status: " << resp.get_StatusCode() << "\r\n";
if (resp.get_StatusCode() == 429) {
std::cout << "Retry-After: " << resp.getHeaderField("Retry-After") << "\r\n";
}
return;
}
std::cout << "Archived and acknowledged records: " << count << "\r\n";
}