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

Lazarus Pascal
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.Http,
  Chilkat.Secrets,
  Chilkat.StringBuilder,
  Chilkat.HttpResponse,
  Chilkat.JsonObject;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  vault: TSecrets;
  entry: TJsonObject;
  tokens: TJsonObject;
  loaded: Boolean;
  http: THttp;
  resp: THttpResponse;
  url: TStringBuilder;
  page: TJsonObject;
  total: Integer;
  count: Integer;
  i: Integer;

begin
  success := False;

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

  http.SetUrlVar('list','LIST_ID');
  url := TStringBuilder.Create;
  url.Append('https://api.cc.email/v3/contacts?lists={$list}&limit=50');
  page := TJsonObject.Create;
  total := 0;
  while (url.Length > 0) do
    begin
      success := http.HttpNoBody('GET',url.GetAsString(),resp);
      if ((success <> True) or (resp.StatusCode <> 200)) then
        begin
          WriteLn('Expected HTTP 200. Received ' + resp.StatusCode);
          if (resp.StatusCode = 401) then
            begin
              WriteLn('Refresh the saved access token or authorize again.');
            end;
          Exit;
        end;
      success := page.Load(resp.BodyStr);
      count := page.SizeOfArray('contacts');
      if ((success <> True) or (count < 0)) then
        begin
          WriteLn('Expected a page of contacts.');
          Exit;
        end;
      i := 0;
      while (i < count) do
        begin
          page.I := i;
          WriteLn(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?',True) <> True) then
            begin
              WriteLn('Unexpected pagination link. Stopped before following it.');
              Exit;
            end;
          url.Prepend('https://api.cc.email');
        end;
    end;

  WriteLn('List members read: ' + total);


  vault.Free;
  entry.Free;
  tokens.Free;
  http.Free;
  resp.Free;
  url.Free;
  page.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.