Sample code for 30+ languages & platforms
Lazarus Pascal Requires Chilkat v11.0.0+

Constant Contact V3: Create a contact list

See more Constant Contact Examples

Create a list with a unique name and a short description. Save the returned list_id with your application’s list record and use it in the membership examples. The new list starts empty. 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.HttpResponse,
  Chilkat.JsonObject;

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

procedure RunDemo;
var
  success: Boolean;
  vault: TSecrets;
  entry: TJsonObject;
  tokens: TJsonObject;
  loaded: Boolean;
  http: THttp;
  resp: THttpResponse;
  body: TJsonObject;
  created: TJsonObject;

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;

  body := TJsonObject.Create;
  body.UpdateString('name','Workshop announcements');
  body.UpdateString('description','People who requested workshop updates');
  success := http.HttpJson('POST','https://api.cc.email/v3/contact_lists',body,'application/json',resp);
  if (resp.StatusCode = 409) then
    begin
      WriteLn('A conflicting list already exists. Check the existing lists before retrying.');
      Exit;
    end;
  if ((success <> True) or (resp.StatusCode <> 201)) then
    begin
      WriteLn('Expected HTTP 201. Received ' + resp.StatusCode);
      if (resp.StatusCode = 401) then
        begin
          WriteLn('Refresh the saved access token or authorize again.');
        end;
      Exit;
    end;
  created := TJsonObject.Create;
  success := created.Load(resp.BodyStr);
  if (success <> True) then
    begin
      WriteLn('Expected a JSON response.');
      Exit;
    end;
  if ((created.JsonTypeOf('list_id') <> 1) or (created.StringOfEquals('list_id','',True) = True)) then
    begin
      WriteLn('Missing list_id in the response.');
      Exit;
    end;
  WriteLn('Created ' + created.StringOf('name') + '. List ID: ' + created.StringOf('list_id'));


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