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

Constant Contact V3: Read every page of list members

See more Constant Contact Examples

Filter the contacts collection with lists and a list ID. To query multiple lists together, supply their comma-separated IDs. Follow the returned cursor links and report the total after the last page. The default contacts query excludes deleted records. A list member can still have unsubscribed email permission. 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;
url: TChilkatStringBuilder;
page: TChilkatJsonObject;
total: Integer;
count: Integer;
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);

http.SetUrlVar('list','LIST_ID');
url := TChilkatStringBuilder.Create(Self);
url.Append('https://api.cc.email/v3/contacts?lists={$list}&limit=50');
page := TChilkatJsonObject.Create(Self);
total := 0;
while (url.Length > 0) do
  begin
    success := http.HttpNoBody('GET',url.GetAsString(),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;
    success := page.Load(resp.BodyStr);
    count := page.SizeOfArray('contacts');
    if ((success <> 1) or (count < 0)) then
      begin
        Memo1.Lines.Add('Expected a page of contacts.');
        Exit;
      end;
    i := 0;
    while (i < count) do
      begin
        page.I := i;
        Memo1.Lines.Add(page.StringOf('contacts[i].contact_id') + '  ' + page.StringOf('contacts[i].email_address.address'));
        i := i + 1;
      end;

    total := total + count;
    url.Clear();
    if (page.JsonTypeOf('_links.next.href') = 1) then
      begin
        url.Append(page.StringOf('_links.next.href'));
        //  The documented cursor link is relative to api.cc.email.
        if (url.StartsWith('/v3/contacts?',1) <> 1) then
          begin
            Memo1.Lines.Add('Unexpected pagination link. Stopped before following it.');
            Exit;
          end;
        url.Prepend('https://api.cc.email');
      end;
  end;

Memo1.Lines.Add('List members read: ' + IntToStr(total));