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

Constant Contact V3: Find a contact list by name

See more Constant Contact Examples

Start with the full list name from the Constant Contact account. Read its list_id and the number of active, mailable contacts. Use the ID for list membership operations or campaign targeting. This lookup requires contact_data.

Authorize the application using the same account and credential store.

Constant Contact developer guide

Chilkat Dart Downloads

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

void main() {
  var success = false;

  // Assumes Chilkat is unlocked. See Global Unlock Sample.
  final vault = CkSecrets();
  vault.location = 'local_manager';
  vault.autoSplit = true;
  final entry = CkJsonObject();
  entry.updateString('appName', 'ConstantContactV3');
  entry.updateString('service', 'OAuth2');
  entry.updateString('username', 'my-account');
  final tokens = CkJsonObject();
  try {
    vault.getSecretJson(entry, tokens);
  } on ChilkatException {
    print('Run the desktop authorization example using this account and store.');
    return;
  }

  if (tokens.jsonTypeOf('access_token') != 1) {
    print('The saved authorization has no access token.');
    return;
  }

  final http = CkHttp();
  http.requireSslCertVerify = true;
  http.authToken = tokens.stringOf('access_token');
  final resp = CkHttpResponse();

  // Use the full display name. AddParam encodes spaces and punctuation.
  final listName = 'Workshop announcements';
  final query = CkHttpRequest();
  query.httpVerb = 'GET';
  query.addParam('name', listName);
  query.addParam('status', 'active');
  query.addParam('include_membership_count', 'active');
  success = true;
  try {
    http.httpReq('https://api.cc.email/v3/contact_lists', query, resp);
  } on ChilkatException {
    success = false;
  }
  if ((!success) && (resp.statusCode <= 0)) {
    print('Look up list: HTTP request failed.');
    return;
  }

  if ((!success) || (resp.statusCode != 200)) {
    print('Look up list: HTTP ${resp.statusCode} ${resp.bodyStr}');
    return;
  }

  final matches = CkJsonObject();
  try {
    matches.load(resp.bodyStr);
  } on ChilkatException {
    print('Expected a JSON object in the response.');
    return;
  }

  if (matches.sizeOfArray('lists') == 0) {
    print('No active email list named $listName');
    return;
  }

  if ((matches.sizeOfArray('lists') != 1) || (!matches.stringOfEquals('lists[0].name', listName, true))) {
    print('Expected one exact list name.');
    return;
  }

  final listId = matches.stringOf('lists[0].list_id');
  if (listId == '') {
    print('Missing list ID.');
    return;
  }

  print('$listName | List ID: $listId | Mailable contacts: ${matches.intOf('lists[0].membership_count')}');
}