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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoConfig
    String sApiHost
    Handle hoHttp
    Handle hoUrl
    Variant vResp
    Handle hoResp
    Handle hoResult
    String sMessageId
    Handle hoPath
    Handle hoNow
    String sDateHeader
    Handle hoSigning
    Handle hoMac
    String sSignature
    Handle hoAuthorization
    String sTemp1
    Integer iTemp1
    Integer iTemp2
    Boolean bTemp1
    Boolean bTemp2

    Move False To iSuccess

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

    //  Store real credentials in private configuration, never in distributed client code.
    Get Create (RefClass(cComChilkatJsonObject)) To hoConfig
    If (Not(IsComObjectCreated(hoConfig))) Begin
        Send CreateComObject of hoConfig
    End
    Set ComStrict Of hoConfig To True
    Get ComLoadFile Of hoConfig "sinch-engage.json" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not load private sinch-engage.json."
        Procedure_Return
    End

    Get ComJsonTypeOf Of hoConfig "api_key" To iTemp1
    Get ComStringOfEquals Of hoConfig "api_key" "" True To bTemp1
    Get ComJsonTypeOf Of hoConfig "api_secret" To iTemp2
    Get ComStringOfEquals Of hoConfig "api_secret" "" True To bTemp2
    If ((iTemp1 <> 1) Or (bTemp1 = True) Or (iTemp2 <> 1) Or (bTemp2 = True)) Begin
        Showln "The private configuration must contain api_key and api_secret."
        Procedure_Return
    End

    //  Choose the host for your Engage account: au, eu, or us. Do not guess from the recipient.
    Move "https://au.app.api.sinch.com" To sApiHost
    Get Create (RefClass(cComChilkatHttp)) To hoHttp
    If (Not(IsComObjectCreated(hoHttp))) Begin
        Send CreateComObject of hoHttp
    End
    Set ComRequireSslCertVerify Of hoHttp To True
    Set ComFollowRedirects Of hoHttp To False
    Set ComConnectTimeout Of hoHttp To 20
    Set ComReadTimeout Of hoHttp To 30
    Send ComSetRequestHeader To hoHttp "Accept" "application/json"
    Get Create (RefClass(cComChilkatStringBuilder)) To hoUrl
    If (Not(IsComObjectCreated(hoUrl))) Begin
        Send CreateComObject of hoUrl
    End
    Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
    If (Not(IsComObjectCreated(hoResp))) Begin
        Send CreateComObject of hoResp
    End
    Get Create (RefClass(cComChilkatJsonObject)) To hoResult
    If (Not(IsComObjectCreated(hoResult))) Begin
        Send CreateComObject of hoResult
    End
    Set ComStrict Of hoResult To True
    Move "YOUR_MESSAGE_ID" To sMessageId
    Get Create (RefClass(cComChilkatStringBuilder)) To hoPath
    If (Not(IsComObjectCreated(hoPath))) Begin
        Send CreateComObject of hoPath
    End
    Get ComAppend Of hoPath "/v1/messages/" To iSuccess
    Get ComUrlEncode Of hoHttp sMessageId To sTemp1
    Get ComAppend Of hoPath sTemp1 To iSuccess
    Get Create (RefClass(cComCkDateTime)) To hoNow
    If (Not(IsComObjectCreated(hoNow))) Begin
        Send CreateComObject of hoNow
    End
    Get ComSetFromCurrentSystemTime Of hoNow To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not get the signing time."
        Procedure_Return
    End

    Get ComGetAsRfc822 Of hoNow False To sDateHeader
    Get Create (RefClass(cComChilkatStringBuilder)) To hoSigning
    If (Not(IsComObjectCreated(hoSigning))) Begin
        Send CreateComObject of hoSigning
    End
    Get ComAppend Of hoSigning "Date: " To iSuccess
    Get ComAppend Of hoSigning sDateHeader To iSuccess
    Get ComAppend Of hoSigning (character(10)) + "GET " To iSuccess
    Get ComGetAsString Of hoPath To sTemp1
    Get ComAppend Of hoSigning sTemp1 To iSuccess
    Get ComAppend Of hoSigning " HTTP/1.1" To iSuccess
    Get Create (RefClass(cComChilkatCrypt2)) To hoMac
    If (Not(IsComObjectCreated(hoMac))) Begin
        Send CreateComObject of hoMac
    End
    Set ComMacAlgorithm Of hoMac To "hmac"
    Set ComHashAlgorithm Of hoMac To "sha256"
    Set ComCharset Of hoMac To "utf-8"
    Set ComEncodingMode Of hoMac To "base64"
    Get ComStringOf Of hoConfig "api_secret" To sTemp1
    Get ComSetMacKeyString Of hoMac sTemp1 To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not set the signing key."
        Procedure_Return
    End

    Get ComGetAsString Of hoSigning To sTemp1
    Get ComMacStringENC Of hoMac sTemp1 To sSignature
    Get ComLastMethodSuccess Of hoMac To bTemp1
    If (bTemp1 <> True) Begin
        Showln "Could not sign the request."
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatStringBuilder)) To hoAuthorization
    If (Not(IsComObjectCreated(hoAuthorization))) Begin
        Send CreateComObject of hoAuthorization
    End
    Get ComAppend Of hoAuthorization 'hmac username="' To iSuccess
    Get ComStringOf Of hoConfig "api_key" To sTemp1
    Get ComAppend Of hoAuthorization sTemp1 To iSuccess
    Get ComAppend Of hoAuthorization '", algorithm="hmac-sha256", headers="Date request-line", signature="' To iSuccess
    Get ComAppend Of hoAuthorization sSignature To iSuccess
    Get ComAppend Of hoAuthorization '"' To iSuccess
    Send ComSetRequestHeader To hoHttp "Date" sDateHeader
    Get ComGetAsString Of hoAuthorization To sTemp1
    Send ComSetRequestHeader To hoHttp "Authorization" sTemp1
    Send ComClear To hoUrl
    Get ComAppend Of hoUrl sApiHost To iSuccess
    Get ComAppend Of hoUrl "" To iSuccess
    Get ComGetAsString Of hoPath To sTemp1
    Get ComAppend Of hoUrl sTemp1 To iSuccess
    Get ComGetAsString Of hoUrl To sTemp1
    Get pvComObject of hoResp to vResp
    Get ComHttpNoBody Of hoHttp "GET" sTemp1 vResp To iSuccess
    //  HttpJson/HttpNoBody also return false for HTTP 4xx/5xx responses.
    Get ComStatusCode Of hoResp To iTemp1
    If ((iSuccess <> True) And (iTemp1 = 0)) Begin
        Showln "HTTP transport failed. Inspect diagnostics privately; do not blindly retry a write."
        Procedure_Return
    End

    Get ComStatusCode Of hoResp To iTemp1
    If ((iTemp1 <> 200)) Begin
        Get ComStatusCode Of hoResp To iTemp1
        Showln "Unexpected HTTP status: " iTemp1
        Get ComStatusCode Of hoResp To iTemp1
        If (iTemp1 = 429) Begin
            Get ComGetHeaderField Of hoResp "Retry-After" To sTemp1
            Showln "Retry-After: " sTemp1
        End

        Procedure_Return
    End

    Get ComBodyStr Of hoResp To sTemp1
    Get ComLoad Of hoResult sTemp1 To iSuccess
    If (iSuccess <> True) Begin
        Showln "Expected a JSON response."
        Procedure_Return
    End

    Get ComStringOf Of hoResult "status" To sTemp1
    Showln "Current status: " sTemp1


End_Procedure