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

Constant Contact V3: Restore a deleted contact to a list

See more Constant Contact Examples

Look up the deleted contact by email, then restore it by adding its existing ID to a chosen list. Constant Contact documents this membership activity as a restoration method. It changes only membership, preserves the saved profile, and makes no email-permission claim. Select the intended destination list explicitly; previous memberships were removed by deletion. 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;
query: HCkHttpRequest;
matches: HCkJsonObject;
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();

query := CkHttpRequest_Create();
CkHttpRequest_putHttpVerb(query,'GET');
CkHttpRequest_AddParam(query,'email','reader+newsletter@example.com');
CkHttpRequest_AddParam(query,'status','deleted');
success := CkHttp_HttpReq(http,'https://api.cc.email/v3/contacts',query,resp);
if ((success <> True) or (CkHttpResponse_getStatusCode(resp) <> 200)) then
  begin
    Memo1.Lines.Add('Expected HTTP 200. Received ' + IntToStr(CkHttpResponse_getStatusCode(resp)));
    if (CkHttpResponse_getStatusCode(resp) = 401) then
      begin
        Memo1.Lines.Add('Refresh the saved access token or authorize again.');
      end;
    Exit;
  end;
matches := CkJsonObject_Create();
success := CkJsonObject_Load(matches,CkHttpResponse__bodyStr(resp));
if (success <> True) then
  begin
    Memo1.Lines.Add('Expected a JSON response.');
    Exit;
  end;
if ((CkJsonObject_SizeOfArray(matches,'contacts') <> 1) or (CkJsonObject_JsonTypeOf(matches,'contacts[0].deleted_at') <> 1)) then
  begin
    Memo1.Lines.Add('Expected one deleted contact for this email. No restoration submitted.');
    Exit;
  end;
if ((CkJsonObject_JsonTypeOf(matches,'contacts[0].contact_id') <> 1) or (CkJsonObject_StringOfEquals(matches,'contacts[0].contact_id','',True) = True)) then
  begin
    Memo1.Lines.Add('Missing contacts[0].contact_id in the response.');
    Exit;
  end;
body := CkJsonObject_Create();
CkJsonObject_UpdateString(body,'source.contact_ids[0]',CkJsonObject__stringOf(matches,'contacts[0].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);
CkHttpRequest_Dispose(query);
CkJsonObject_Dispose(matches);
CkJsonObject_Dispose(body);
CkJsonObject_Dispose(job);