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 Rust Downloads
let mut success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
let http = chilkat::Http::new();
http.set_require_ssl_cert_verify(true);
// Supply the key for this One API integration from application configuration.
http.set_request_header("Authorization", "ONE_API_KEY");
http.set_request_header("Accept", "application/json");
let body = chilkat::JsonObject::new();
let _ = body.update_string("messages[0].channel", "sms");
let _ = body.update_string("messages[0].to", "PHONE_NUMBER");
let _ = body.update_string("messages[0].content", "Test message: your order is ready.");
// For a two-way sender, supply the number provisioned on the integration.
let sender = String::new();
if sender != "" {
let _ = body.update_string("messages[0].from", &sender);
}
let resp = chilkat::HttpResponse::new();
success = http.http_json("POST", "https://platform.clickatell.com/v1/message", &body, "application/json", &resp).is_ok();
if resp.status_code() == 0 {
println!("The send outcome is unknown. Check the message report before resending.");
return;
}
let reply = chilkat::JsonObject::new();
let parsed = reply.load(&resp.body_str()).is_ok();
if (!success) || ((resp.status_code() != 202) && (resp.status_code() != 207)) {
println!("Clickatell returned HTTP {}", resp.status_code());
if parsed {
if reply.has_member("error.description") {
println!("{}", reply.string_of("error.description").unwrap_or_default());
}
}
return;
}
if !parsed {
println!("Expected a JSON response.");
return;
}
// A request-level error applies to the submission as a whole.
if reply.has_member("error.code") {
if !reply.is_null_of("error.code") {
println!("Submission error {} {}", reply.string_of("error.code").unwrap_or_default(), reply.string_of("error.description").unwrap_or_default());
return;
}
}
let count = reply.size_of_array("messages");
if count <= 0 {
println!("No recipient results returned. Check the message report before resending.");
return;
}
let mut i = 0;
while i < count {
reply.set_i(i);
let result_number = i + 1;
// JsonTypeOf returns 5 for a JSON boolean.
if reply.json_type_of("messages[i].accepted") != 5 {
println!("Result {} has no acceptance flag.", result_number);
} else {
if reply.bool_of("messages[i].accepted") {
let message_id = reply.string_of("messages[i].apiMessageId").unwrap_or_default();
if (reply.json_type_of("messages[i].apiMessageId") != 1) || (message_id == "") {
println!("Result {} was accepted without a tracking ID.", result_number);
} else {
println!("Result {} accepted. Message ID: {}", result_number, message_id);
}
} else {
println!("Result {} rejected: {} {}", result_number, reply.string_of("messages[i].error.code").unwrap_or_default(), reply.string_of("messages[i].error.description").unwrap_or_default());
}
}
i = i + 1;
}