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 Dart Downloads
import 'package:chilkat/chilkat.dart';
void main() {
var success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
final http = 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');
final body = 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.
final sender = '';
if (sender != '') {
body.updateString('messages[0].from', sender);
}
final resp = CkHttpResponse();
success = true;
try {
http.httpJson('POST', 'https://platform.clickatell.com/v1/message', body, 'application/json', resp);
} on ChilkatException {
success = false;
}
if (resp.statusCode == 0) {
print('The send outcome is unknown. Check the message report before resending.');
return;
}
final reply = CkJsonObject();
var parsed = true;
try {
reply.load(resp.bodyStr);
} on ChilkatException {
parsed = false;
}
if ((!success) || ((resp.statusCode != 202) && (resp.statusCode != 207))) {
print('Clickatell returned HTTP ${resp.statusCode}');
if (parsed) {
if (reply.hasMember('error.description')) {
print(reply.stringOf('error.description'));
}
}
return;
}
if (!parsed) {
print('Expected a JSON response.');
return;
}
// A request-level error applies to the submission as a whole.
if (reply.hasMember('error.code')) {
if (!reply.isNullOf('error.code')) {
print('Submission error ${reply.stringOf('error.code')} ${reply.stringOf('error.description')}');
return;
}
}
final count = reply.sizeOfArray('messages');
if (count <= 0) {
print('No recipient results returned. Check the message report before resending.');
return;
}
var i = 0;
while (i < count) {
reply.i = i;
final resultNumber = i + 1;
// JsonTypeOf returns 5 for a JSON boolean.
if (reply.jsonTypeOf('messages[i].accepted') != 5) {
print('Result $resultNumber has no acceptance flag.');
} else {
if (reply.boolOf('messages[i].accepted')) {
final messageId = reply.stringOf('messages[i].apiMessageId');
if ((reply.jsonTypeOf('messages[i].apiMessageId') != 1) || (messageId == '')) {
print('Result $resultNumber was accepted without a tracking ID.');
} else {
print('Result $resultNumber accepted. Message ID: $messageId');
}
} else {
print('Result $resultNumber rejected: ${reply.stringOf('messages[i].error.code')} ${reply.stringOf('messages[i].error.description')}');
}
}
i++;
}
}