Sample code for 30+ languages & platforms
Lazarus Pascal 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 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.HttpRequest,
  Chilkat.HttpResponse,
  Chilkat.JsonObject;

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

procedure RunDemo;
var
  success: Boolean;
  vault: TSecrets;
  entry: TJsonObject;
  tokens: TJsonObject;
  loaded: Boolean;
  http: THttp;
  resp: THttpResponse;
  listName: string;
  query: THttpRequest;
  matches: TJsonObject;
  listId: string;

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;

  //  Use the full display name. AddParam encodes spaces and punctuation.
  listName := 'Workshop announcements';
  query := THttpRequest.Create;
  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,resp);
  if ((success <> True) and (resp.StatusCode <= 0)) then
    begin
      WriteLn('Look up list: HTTP request failed.');
      Exit;
    end;
  if ((success <> True) or (resp.StatusCode <> 200)) then
    begin
      WriteLn('Look up list: HTTP ' + resp.StatusCode + ' ' + resp.BodyStr);
      Exit;
    end;
  matches := TJsonObject.Create;
  success := matches.Load(resp.BodyStr);
  if (success <> True) then
    begin
      WriteLn('Expected a JSON object in the response.');
      Exit;
    end;
  if (matches.SizeOfArray('lists') = 0) then
    begin
      WriteLn('No active email list named ' + listName);
      Exit;
    end;
  if ((matches.SizeOfArray('lists') <> 1) or (matches.StringOfEquals('lists[0].name',listName,True) <> True)) then
    begin
      WriteLn('Expected one exact list name.');
      Exit;
    end;
  listId := matches.StringOf('lists[0].list_id');
  if (listId = '') then
    begin
      WriteLn('Missing list ID.');
      Exit;
    end;
  WriteLn(listName + ' | List ID: ' + listId + ' | Mailable contacts: ' + matches.IntOf('lists[0].membership_count'));


  vault.Free;
  entry.Free;
  tokens.Free;
  http.Free;
  resp.Free;
  query.Free;
  matches.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.