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

Delphi DLL
var
success: Boolean;
vault: HCkSecrets;
entry: HCkJsonObject;
tokens: HCkJsonObject;
loaded: Boolean;
http: HCkHttp;
resp: HCkHttpResponse;
url: HCkStringBuilder;
page: HCkJsonObject;
total: Integer;
count: Integer;
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();

CkHttp_SetUrlVar(http,'list','LIST_ID');
url := CkStringBuilder_Create();
CkStringBuilder_Append(url,'https://api.cc.email/v3/contacts?lists={$list}&limit=50');
page := CkJsonObject_Create();
total := 0;
while (CkStringBuilder_getLength(url) > 0) do
  begin
    success := CkHttp_HttpNoBody(http,'GET',CkStringBuilder__getAsString(url),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;
    success := CkJsonObject_Load(page,CkHttpResponse__bodyStr(resp));
    count := CkJsonObject_SizeOfArray(page,'contacts');
    if ((success <> True) or (count < 0)) then
      begin
        Memo1.Lines.Add('Expected a page of contacts.');
        Exit;
      end;
    i := 0;
    while (i < count) do
      begin
        CkJsonObject_putI(page,i);
        Memo1.Lines.Add(CkJsonObject__stringOf(page,'contacts[i].contact_id') + '  ' + CkJsonObject__stringOf(page,'contacts[i].email_address.address'));
        i := i + 1;
      end;

    total := total + count;
    CkStringBuilder_Clear(url);
    if (CkJsonObject_JsonTypeOf(page,'_links.next.href') = 1) then
      begin
        CkStringBuilder_Append(url,CkJsonObject__stringOf(page,'_links.next.href'));
        //  The documented cursor link is relative to api.cc.email.
        if (CkStringBuilder_StartsWith(url,'/v3/contacts?',True) <> True) then
          begin
            Memo1.Lines.Add('Unexpected pagination link. Stopped before following it.');
            Exit;
          end;
        CkStringBuilder_Prepend(url,'https://api.cc.email');
      end;
  end;

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

CkSecrets_Dispose(vault);
CkJsonObject_Dispose(entry);
CkJsonObject_Dispose(tokens);
CkHttp_Dispose(http);
CkHttpResponse_Dispose(resp);
CkStringBuilder_Dispose(url);
CkJsonObject_Dispose(page);