Sample code for 30+ languages & platforms
Tcl 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 Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

#  Assumes Chilkat is unlocked. See Global Unlock Sample.

#  Store real credentials in private configuration, never in distributed client code.
set config [new_CkJsonObject]

CkJsonObject_put_Strict $config 1
set success [CkJsonObject_LoadFile $config "sinch-engage.json"]
if {$success != 1} then {
    puts "Could not load private sinch-engage.json."
    delete_CkJsonObject $config
    exit
}

if {expr [[CkJsonObject_JsonTypeOf $config "api_key"] != 1]  ||  [expr [[CkJsonObject_StringOfEquals $config "api_key" "" 1] == 1]  ||  [expr [[CkJsonObject_JsonTypeOf $config "api_secret"] != 1]  ||  [[CkJsonObject_StringOfEquals $config "api_secret" "" 1] == 1]]]} then {
    puts "The private configuration must contain api_key and api_secret."
    delete_CkJsonObject $config
    exit
}

#  Choose the host for your Engage account: au, eu, or us. Do not guess from the recipient.
set apiHost "https://au.app.api.sinch.com"
set http [new_CkHttp]

CkHttp_put_RequireSslCertVerify $http 1
CkHttp_put_FollowRedirects $http 0
CkHttp_put_ConnectTimeout $http 20
CkHttp_put_ReadTimeout $http 30
CkHttp_put_Login $http [CkJsonObject_stringOf $config "api_key"]
CkHttp_put_Password $http [CkJsonObject_stringOf $config "api_secret"]
CkHttp_put_BasicAuth $http 1
CkHttp_SetRequestHeader $http "Accept" "application/json"
set url [new_CkStringBuilder]

set resp [new_CkHttpResponse]

set result [new_CkJsonObject]

CkJsonObject_put_Strict $result 1
set body [new_CkJsonObject]

set window [new_CkDateTime]

set success [CkDateTime_SetFromCurrentSystemTime $window]
if {$success != 1} then {
    puts "Could not get the report end time."
    delete_CkJsonObject $config
    delete_CkHttp $http
    delete_CkStringBuilder $url
    delete_CkHttpResponse $resp
    delete_CkJsonObject $result
    delete_CkJsonObject $body
    delete_CkDateTime $window
    exit
}

CkJsonObject_UpdateString $body "end_date" [CkDateTime_getAsTimestamp $window 0]
set success [CkDateTime_AddSeconds $window -86400]
if {$success != 1} then {
    puts "Could not get the report start time."
    delete_CkJsonObject $config
    delete_CkHttp $http
    delete_CkStringBuilder $url
    delete_CkHttpResponse $resp
    delete_CkJsonObject $result
    delete_CkJsonObject $body
    delete_CkDateTime $window
    exit
}

CkJsonObject_UpdateString $body "start_date" [CkDateTime_getAsTimestamp $window 0]
CkJsonObject_UpdateString $body "direction" "all"
CkJsonObject_UpdateString $body "channels[0]" "SMS"
CkJsonObject_UpdateString $body "channels[1]" "MMS"
CkJsonObject_UpdateString $body "channels[2]" "TTS"
CkJsonObject_UpdateString $body "channels[3]" "RCS"
CkJsonObject_UpdateInt $body "page_size" 100
set page 0
while {$page < 100} {
    CkJsonObject_UpdateInt $body "page" $page
    CkStringBuilder_Clear $url
    CkStringBuilder_Append $url $apiHost
    CkStringBuilder_Append $url "/v2-preview/reporting/messages/detail"
    set success [CkHttp_HttpJson $http "POST" [CkStringBuilder_getAsString $url] $body "application/json" $resp]
    #  HttpJson/HttpNoBody also return false for HTTP 4xx/5xx responses.
    if {expr [$success != 1]  &&  [[CkHttpResponse_get_StatusCode $resp] == 0]} then {
        puts "HTTP transport failed. Inspect diagnostics privately; do not blindly retry a write."
        delete_CkJsonObject $config
        delete_CkHttp $http
        delete_CkStringBuilder $url
        delete_CkHttpResponse $resp
        delete_CkJsonObject $result
        delete_CkJsonObject $body
        delete_CkDateTime $window
        exit
    }

    if {[CkHttpResponse_get_StatusCode $resp] != 200} then {
        puts "Unexpected HTTP status: [CkHttpResponse_get_StatusCode $resp]"
        if {[CkHttpResponse_get_StatusCode $resp] == 429} then {
            puts "Retry-After: [CkHttpResponse_getHeaderField $resp Retry-After]"
        }

        delete_CkJsonObject $config
        delete_CkHttp $http
        delete_CkStringBuilder $url
        delete_CkHttpResponse $resp
        delete_CkJsonObject $result
        delete_CkJsonObject $body
        delete_CkDateTime $window
        exit
    }

    set success [CkJsonObject_Load $result [CkHttpResponse_bodyStr $resp]]
    if {$success != 1} then {
        puts "Expected a JSON response."
        delete_CkJsonObject $config
        delete_CkHttp $http
        delete_CkStringBuilder $url
        delete_CkHttpResponse $resp
        delete_CkJsonObject $result
        delete_CkJsonObject $body
        delete_CkDateTime $window
        exit
    }

    if {[CkJsonObject_SizeOfArray $result "messages"] < 0} then {
        puts "Expected an array in the response."
        delete_CkJsonObject $config
        delete_CkHttp $http
        delete_CkStringBuilder $url
        delete_CkHttpResponse $resp
        delete_CkJsonObject $result
        delete_CkJsonObject $body
        delete_CkDateTime $window
        exit
    }

    set i 0
    while {$i < [CkJsonObject_SizeOfArray $result "messages"]} {
        CkJsonObject_put_I $result $i
        puts "Message ID: [CkJsonObject_stringOf $result {messages[i].message_id}]"
        puts "Status: [CkJsonObject_stringOf $result {messages[i].status}]"
        set i [expr $i + 1]
    }
    if {[CkJsonObject_JsonTypeOf $result "pagination.has_next"] != 5} then {
        puts "Missing pagination.has_next; cannot establish completeness."
        delete_CkJsonObject $config
        delete_CkHttp $http
        delete_CkStringBuilder $url
        delete_CkHttpResponse $resp
        delete_CkJsonObject $result
        delete_CkJsonObject $body
        delete_CkDateTime $window
        exit
    }

    if {[CkJsonObject_BoolOf $result "pagination.has_next"] != 1} then {
        puts "Report complete."
        delete_CkJsonObject $config
        delete_CkHttp $http
        delete_CkStringBuilder $url
        delete_CkHttpResponse $resp
        delete_CkJsonObject $result
        delete_CkJsonObject $body
        delete_CkDateTime $window
        exit
    }

    set page [expr $page + 1]
}
puts "Stopped at the example's 100-page limit. Resume the same time window and page."

delete_CkJsonObject $config
delete_CkHttp $http
delete_CkStringBuilder $url
delete_CkHttpResponse $resp
delete_CkJsonObject $result
delete_CkJsonObject $body
delete_CkDateTime $window