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

Constant Contact V3: Find a contact list by name

See more Constant Contact Examples

Start with the full list name from the Constant Contact account. Read its list_id and the number of active, mailable contacts. Use the ID for list membership operations or campaign targeting. This lookup requires contact_data.

Authorize the application using the same account and 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;
listName: PWideChar;
query: HCkHttpRequest;
matches: HCkJsonObject;
listId: PWideChar;

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();

//  Use the full display name. AddParam encodes spaces and punctuation.
listName := 'Workshop announcements';
query := CkHttpRequest_Create();
CkHttpRequest_putHttpVerb(query,'GET');
CkHttpRequest_AddParam(query,'name',listName);
CkHttpRequest_AddParam(query,'status','active');
CkHttpRequest_AddParam(query,'include_membership_count','active');
success := CkHttp_HttpReq(http,'https://api.cc.email/v3/contact_lists',query,resp);
if ((success <> True) and (CkHttpResponse_getStatusCode(resp) <= 0)) then
  begin
    Memo1.Lines.Add('Look up list: HTTP request failed.');
    Exit;
  end;
if ((success <> True) or (CkHttpResponse_getStatusCode(resp) <> 200)) then
  begin
    Memo1.Lines.Add('Look up list: HTTP ' + IntToStr(CkHttpResponse_getStatusCode(resp)) + ' ' + CkHttpResponse__bodyStr(resp));
    Exit;
  end;
matches := CkJsonObject_Create();
success := CkJsonObject_Load(matches,CkHttpResponse__bodyStr(resp));
if (success <> True) then
  begin
    Memo1.Lines.Add('Expected a JSON object in the response.');
    Exit;
  end;
if (CkJsonObject_SizeOfArray(matches,'lists') = 0) then
  begin
    Memo1.Lines.Add('No active email list named ' + listName);
    Exit;
  end;
if ((CkJsonObject_SizeOfArray(matches,'lists') <> 1) or (CkJsonObject_StringOfEquals(matches,'lists[0].name',listName,True) <> True)) then
  begin
    Memo1.Lines.Add('Expected one exact list name.');
    Exit;
  end;
listId := CkJsonObject__stringOf(matches,'lists[0].list_id');
if (listId = '') then
  begin
    Memo1.Lines.Add('Missing list ID.');
    Exit;
  end;
Memo1.Lines.Add(listName + ' | List ID: ' + listId + ' | Mailable contacts: ' + IntToStr(CkJsonObject_IntOf(matches,'lists[0].membership_count')));

CkSecrets_Dispose(vault);
CkJsonObject_Dispose(entry);
CkJsonObject_Dispose(tokens);
CkHttp_Dispose(http);
CkHttpResponse_Dispose(resp);
CkHttpRequest_Dispose(query);
CkJsonObject_Dispose(matches);