Clickatell One API: Send an SMS and get its message ID
See more Clickatell Examples
Send one text using an SMS-enabled One API integration. Supply an international phone number starting with the country code, using digits only. For a US two-way integration, set from to its provisioned two-way number. With a test integration, use a registered test phone.
HTTP 202 means the submission was processed. Inspect accepted for each message. Save the returned apiMessageId with the application record and use it to look up delivery status. When a multi-message submission returns HTTP 207, check each result before retrying any rejected message.
Already using the SMS-only REST integration? Its documented request is POST /messages with the integration key in Authorization and a body such as {"content":"Test message","to":["PHONE_NUMBER"]}. One API has a different request body and a separate integration key. SMS API reference
Chilkat Unicode C Downloads
#include <C_CkHttpW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkHttpResponseW.h>
void ChilkatSample(void)
{
BOOL success;
HCkHttpW http;
HCkJsonObjectW body;
const wchar_t *sender;
HCkHttpResponseW resp;
HCkJsonObjectW reply;
BOOL parsed;
int count;
int i;
int resultNumber;
const wchar_t *messageId;
success = FALSE;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
http = CkHttpW_Create();
CkHttpW_putRequireSslCertVerify(http,TRUE);
// Supply the key for this One API integration from application configuration.
CkHttpW_SetRequestHeader(http,L"Authorization",L"ONE_API_KEY");
CkHttpW_SetRequestHeader(http,L"Accept",L"application/json");
body = CkJsonObjectW_Create();
CkJsonObjectW_UpdateString(body,L"messages[0].channel",L"sms");
CkJsonObjectW_UpdateString(body,L"messages[0].to",L"PHONE_NUMBER");
CkJsonObjectW_UpdateString(body,L"messages[0].content",L"Test message: your order is ready.");
// For a two-way sender, supply the number provisioned on the integration.
sender = L"";
if (sender != L"") {
CkJsonObjectW_UpdateString(body,L"messages[0].from",sender);
}
resp = CkHttpResponseW_Create();
success = CkHttpW_HttpJson(http,L"POST",L"https://platform.clickatell.com/v1/message",body,L"application/json",resp);
if (CkHttpResponseW_getStatusCode(resp) == 0) {
wprintf(L"The send outcome is unknown. Check the message report before resending.\n");
CkHttpW_Dispose(http);
CkJsonObjectW_Dispose(body);
CkHttpResponseW_Dispose(resp);
return;
}
reply = CkJsonObjectW_Create();
parsed = CkJsonObjectW_Load(reply,CkHttpResponseW_bodyStr(resp));
if ((success != TRUE) || ((CkHttpResponseW_getStatusCode(resp) != 202) && (CkHttpResponseW_getStatusCode(resp) != 207))) {
wprintf(L"Clickatell returned HTTP %d\n",CkHttpResponseW_getStatusCode(resp));
if (parsed == TRUE) {
if (CkJsonObjectW_HasMember(reply,L"error.description") == TRUE) {
wprintf(L"%s\n",CkJsonObjectW_stringOf(reply,L"error.description"));
}
}
CkHttpW_Dispose(http);
CkJsonObjectW_Dispose(body);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(reply);
return;
}
if (parsed != TRUE) {
wprintf(L"Expected a JSON response.\n");
CkHttpW_Dispose(http);
CkJsonObjectW_Dispose(body);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(reply);
return;
}
// A request-level error applies to the submission as a whole.
if (CkJsonObjectW_HasMember(reply,L"error.code") == TRUE) {
if (CkJsonObjectW_IsNullOf(reply,L"error.code") != TRUE) {
wprintf(L"Submission error %s %s\n",CkJsonObjectW_stringOf(reply,L"error.code"),CkJsonObjectW_stringOf(reply,L"error.description"));
CkHttpW_Dispose(http);
CkJsonObjectW_Dispose(body);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(reply);
return;
}
}
count = CkJsonObjectW_SizeOfArray(reply,L"messages");
if (count <= 0) {
wprintf(L"No recipient results returned. Check the message report before resending.\n");
CkHttpW_Dispose(http);
CkJsonObjectW_Dispose(body);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(reply);
return;
}
i = 0;
while ((i < count)) {
CkJsonObjectW_putI(reply,i);
resultNumber = i + 1;
// JsonTypeOf returns 5 for a JSON boolean.
if (CkJsonObjectW_JsonTypeOf(reply,L"messages[i].accepted") != 5) {
wprintf(L"Result %d has no acceptance flag.\n",resultNumber);
}
else {
if (CkJsonObjectW_BoolOf(reply,L"messages[i].accepted") == TRUE) {
messageId = CkJsonObjectW_stringOf(reply,L"messages[i].apiMessageId");
if ((CkJsonObjectW_JsonTypeOf(reply,L"messages[i].apiMessageId") != 1) || (messageId == L"")) {
wprintf(L"Result %d was accepted without a tracking ID.\n",resultNumber);
}
else {
wprintf(L"Result %d accepted. Message ID: %s\n",resultNumber,messageId);
}
}
else {
wprintf(L"Result %d rejected: %s %s\n",resultNumber,CkJsonObjectW_stringOf(reply,L"messages[i].error.code"),CkJsonObjectW_stringOf(reply,L"messages[i].error.description"));
}
}
i = i + 1;
}
CkHttpW_Dispose(http);
CkJsonObjectW_Dispose(body);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(reply);
}