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

Constant Contact V3: Create a contact

See more Constant Contact Examples

Create an account-entered contact and read its new ID. Use the email permission that accurately describes the contact’s relationship with the account. The optional postal address belongs in the street_addresses array. On a duplicate, look up the existing contact before deciding whether an update is appropriate. Authorize the app first and use the same 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();

  final body = CkJsonObject();
  body.updateString('create_source', 'Account');
  body.updateString('first_name', 'Alex');
  body.updateString('last_name', 'Rivera');
  body.updateString('email_address.address', 'reader+newsletter@example.com');
  // This sample assumes the account has recorded a qualifying existing relationship.
  body.updateString('email_address.permission_to_send', 'implicit');
  body.updateString('street_addresses[0].kind', 'work');
  body.updateString('street_addresses[0].street', '100 Main Street');
  body.updateString('street_addresses[0].city', 'Boston');
  body.updateString('street_addresses[0].state', 'MA');
  body.updateString('street_addresses[0].country', 'United States');
  success = true;
  try {
    http.httpJson('POST', 'https://api.cc.email/v3/contacts', body, 'application/json', resp);
  } on ChilkatException {
    success = false;
  }
  if (resp.statusCode == 409) {
    print('Contact already exists. Look up the email address to obtain its ID.');
    return;
  }

  if ((!success) || (resp.statusCode != 201)) {
    print('Expected HTTP 201. Received ${resp.statusCode}');
    if (resp.statusCode == 401) {
      print('Refresh the saved access token or authorize again.');
    }

    return;
  }

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

  if ((created.jsonTypeOf('contact_id') != 1) || (created.stringOfEquals('contact_id', '', true))) {
    print('Missing contact_id in the response.');
    return;
  }

  print('Created contact ID: ${created.stringOf('contact_id')}');
}