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.
Chilkat PowerBuilder Downloads
integer li_rc
long li_Success
oleobject loo_Config
string ls_ApiHost
oleobject loo_Http
oleobject loo_Url
oleobject loo_Resp
oleobject loo_Result
string ls_MessageId
oleobject loo_Path
oleobject loo_Now
string ls_DateHeader
oleobject loo_Signing
oleobject loo_Mac
string ls_Signature
oleobject loo_Authorization
li_Success = 0
// Assumes Chilkat is unlocked. See Global Unlock Sample.
// Store real credentials in private configuration, never in distributed client code.
loo_Config = create oleobject
li_rc = loo_Config.ConnectToNewObject("Chilkat.JsonObject")
if li_rc < 0 then
destroy loo_Config
MessageBox("Error","Connecting to COM object failed")
return
end if
loo_Config.Strict = 1
li_Success = loo_Config.LoadFile("sinch-engage.json")
if li_Success <> 1 then
Write-Debug "Could not load private sinch-engage.json."
destroy loo_Config
return
end if
if (loo_Config.JsonTypeOf("api_key") <> 1) OR (loo_Config.StringOfEquals("api_key","",1) = 1) OR (loo_Config.JsonTypeOf("api_secret") <> 1) OR (loo_Config.StringOfEquals("api_secret","",1) = 1) then
Write-Debug "The private configuration must contain api_key and api_secret."
destroy loo_Config
return
end if
// Choose the host for your Engage account: au, eu, or us. Do not guess from the recipient.
ls_ApiHost = "https://au.app.api.sinch.com"
loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
loo_Http.RequireSslCertVerify = 1
loo_Http.FollowRedirects = 0
loo_Http.ConnectTimeout = 20
loo_Http.ReadTimeout = 30
loo_Http.SetRequestHeader("Accept","application/json")
loo_Url = create oleobject
li_rc = loo_Url.ConnectToNewObject("Chilkat.StringBuilder")
loo_Resp = create oleobject
li_rc = loo_Resp.ConnectToNewObject("Chilkat.HttpResponse")
loo_Result = create oleobject
li_rc = loo_Result.ConnectToNewObject("Chilkat.JsonObject")
loo_Result.Strict = 1
ls_MessageId = "YOUR_MESSAGE_ID"
loo_Path = create oleobject
li_rc = loo_Path.ConnectToNewObject("Chilkat.StringBuilder")
loo_Path.Append("/v1/messages/")
loo_Path.Append(loo_Http.UrlEncode(ls_MessageId))
loo_Now = create oleobject
li_rc = loo_Now.ConnectToNewObject("Chilkat.CkDateTime")
li_Success = loo_Now.SetFromCurrentSystemTime()
if li_Success <> 1 then
Write-Debug "Could not get the signing time."
destroy loo_Config
destroy loo_Http
destroy loo_Url
destroy loo_Resp
destroy loo_Result
destroy loo_Path
destroy loo_Now
return
end if
ls_DateHeader = loo_Now.GetAsRfc822(0)
loo_Signing = create oleobject
li_rc = loo_Signing.ConnectToNewObject("Chilkat.StringBuilder")
loo_Signing.Append("Date: ")
loo_Signing.Append(ls_DateHeader)
loo_Signing.Append("~nGET ")
loo_Signing.Append(loo_Path.GetAsString())
loo_Signing.Append(" HTTP/1.1")
loo_Mac = create oleobject
li_rc = loo_Mac.ConnectToNewObject("Chilkat.Crypt2")
loo_Mac.MacAlgorithm = "hmac"
loo_Mac.HashAlgorithm = "sha256"
loo_Mac.Charset = "utf-8"
loo_Mac.EncodingMode = "base64"
li_Success = loo_Mac.SetMacKeyString(loo_Config.StringOf("api_secret"))
if li_Success <> 1 then
Write-Debug "Could not set the signing key."
destroy loo_Config
destroy loo_Http
destroy loo_Url
destroy loo_Resp
destroy loo_Result
destroy loo_Path
destroy loo_Now
destroy loo_Signing
destroy loo_Mac
return
end if
ls_Signature = loo_Mac.MacStringENC(loo_Signing.GetAsString())
if loo_Mac.LastMethodSuccess <> 1 then
Write-Debug "Could not sign the request."
destroy loo_Config
destroy loo_Http
destroy loo_Url
destroy loo_Resp
destroy loo_Result
destroy loo_Path
destroy loo_Now
destroy loo_Signing
destroy loo_Mac
return
end if
loo_Authorization = create oleobject
li_rc = loo_Authorization.ConnectToNewObject("Chilkat.StringBuilder")
loo_Authorization.Append("hmac username=~"")
loo_Authorization.Append(loo_Config.StringOf("api_key"))
loo_Authorization.Append("~", algorithm=~"hmac-sha256~", headers=~"Date request-line~", signature=~"")
loo_Authorization.Append(ls_Signature)
loo_Authorization.Append("~"")
loo_Http.SetRequestHeader("Date",ls_DateHeader)
loo_Http.SetRequestHeader("Authorization",loo_Authorization.GetAsString())
loo_Url.Clear()
loo_Url.Append(ls_ApiHost)
loo_Url.Append("")
loo_Url.Append(loo_Path.GetAsString())
li_Success = loo_Http.HttpNoBody("GET",loo_Url.GetAsString(),loo_Resp)
// HttpJson/HttpNoBody also return false for HTTP 4xx/5xx responses.
if (li_Success <> 1) AND (loo_Resp.StatusCode = 0) then
Write-Debug "HTTP transport failed. Inspect diagnostics privately; do not blindly retry a write."
destroy loo_Config
destroy loo_Http
destroy loo_Url
destroy loo_Resp
destroy loo_Result
destroy loo_Path
destroy loo_Now
destroy loo_Signing
destroy loo_Mac
destroy loo_Authorization
return
end if
if (loo_Resp.StatusCode <> 200) then
Write-Debug "Unexpected HTTP status: " + string(loo_Resp.StatusCode)
if loo_Resp.StatusCode = 429 then
Write-Debug "Retry-After: " + loo_Resp.GetHeaderField("Retry-After")
end if
destroy loo_Config
destroy loo_Http
destroy loo_Url
destroy loo_Resp
destroy loo_Result
destroy loo_Path
destroy loo_Now
destroy loo_Signing
destroy loo_Mac
destroy loo_Authorization
return
end if
li_Success = loo_Result.Load(loo_Resp.BodyStr)
if li_Success <> 1 then
Write-Debug "Expected a JSON response."
destroy loo_Config
destroy loo_Http
destroy loo_Url
destroy loo_Resp
destroy loo_Result
destroy loo_Path
destroy loo_Now
destroy loo_Signing
destroy loo_Mac
destroy loo_Authorization
return
end if
Write-Debug "Current status: " + loo_Result.StringOf("status")
destroy loo_Config
destroy loo_Http
destroy loo_Url
destroy loo_Resp
destroy loo_Result
destroy loo_Path
destroy loo_Now
destroy loo_Signing
destroy loo_Mac
destroy loo_Authorization