JavaScript Requires Chilkat v11.0.0+
JavaScript
Clickatell HTTP API: Send an SMS with query parameters
See more Clickatell Examples
Use an SMS integration configured for HTTP in the Clickatell Portal. The endpoint takes apiKey, to, and content as query parameters. Pass raw values to Chilkat so it can encode spaces, ampersands, and other reserved characters. Supply a country code and phone number as digits. Set from when your two-way integration requires a sender.
The URL contains the API key, recipient, and message text. Keep full request URLs out of logs and browser history. Check the per-message result after HTTP 202. If the connection is interrupted, check the Portal message report before resending. For the header-authenticated JSON workflow, see Send an SMS with One API.
Note
This example is intended for running within a Chilkat.Js embedded JavaScript engine. All Chilkat JavaScript examples require Chilkat
v11.4.0 or greater.
var success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
var http = new CkHttp();
http.RequireSslCertVerify = true;
// Use the key issued for this HTTP integration. Chilkat encodes each query value.
var query = new CkHttpRequest();
query.HttpVerb = "GET";
query.AddHeader("Accept","application/json");
query.AddParam("apiKey","HTTP_API_KEY");
query.AddParam("to","PHONE_NUMBER");
query.AddParam("content","Test message: pickup between 2 & 4.");
var sender = "";
if (sender !== "") {
query.AddParam("from",sender);
}
var resp = new CkHttpResponse();
success = http.HttpReq("https://platform.clickatell.com/messages/http/send",query,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))) {
console.log("Clickatell returned HTTP " + resp.StatusCode);
if (parsed == true) {
if (reply.HasMember("errorDescription") == true) {
console.log(reply.StringOf("errorDescription"));
}
}
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("errorCode") == true) {
if (reply.IsNullOf("errorCode") !== true) {
console.log("Submission error " + reply.StringOf("errorCode") + " " + reply.StringOf("errorDescription"));
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].errorCode") + " " + reply.StringOf("messages[i].errorDescription"));
}
}
i = i+1;
}