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.
Chilkat C Downloads
#include <C_CkJsonObject.h>
#include <C_CkHttp.h>
#include <C_CkStringBuilder.h>
#include <C_CkHttpResponse.h>
void ChilkatSample(void)
{
BOOL success;
HCkJsonObject config;
const char *apiHost;
HCkHttp http;
HCkStringBuilder url;
HCkHttpResponse resp;
HCkJsonObject result;
const char *contactId;
HCkJsonObject body;
const char *listId;
success = FALSE;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
// Store real credentials in private configuration, never in distributed client code.
config = CkJsonObject_Create();
CkJsonObject_putStrict(config,TRUE);
success = CkJsonObject_LoadFile(config,"sinch-engage.json");
if (success != TRUE) {
printf("Could not load private sinch-engage.json.\n");
CkJsonObject_Dispose(config);
return;
}
if ((CkJsonObject_JsonTypeOf(config,"api_key") != 1) || (CkJsonObject_StringOfEquals(config,"api_key","",TRUE) == TRUE) || (CkJsonObject_JsonTypeOf(config,"api_secret") != 1) || (CkJsonObject_StringOfEquals(config,"api_secret","",TRUE) == TRUE)) {
printf("The private configuration must contain api_key and api_secret.\n");
CkJsonObject_Dispose(config);
return;
}
// Choose the host for your Engage account: au, eu, or us. Do not guess from the recipient.
apiHost = "https://au.app.api.sinch.com";
http = CkHttp_Create();
CkHttp_putRequireSslCertVerify(http,TRUE);
CkHttp_putFollowRedirects(http,FALSE);
CkHttp_putConnectTimeout(http,20);
CkHttp_putReadTimeout(http,30);
CkHttp_putLogin(http,CkJsonObject_stringOf(config,"api_key"));
CkHttp_putPassword(http,CkJsonObject_stringOf(config,"api_secret"));
CkHttp_putBasicAuth(http,TRUE);
CkHttp_SetRequestHeader(http,"Accept","application/json");
url = CkStringBuilder_Create();
resp = CkHttpResponse_Create();
result = CkJsonObject_Create();
CkJsonObject_putStrict(result,TRUE);
contactId = "YOUR_CONTACT_ID";
body = CkJsonObject_Create();
CkJsonObject_UpdateString(body,"name","Example contacts");
CkStringBuilder_Clear(url);
CkStringBuilder_Append(url,apiHost);
CkStringBuilder_Append(url,"/api/v1/contacts/lists");
success = CkHttp_HttpJson(http,"POST",CkStringBuilder_getAsString(url),body,"application/json",resp);
// HttpJson/HttpNoBody also return false for HTTP 4xx/5xx responses.
if ((success != TRUE) && (CkHttpResponse_getStatusCode(resp) == 0)) {
printf("HTTP transport failed. Inspect diagnostics privately; do not blindly retry a write.\n");
CkJsonObject_Dispose(config);
CkHttp_Dispose(http);
CkStringBuilder_Dispose(url);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(result);
CkJsonObject_Dispose(body);
return;
}
if ((CkHttpResponse_getStatusCode(resp) != 201)) {
printf("Unexpected HTTP status: %d\n",CkHttpResponse_getStatusCode(resp));
if (CkHttpResponse_getStatusCode(resp) == 429) {
printf("Retry-After: %s\n",CkHttpResponse_getHeaderField(resp,"Retry-After"));
}
CkJsonObject_Dispose(config);
CkHttp_Dispose(http);
CkStringBuilder_Dispose(url);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(result);
CkJsonObject_Dispose(body);
return;
}
success = CkJsonObject_Load(result,CkHttpResponse_bodyStr(resp));
if (success != TRUE) {
printf("Expected a JSON response.\n");
CkJsonObject_Dispose(config);
CkHttp_Dispose(http);
CkStringBuilder_Dispose(url);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(result);
CkJsonObject_Dispose(body);
return;
}
if ((CkJsonObject_JsonTypeOf(result,"id") != 1) || (CkJsonObject_StringOfEquals(result,"id","",TRUE) == TRUE)) {
printf("The response did not contain the expected ID.\n");
CkJsonObject_Dispose(config);
CkHttp_Dispose(http);
CkStringBuilder_Dispose(url);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(result);
CkJsonObject_Dispose(body);
return;
}
listId = CkJsonObject_stringOf(result,"id");
printf("Created list ID: %s\n",listId);
// Save the list ID before changing membership. A later failure does not undo creation.
CkStringBuilder_Clear(url);
CkStringBuilder_Append(url,apiHost);
CkStringBuilder_Append(url,"/api/v1/contacts/lists/");
CkStringBuilder_Append(url,CkHttp_urlEncode(http,listId));
CkStringBuilder_Append(url,"/contacts/");
CkStringBuilder_Append(url,CkHttp_urlEncode(http,contactId));
success = CkHttp_HttpNoBody(http,"POST",CkStringBuilder_getAsString(url),resp);
// HttpJson/HttpNoBody also return false for HTTP 4xx/5xx responses.
if ((success != TRUE) && (CkHttpResponse_getStatusCode(resp) == 0)) {
printf("HTTP transport failed. Inspect diagnostics privately; do not blindly retry a write.\n");
CkJsonObject_Dispose(config);
CkHttp_Dispose(http);
CkStringBuilder_Dispose(url);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(result);
CkJsonObject_Dispose(body);
return;
}
if ((CkHttpResponse_getStatusCode(resp) != 200) && (CkHttpResponse_getStatusCode(resp) != 201)) {
printf("Unexpected HTTP status: %d\n",CkHttpResponse_getStatusCode(resp));
if (CkHttpResponse_getStatusCode(resp) == 429) {
printf("Retry-After: %s\n",CkHttpResponse_getHeaderField(resp,"Retry-After"));
}
CkJsonObject_Dispose(config);
CkHttp_Dispose(http);
CkStringBuilder_Dispose(url);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(result);
CkJsonObject_Dispose(body);
return;
}
printf("Contact added to the list.\n");
CkJsonObject_Dispose(config);
CkHttp_Dispose(http);
CkStringBuilder_Dispose(url);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(result);
CkJsonObject_Dispose(body);
}