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

Constant Contact V3: Add existing contacts to a list

See more Constant Contact Examples

Submit two existing contact IDs and one destination list ID. Constant Contact queues the change as a bulk activity. Save its ID, check the activity state and item errors, and resume checking that ID if the first few polls are inconclusive. Authorize the app first and use the same credential store.

Constant Contact developer guide

Chilkat Delphi DLL Downloads

Delphi DLL
var
success: Boolean;
vault: HCkSecrets;
entry: HCkJsonObject;
tokens: HCkJsonObject;
loaded: Boolean;
http: HCkHttp;
resp: HCkHttpResponse;
body: HCkJsonObject;
job: HCkJsonObject;
activityId: PWideChar;
attempt: Integer;
state: PWideChar;
i: Integer;

begin
success := False;

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
vault := CkSecrets_Create();
CkSecrets_putLocation(vault,'local_manager');
CkSecrets_putAutoSplit(vault,True);
entry := CkJsonObject_Create();
CkJsonObject_UpdateString(entry,'appName','ConstantContactV3');
CkJsonObject_UpdateString(entry,'service','OAuth2');
CkJsonObject_UpdateString(entry,'username','my-account');
tokens := CkJsonObject_Create();
loaded := CkSecrets_GetSecretJson(vault,entry,tokens);
if (loaded <> True) then
  begin
    Memo1.Lines.Add('Run the desktop authorization example using this account and store.');
    Exit;
  end;
if (CkJsonObject_JsonTypeOf(tokens,'access_token') <> 1) then
  begin
    Memo1.Lines.Add('The saved authorization has no access token.');
    Exit;
  end;
http := CkHttp_Create();
CkHttp_putRequireSslCertVerify(http,True);
CkHttp_putAuthToken(http,CkJsonObject__stringOf(tokens,'access_token'));
resp := CkHttpResponse_Create();

//  Use contact IDs from email lookups and the list ID from the selected destination.
body := CkJsonObject_Create();
CkJsonObject_UpdateString(body,'source.contact_ids[0]','FIRST_CONTACT_ID');
CkJsonObject_UpdateString(body,'source.contact_ids[1]','SECOND_CONTACT_ID');
CkJsonObject_UpdateString(body,'list_ids[0]','DESTINATION_LIST_ID');
success := CkHttp_HttpJson(http,'POST','https://api.cc.email/v3/activities/add_list_memberships',body,'application/json',resp);
if ((success <> True) or (CkHttpResponse_getStatusCode(resp) < 200) or (CkHttpResponse_getStatusCode(resp) >= 300)) then
  begin
    Memo1.Lines.Add('Could not start the membership activity. HTTP ' + IntToStr(CkHttpResponse_getStatusCode(resp)));
    Exit;
  end;
job := CkJsonObject_Create();
success := CkJsonObject_Load(job,CkHttpResponse__bodyStr(resp));
if (success <> True) then
  begin
    Memo1.Lines.Add('Expected a JSON response.');
    Exit;
  end;
if ((CkJsonObject_JsonTypeOf(job,'activity_id') <> 1) or (CkJsonObject_StringOfEquals(job,'activity_id','',True) = True)) then
  begin
    Memo1.Lines.Add('Missing activity_id in the response.');
    Exit;
  end;

activityId := CkJsonObject__stringOf(job,'activity_id');
Memo1.Lines.Add('Submitted activity ID: ' + activityId);
CkHttp_SetUrlVar(http,'job',activityId);
//  Poll briefly. A longer-running job can be checked later using the same ID.
attempt := 0;
while (attempt < 5) do
  begin
    success := CkHttp_HttpNoBody(http,'GET','https://api.cc.email/v3/activities/{$job}',resp);
    if ((success <> True) or (CkHttpResponse_getStatusCode(resp) <> 200)) then
      begin
        Memo1.Lines.Add('Status check returned HTTP ' + IntToStr(CkHttpResponse_getStatusCode(resp)) + '. Keep the activity ID and check again later.');
        Exit;
      end;
    success := CkJsonObject_Load(job,CkHttpResponse__bodyStr(resp));
    if ((success <> True) or (CkJsonObject_JsonTypeOf(job,'state') <> 1)) then
      begin
        Memo1.Lines.Add('Missing activity state. Keep the activity ID.');
        Exit;
      end;
    state := CkJsonObject__stringOf(job,'state');
    if ((state <> 'initialized') and (state <> 'processing')) then
      begin
        Memo1.Lines.Add('Activity state: ' + state);
        i := 0;
        while (i < CkJsonObject_SizeOfArray(job,'activity_errors')) do
          begin
            CkJsonObject_putI(job,i);
            Memo1.Lines.Add(CkJsonObject__stringOf(job,'activity_errors[i]'));
            i := i + 1;
          end;

        Exit;
      end;
    attempt := attempt + 1;
    if (attempt < 5) then
      begin
        CkHttp_SleepMs(http,2000);
      end;
  end;

Memo1.Lines.Add('Still processing. Recheck the activity ID before submitting another request.');

CkSecrets_Dispose(vault);
CkJsonObject_Dispose(entry);
CkJsonObject_Dispose(tokens);
CkHttp_Dispose(http);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(body);
CkJsonObject_Dispose(job);