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
v11.4.0 or greater.
var success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
var http = new CkHttp();
http.RequireSslCertVerify = true;
// Supply the key for this One API integration from application configuration.
http.SetRequestHeader("Authorization","ONE_API_KEY");
http.SetRequestHeader("Accept","application/json");
var body = new CkJsonObject();
body.UpdateString("messages[0].channel","sms");
body.UpdateString("messages[0].to","PHONE_NUMBER");
body.UpdateString("messages[0].content","Test message: your order is ready.");
// For a two-way sender, supply the number provisioned on the integration.
var sender = "";
if (sender !== "") {
body.UpdateString("messages[0].from",sender);
}
var resp = new CkHttpResponse();
success = http.HttpJson("POST","https://platform.clickatell.com/v1/message",body,"application/json",resp);
if (resp.StatusCode == 0) {
console.log("The send outcome is unknown. Check the message report before resending.");
return;
}
var reply = new CkJsonObject();
var parsed = reply.Load(resp.BodyStr);
if ((success !== true) || ((resp.StatusCode !== 202) && (resp.StatusCode !== 207))) {
console.log("Clickatell returned HTTP " + resp.StatusCode);
if (parsed == true) {
if (reply.HasMember("error.description") == true) {
console.log(reply.StringOf("error.description"));
}
}
return;
}
if (parsed !== true) {
console.log("Expected a JSON response.");
return;
}
// A request-level error applies to the submission as a whole.
if (reply.HasMember("error.code") == true) {
if (reply.IsNullOf("error.code") !== true) {
console.log("Submission error " + reply.StringOf("error.code") + " " + reply.StringOf("error.description"));
return;
}
}
var count = reply.SizeOfArray("messages");
if (count <= 0) {
console.log("No recipient results returned. Check the message report before resending.");
return;
}
var i = 0;
while ((i < count)) {
reply.I = i;
var resultNumber = i+1;
// JsonTypeOf returns 5 for a JSON boolean.
if (reply.JsonTypeOf("messages[i].accepted") !== 5) {
console.log("Result " + resultNumber + " has no acceptance flag.");
}
else {
if (reply.BoolOf("messages[i].accepted") == true) {
var messageId = reply.StringOf("messages[i].apiMessageId");
if ((reply.JsonTypeOf("messages[i].apiMessageId") !== 1) || (messageId == "")) {
console.log("Result " + resultNumber + " was accepted without a tracking ID.");
}
else {
console.log("Result " + resultNumber + " accepted. Message ID: " + messageId);
}
}
else {
console.log("Result " + resultNumber + " rejected: " + reply.StringOf("messages[i].error.code") + " " + reply.StringOf("messages[i].error.description"));
}
}
i = i+1;
}