Sample code for 30+ languages & platforms
Delphi ActiveX 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 ActiveX Downloads

Delphi ActiveX
var
success: Integer;
vault: TChilkatSecrets;
entry: TChilkatJsonObject;
tokens: TChilkatJsonObject;
loaded: Integer;
http: TChilkatHttp;
resp: TChilkatHttpResponse;
query: TChilkatHttpRequest;
matches: TChilkatJsonObject;
body: TChilkatJsonObject;
job: TChilkatJsonObject;
activityId: WideString;
attempt: Integer;
state: WideString;
i: Integer;

begin
success := 0;

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
vault := TChilkatSecrets.Create(Self);
vault.Location := 'local_manager';
vault.AutoSplit := 1;
entry := TChilkatJsonObject.Create(Self);
entry.UpdateString('appName','ConstantContactV3');
entry.UpdateString('service','OAuth2');
entry.UpdateString('username','my-account');
tokens := TChilkatJsonObject.Create(Self);
loaded := vault.GetSecretJson(entry.ControlInterface,tokens.ControlInterface);
if (loaded <> 1) then
  begin
    Memo1.Lines.Add('Run the desktop authorization example using this account and store.');
    Exit;
  end;
if (tokens.JsonTypeOf('access_token') <> 1) then
  begin
    Memo1.Lines.Add('The saved authorization has no access token.');
    Exit;
  end;
http := TChilkatHttp.Create(Self);
http.RequireSslCertVerify := 1;
http.AuthToken := tokens.StringOf('access_token');
resp := TChilkatHttpResponse.Create(Self);

query := TChilkatHttpRequest.Create(Self);
query.HttpVerb := 'GET';
query.AddParam('email','reader+newsletter@example.com');
query.AddParam('status','deleted');
success := http.HttpReq('https://api.cc.email/v3/contacts',query.ControlInterface,resp.ControlInterface);
if ((success <> 1) or (resp.StatusCode <> 200)) then
  begin
    Memo1.Lines.Add('Expected HTTP 200. Received ' + IntToStr(resp.StatusCode));
    if (resp.StatusCode = 401) then
      begin
        Memo1.Lines.Add('Refresh the saved access token or authorize again.');
      end;
    Exit;
  end;
matches := TChilkatJsonObject.Create(Self);
success := matches.Load(resp.BodyStr);
if (success <> 1) then
  begin
    Memo1.Lines.Add('Expected a JSON response.');
    Exit;
  end;
if ((matches.SizeOfArray('contacts') <> 1) or (matches.JsonTypeOf('contacts[0].deleted_at') <> 1)) then
  begin
    Memo1.Lines.Add('Expected one deleted contact for this email. No restoration submitted.');
    Exit;
  end;
if ((matches.JsonTypeOf('contacts[0].contact_id') <> 1) or (matches.StringOfEquals('contacts[0].contact_id','',1) = 1)) then
  begin
    Memo1.Lines.Add('Missing contacts[0].contact_id in the response.');
    Exit;
  end;
body := TChilkatJsonObject.Create(Self);
body.UpdateString('source.contact_ids[0]',matches.StringOf('contacts[0].contact_id'));
body.UpdateString('list_ids[0]','DESTINATION_LIST_ID');
success := http.HttpJson('POST','https://api.cc.email/v3/activities/add_list_memberships',body.ControlInterface,'application/json',resp.ControlInterface);
if ((success <> 1) or (resp.StatusCode < 200) or (resp.StatusCode >= 300)) then
  begin
    Memo1.Lines.Add('Could not start the membership activity. HTTP ' + IntToStr(resp.StatusCode));
    Exit;
  end;
job := TChilkatJsonObject.Create(Self);
success := job.Load(resp.BodyStr);
if (success <> 1) then
  begin
    Memo1.Lines.Add('Expected a JSON response.');
    Exit;
  end;
if ((job.JsonTypeOf('activity_id') <> 1) or (job.StringOfEquals('activity_id','',1) = 1)) then
  begin
    Memo1.Lines.Add('Missing activity_id in the response.');
    Exit;
  end;

activityId := job.StringOf('activity_id');
Memo1.Lines.Add('Submitted activity ID: ' + activityId);
http.SetUrlVar('job',activityId);
//  Poll briefly. A longer-running job can be checked later using the same ID.
attempt := 0;
while (attempt < 5) do
  begin
    success := http.HttpNoBody('GET','https://api.cc.email/v3/activities/{$job}',resp.ControlInterface);
    if ((success <> 1) or (resp.StatusCode <> 200)) then
      begin
        Memo1.Lines.Add('Status check returned HTTP ' + IntToStr(resp.StatusCode) + '. Keep the activity ID and check again later.');
        Exit;
      end;
    success := job.Load(resp.BodyStr);
    if ((success <> 1) or (job.JsonTypeOf('state') <> 1)) then
      begin
        Memo1.Lines.Add('Missing activity state. Keep the activity ID.');
        Exit;
      end;
    state := job.StringOf('state');
    if ((state <> 'initialized') and (state <> 'processing')) then
      begin
        Memo1.Lines.Add('Activity state: ' + state);
        i := 0;
        while (i < job.SizeOfArray('activity_errors')) do
          begin
            job.I := i;
            Memo1.Lines.Add(job.StringOf('activity_errors[i]'));
            i := i + 1;
          end;

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

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