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

Delphi ActiveX
var
success: Integer;
vault: TChilkatSecrets;
entry: TChilkatJsonObject;
tokens: TChilkatJsonObject;
loaded: Integer;
http: TChilkatHttp;
resp: TChilkatHttpResponse;
listName: WideString;
query: TChilkatHttpRequest;
matches: TChilkatJsonObject;
listId: WideString;

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

//  Use the full display name. AddParam encodes spaces and punctuation.
listName := 'Workshop announcements';
query := TChilkatHttpRequest.Create(Self);
query.HttpVerb := 'GET';
query.AddParam('name',listName);
query.AddParam('status','active');
query.AddParam('include_membership_count','active');
success := http.HttpReq('https://api.cc.email/v3/contact_lists',query.ControlInterface,resp.ControlInterface);
if ((success <> 1) and (resp.StatusCode <= 0)) then
  begin
    Memo1.Lines.Add('Look up list: HTTP request failed.');
    Exit;
  end;
if ((success <> 1) or (resp.StatusCode <> 200)) then
  begin
    Memo1.Lines.Add('Look up list: HTTP ' + IntToStr(resp.StatusCode) + ' ' + resp.BodyStr);
    Exit;
  end;
matches := TChilkatJsonObject.Create(Self);
success := matches.Load(resp.BodyStr);
if (success <> 1) then
  begin
    Memo1.Lines.Add('Expected a JSON object in the response.');
    Exit;
  end;
if (matches.SizeOfArray('lists') = 0) then
  begin
    Memo1.Lines.Add('No active email list named ' + listName);
    Exit;
  end;
if ((matches.SizeOfArray('lists') <> 1) or (matches.StringOfEquals('lists[0].name',listName,1) <> 1)) then
  begin
    Memo1.Lines.Add('Expected one exact list name.');
    Exit;
  end;
listId := matches.StringOf('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(matches.IntOf('lists[0].membership_count')));