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

Constant Contact V3: Create a contact

See more Constant Contact Examples

Create an account-entered contact and read its new ID. Use the email permission that accurately describes the contact’s relationship with the account. The optional postal address belongs in the street_addresses array. On a duplicate, look up the existing contact before deciding whether an update is appropriate. 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('create_source','Account');
  body.UpdateString('first_name','Alex');
  body.UpdateString('last_name','Rivera');
  body.UpdateString('email_address.address','reader+newsletter@example.com');
  //  This sample assumes the account has recorded a qualifying existing relationship.
  body.UpdateString('email_address.permission_to_send','implicit');
  body.UpdateString('street_addresses[0].kind','work');
  body.UpdateString('street_addresses[0].street','100 Main Street');
  body.UpdateString('street_addresses[0].city','Boston');
  body.UpdateString('street_addresses[0].state','MA');
  body.UpdateString('street_addresses[0].country','United States');
  success := http.HttpJson('POST','https://api.cc.email/v3/contacts',body,'application/json',resp);
  if (resp.StatusCode = 409) then
    begin
      WriteLn('Contact already exists. Look up the email address to obtain its ID.');
      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('contact_id') <> 1) or (created.StringOfEquals('contact_id','',True) = True)) then
    begin
      WriteLn('Missing contact_id in the response.');
      Exit;
    end;
  WriteLn('Created contact ID: ' + created.StringOf('contact_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.