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

Sinch Engage: Authenticate a GET Request with HMAC-SHA256

See more Sinch Engage Examples

Use an Engage HMAC API key/secret to sign a read-only message-status request. This is an alternative to Basic authentication. The signature covers the Date header and exact HTTP request line, separated by one LF, with no final newline. Base64-encode the raw HMAC-SHA256 bytes, not hexadecimal text. Keep the clock accurate and sign again for each request.

This no-body GET example deliberately omits Content-MD5. For a request with a body, follow Engage's additional Content-MD5/signing requirements and hash the exact transmitted bytes. API request HMAC and RSA-signed webhook verification are different mechanisms. Parent-account requests can use the documented Account header on supported resources; never assume that header applies to every API family.

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: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 messageId = "YOUR_MESSAGE_ID"
local path = chilkat.StringBuilder.new()
path:append("/v1/messages/")
path:append(http:url_encode(messageId))
local now = chilkat.DateTime.new()
success = now:set_from_current_system_time()
if not success then
    print("Could not get the signing time.")
    return
end

local dateHeader = now:get_as_rfc822(false)
local signing = chilkat.StringBuilder.new()
signing:append("Date: ")
signing:append(dateHeader)
signing:append("\nGET ")
signing:append(path:get_as_string())
signing:append(" HTTP/1.1")
local mac = chilkat.Crypt2.new()
mac.mac_algorithm = "hmac"
mac.hash_algorithm = "sha256"
mac.charset = "utf-8"
mac.encoding_mode = "base64"
success = mac:set_mac_key_string(config:string_of("api_secret"))
if not success then
    print("Could not set the signing key.")
    return
end

local signature = mac:mac_string_enc(signing:get_as_string())
if not mac.last_method_success then
    print("Could not sign the request.")
    return
end

local authorization = chilkat.StringBuilder.new()
authorization:append("hmac username=\"")
authorization:append(config:string_of("api_key"))
authorization:append("\", algorithm=\"hmac-sha256\", headers=\"Date request-line\", signature=\"")
authorization:append(signature)
authorization:append("\"")
http:set_request_header("Date", dateHeader)
http:set_request_header("Authorization", authorization:get_as_string())
url:clear()
url:append(apiHost)
url:append("")
url:append(path:get_as_string())
success = http:http_no_body("GET", url:get_as_string(), 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

print("Current status: " .. result:string_of("status"))