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

SendGrid: Add or update list contacts and check the import job

See more SendGrid Examples

Use a Marketing Campaigns API key with contact access and the ID of an existing list. A 202 response queues the upsert; it does not confirm that the contacts were saved. Save the job ID and use it to recheck a pending import without submitting the contacts again. The error report may contain contact data; handle it privately.

Add or update contacts ยท Import job status

Chilkat Dart Downloads

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

void main() {
  // Assumes Chilkat is unlocked. See Global Unlock Sample.
  final http = CkHttp();
  http.requireSslCertVerify = true;
  // Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
  http.authToken = 'SENDGRID_API_KEY';
  final resp = CkHttpResponse();

  var jobId = '';
  if (jobId == '') {
    final contacts = CkJsonObject();
    contacts.updateString('list_ids[0]', 'YOUR_LIST_UUID');
    contacts.updateString('contacts[0].email', 'avery@example.com');
    contacts.updateString('contacts[0].first_name', 'Avery');
    contacts.updateString('contacts[1].email', 'robin@example.com');
    contacts.updateString('contacts[1].first_name', 'Robin');
    try {
      http.httpJson('PUT', 'https://api.sendgrid.com/v3/marketing/contacts', contacts, 'application/json', resp);
    } on ChilkatException catch (e) {
      if (resp.statusCode == 0) {
        print(e.lastErrorText);
      } else {
        print('HTTP ${resp.statusCode}: ${resp.bodyStr}');
      }

      return;
    }

    if (resp.statusCode != 202) {
      print('HTTP ${resp.statusCode}: ${resp.bodyStr}');
      return;
    }

    final queued = CkJsonObject();
    try {
      queued.load(resp.bodyStr);
    } on ChilkatException {
      print('Unexpected response: expected JSON.');
      return;
    }

    if (!queued.hasMember('job_id')) {
      print('No job ID returned.');
      return;
    }

    jobId = queued.stringOf('job_id');
    print('Save this import job ID: $jobId');
  }

  http.setUrlVar('job', jobId);

  try {
    http.httpNoBody('GET', 'https://api.sendgrid.com/v3/marketing/contacts/imports/{\$job}', resp);
  } on ChilkatException catch (e) {
    if (resp.statusCode == 0) {
      print(e.lastErrorText);
    } else {
      print('HTTP ${resp.statusCode}: ${resp.bodyStr}');
    }

    return;
  }

  if (resp.statusCode != 200) {
    print('HTTP ${resp.statusCode}: ${resp.bodyStr}');
    return;
  }

  final job = CkJsonObject();
  try {
    job.load(resp.bodyStr);
  } on ChilkatException {
    print('Unexpected response: expected JSON.');
    return;
  }

  final state = job.stringOf('status');
  if (state == 'pending') {
    print('Still pending. Check again later with jobId = $jobId');
    return;
  }

  print('Job status: $state');
  print('Created: ${job.intOf('results.created_count')}, updated: ${job.intOf('results.updated_count')}, errors: ${job.intOf('results.errored_count')}');
  if ((state == 'errored') || (state == 'failed')) {
    print('Review the job before retrying. If present, results.errors_url identifies the private error report.');
  }
}