Sample code for 30+ languages & platforms
Lua Requires Chilkat v11.6.0+

Sinch Engage: Read a Message Detail Report (Preview API)

See more Sinch Engage Examples

Query a fixed UTC window covering the previous 24 hours and read every page up to a demonstration limit. This uses the explicitly versioned v2-preview reporting API; verify account access and current schema before production use. It uses channels rather than the deprecated message_format filter. Reports are historical queries, distinct from the consumable delivery-report queue. Accounts with sub-accounts can return a wider scope unless accounts is supplied. Large exports should use the asynchronous reporting API.

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.

Sinch Engage API reference

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- Assumes Chilkat is unlocked. See Global Unlock Sample.

-- Store real credentials in private configuration, never in distributed client code.
local config = chilkat.JsonObject.new()
config.strict = true
local success = config:load_file("sinch-engage.json")
if not success then
    print("Could not load private sinch-engage.json.")
    return
end

if (config:json_type_of("api_key") ~= 1) or ((config:string_of_equals("api_key", "", true)) or ((config:json_type_of("api_secret") ~= 1) or (config:string_of_equals("api_secret", "", true)))) then
    print("The private configuration must contain api_key and api_secret.")
    return
end

-- Choose the host for your Engage account: au, eu, or us. Do not guess from the recipient.
local apiHost = "https://au.app.api.sinch.com"
local http = chilkat.Http.new()
http.require_ssl_cert_verify = true
http.follow_redirects = false
http.connect_timeout = 20
http.read_timeout = 30
http.login = config:string_of("api_key")
http.password = config:string_of("api_secret")
http.basic_auth = true
http:set_request_header("Accept", "application/json")
local url = chilkat.StringBuilder.new()
local resp = chilkat.HttpResponse.new()
local result = chilkat.JsonObject.new()
result.strict = true
local body = chilkat.JsonObject.new()
local window = chilkat.DateTime.new()
success = window:set_from_current_system_time()
if not success then
    print("Could not get the report end time.")
    return
end

body:update_string("end_date", window:get_as_timestamp(false))
success = window:add_seconds(-86400)
if not success then
    print("Could not get the report start time.")
    return
end

body:update_string("start_date", window:get_as_timestamp(false))
body:update_string("direction", "all")
body:update_string("channels[0]", "SMS")
body:update_string("channels[1]", "MMS")
body:update_string("channels[2]", "TTS")
body:update_string("channels[3]", "RCS")
body:update_int("page_size", 100)
local page = 0
while page < 100 do
    body:update_int("page", page)
    url:clear()
    url:append(apiHost)
    url:append("/v2-preview/reporting/messages/detail")
    success = http:http_json("POST", url:get_as_string(), body, "application/json", resp)
    -- HttpJson/HttpNoBody also return false for HTTP 4xx/5xx responses.
    if (not success) and (resp.status_code == 0) then
        print("HTTP transport failed. Inspect diagnostics privately; do not blindly retry a write.")
        return
    end

    if resp.status_code ~= 200 then
        print("Unexpected HTTP status: " .. resp.status_code)
        if resp.status_code == 429 then
            print("Retry-After: " .. resp:get_header_field("Retry-After"))
        end

        return
    end

    success = result:load(resp.body_str)
    if not success then
        print("Expected a JSON response.")
        return
    end

    if result:size_of_array("messages") < 0 then
        print("Expected an array in the response.")
        return
    end

    local i = 0
    while i < result:size_of_array("messages") do
        result.i = i
        print("Message ID: " .. result:string_of("messages[i].message_id"))
        print("Status: " .. result:string_of("messages[i].status"))
        i = i + 1
    end

    if result:json_type_of("pagination.has_next") ~= 5 then
        print("Missing pagination.has_next; cannot establish completeness.")
        return
    end

    if not result:bool_of("pagination.has_next") then
        print("Report complete.")
        return
    end

    page = page + 1
end

print("Stopped at the example's 100-page limit. Resume the same time window and page.")