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

Sinch Engage: Create a List and Add an Existing Contact

See more Sinch Engage Examples

Create a list, read its returned ID, and add an existing contact. These are two independent requests, not a transaction. If adding the contact fails, keep the list ID and resume membership rather than creating another list. List membership does not opt a contact in or send a message.

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 PureBasic Downloads

PureBasic
IncludeFile "CkHttpResponse.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkJsonObject.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  Assumes Chilkat is unlocked. See Global Unlock Sample.

    ;  Store real credentials in private configuration, never in distributed client code.
    config.i = CkJsonObject::ckCreate()
    If config.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::setCkStrict(config, 1)
    success = CkJsonObject::ckLoadFile(config,"sinch-engage.json")
    If success <> 1
        Debug "Could not load private sinch-engage.json."
        CkJsonObject::ckDispose(config)
        ProcedureReturn
    EndIf

    If (CkJsonObject::ckJsonTypeOf(config,"api_key") <> 1) OR (CkJsonObject::ckStringOfEquals(config,"api_key","",1) = 1) OR (CkJsonObject::ckJsonTypeOf(config,"api_secret") <> 1) OR (CkJsonObject::ckStringOfEquals(config,"api_secret","",1) = 1)
        Debug "The private configuration must contain api_key and api_secret."
        CkJsonObject::ckDispose(config)
        ProcedureReturn
    EndIf

    ;  Choose the host for your Engage account: au, eu, or us. Do not guess from the recipient.
    apiHost.s = "https://au.app.api.sinch.com"
    http.i = CkHttp::ckCreate()
    If http.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkHttp::setCkRequireSslCertVerify(http, 1)
    CkHttp::setCkFollowRedirects(http, 0)
    CkHttp::setCkConnectTimeout(http, 20)
    CkHttp::setCkReadTimeout(http, 30)
    CkHttp::setCkLogin(http, CkJsonObject::ckStringOf(config,"api_key"))
    CkHttp::setCkPassword(http, CkJsonObject::ckStringOf(config,"api_secret"))
    CkHttp::setCkBasicAuth(http, 1)
    CkHttp::ckSetRequestHeader(http,"Accept","application/json")
    url.i = CkStringBuilder::ckCreate()
    If url.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    resp.i = CkHttpResponse::ckCreate()
    If resp.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    result.i = CkJsonObject::ckCreate()
    If result.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::setCkStrict(result, 1)
    contactId.s = "YOUR_CONTACT_ID"
    body.i = CkJsonObject::ckCreate()
    If body.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckUpdateString(body,"name","Example contacts")
    CkStringBuilder::ckClear(url)
    CkStringBuilder::ckAppend(url,apiHost)
    CkStringBuilder::ckAppend(url,"/api/v1/contacts/lists")
    success = CkHttp::ckHttpJson(http,"POST",CkStringBuilder::ckGetAsString(url),body,"application/json",resp)
    ;  HttpJson/HttpNoBody also return false for HTTP 4xx/5xx responses.
    If (success <> 1) AND (CkHttpResponse::ckStatusCode(resp) = 0)
        Debug "HTTP transport failed. Inspect diagnostics privately; do not blindly retry a write."
        CkJsonObject::ckDispose(config)
        CkHttp::ckDispose(http)
        CkStringBuilder::ckDispose(url)
        CkHttpResponse::ckDispose(resp)
        CkJsonObject::ckDispose(result)
        CkJsonObject::ckDispose(body)
        ProcedureReturn
    EndIf

    If (CkHttpResponse::ckStatusCode(resp) <> 201)
        Debug "Unexpected HTTP status: " + Str(CkHttpResponse::ckStatusCode(resp))
        If CkHttpResponse::ckStatusCode(resp) = 429
            Debug "Retry-After: " + CkHttpResponse::ckGetHeaderField(resp,"Retry-After")
        EndIf

        CkJsonObject::ckDispose(config)
        CkHttp::ckDispose(http)
        CkStringBuilder::ckDispose(url)
        CkHttpResponse::ckDispose(resp)
        CkJsonObject::ckDispose(result)
        CkJsonObject::ckDispose(body)
        ProcedureReturn
    EndIf

    success = CkJsonObject::ckLoad(result,CkHttpResponse::ckBodyStr(resp))
    If success <> 1
        Debug "Expected a JSON response."
        CkJsonObject::ckDispose(config)
        CkHttp::ckDispose(http)
        CkStringBuilder::ckDispose(url)
        CkHttpResponse::ckDispose(resp)
        CkJsonObject::ckDispose(result)
        CkJsonObject::ckDispose(body)
        ProcedureReturn
    EndIf

    If (CkJsonObject::ckJsonTypeOf(result,"id") <> 1) OR (CkJsonObject::ckStringOfEquals(result,"id","",1) = 1)
        Debug "The response did not contain the expected ID."
        CkJsonObject::ckDispose(config)
        CkHttp::ckDispose(http)
        CkStringBuilder::ckDispose(url)
        CkHttpResponse::ckDispose(resp)
        CkJsonObject::ckDispose(result)
        CkJsonObject::ckDispose(body)
        ProcedureReturn
    EndIf

    listId.s = CkJsonObject::ckStringOf(result,"id")
    Debug "Created list ID: " + listId
    ;  Save the list ID before changing membership. A later failure does not undo creation.
    CkStringBuilder::ckClear(url)
    CkStringBuilder::ckAppend(url,apiHost)
    CkStringBuilder::ckAppend(url,"/api/v1/contacts/lists/")
    CkStringBuilder::ckAppend(url,CkHttp::ckUrlEncode(http,listId))
    CkStringBuilder::ckAppend(url,"/contacts/")
    CkStringBuilder::ckAppend(url,CkHttp::ckUrlEncode(http,contactId))
    success = CkHttp::ckHttpNoBody(http,"POST",CkStringBuilder::ckGetAsString(url),resp)
    ;  HttpJson/HttpNoBody also return false for HTTP 4xx/5xx responses.
    If (success <> 1) AND (CkHttpResponse::ckStatusCode(resp) = 0)
        Debug "HTTP transport failed. Inspect diagnostics privately; do not blindly retry a write."
        CkJsonObject::ckDispose(config)
        CkHttp::ckDispose(http)
        CkStringBuilder::ckDispose(url)
        CkHttpResponse::ckDispose(resp)
        CkJsonObject::ckDispose(result)
        CkJsonObject::ckDispose(body)
        ProcedureReturn
    EndIf

    If (CkHttpResponse::ckStatusCode(resp) <> 200) AND (CkHttpResponse::ckStatusCode(resp) <> 201)
        Debug "Unexpected HTTP status: " + Str(CkHttpResponse::ckStatusCode(resp))
        If CkHttpResponse::ckStatusCode(resp) = 429
            Debug "Retry-After: " + CkHttpResponse::ckGetHeaderField(resp,"Retry-After")
        EndIf

        CkJsonObject::ckDispose(config)
        CkHttp::ckDispose(http)
        CkStringBuilder::ckDispose(url)
        CkHttpResponse::ckDispose(resp)
        CkJsonObject::ckDispose(result)
        CkJsonObject::ckDispose(body)
        ProcedureReturn
    EndIf

    Debug "Contact added to the list."


    CkJsonObject::ckDispose(config)
    CkHttp::ckDispose(http)
    CkStringBuilder::ckDispose(url)
    CkHttpResponse::ckDispose(resp)
    CkJsonObject::ckDispose(result)
    CkJsonObject::ckDispose(body)


    ProcedureReturn
EndProcedure