Sample code for 30+ languages & platforms
Dart Requires Chilkat v11.0.0+

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.

Clickatell API reference

Chilkat Dart Downloads

Dart
import 'package:chilkat/chilkat.dart';

void main() {
  var success = false;

  // Assumes Chilkat is unlocked. See Global Unlock Sample.
  final http = CkHttp();
  http.requireSslCertVerify = true;

  // Use the key issued for this HTTP integration. Chilkat encodes each query value.
  final query = 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.');
  final sender = '';
  if (sender != '') {
    query.addParam('from', sender);
  }

  final resp = CkHttpResponse();
  success = true;
  try {
    http.httpReq('https://platform.clickatell.com/messages/http/send', query, 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))) {
    print('Clickatell returned HTTP ${resp.statusCode}');
    if (parsed) {
      if (reply.hasMember('errorDescription')) {
        print(reply.stringOf('errorDescription'));
      }
    }

    return;
  }

  if (!parsed) {
    print('Expected a JSON response.');
    return;
  }

  // A request-level error applies to the submission as a whole.
  if (reply.hasMember('errorCode')) {
    if (!reply.isNullOf('errorCode')) {
      print('Submission error ${reply.stringOf('errorCode')}  ${reply.stringOf('errorDescription')}');
      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].errorCode')}  ${reply.stringOf('messages[i].errorDescription')}');
      }
    }

    i++;
  }
}