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

Clickatell One API: Read the account balance and currency

See more Clickatell Examples

Request the messaging balance associated with a One API integration key. The response supplies both the decimal amount and its currency. Read the amount as text for display to preserve its precision. A zero balance is a valid result. For example, a billing dashboard can display 1267.92865 ZAR.

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;
  // Supply the key for this One API integration from application configuration.
  http.setRequestHeader('Authorization', 'ONE_API_KEY');
  http.setRequestHeader('Accept', 'application/json');

  final resp = CkHttpResponse();
  success = true;
  try {
    http.httpNoBody('GET', 'https://platform.clickatell.com/v1/balance', resp);
  } on ChilkatException {
    success = false;
  }
  if (resp.statusCode == 0) {
    print('No HTTP response. Check the connection and retry the lookup.');
    return;
  }

  final reply = CkJsonObject();
  var parsed = true;
  try {
    reply.load(resp.bodyStr);
  } on ChilkatException {
    parsed = false;
  }
  if ((!success) || ((resp.statusCode != 200))) {
    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;
  }

  // JsonTypeOf returns 2 for a number and 1 for a string. Zero is a valid amount.
  if ((reply.jsonTypeOf('balance') != 2) || (reply.jsonTypeOf('currency') != 1)) {
    print('The balance response is missing the amount or currency.');
    return;
  }

  print('Account balance: ${reply.stringOf('balance')} ${reply.stringOf('currency')}');
}