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

Delphi DLL
var
success: Boolean;
vault: HCkSecrets;
entry: HCkJsonObject;
tokens: HCkJsonObject;
loaded: Boolean;
http: HCkHttp;
resp: HCkHttpResponse;
body: HCkJsonObject;
created: HCkJsonObject;

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

body := CkJsonObject_Create();
CkJsonObject_UpdateString(body,'create_source','Account');
CkJsonObject_UpdateString(body,'first_name','Alex');
CkJsonObject_UpdateString(body,'last_name','Rivera');
CkJsonObject_UpdateString(body,'email_address.address','reader+newsletter@example.com');
//  This sample assumes the account has recorded a qualifying existing relationship.
CkJsonObject_UpdateString(body,'email_address.permission_to_send','implicit');
CkJsonObject_UpdateString(body,'street_addresses[0].kind','work');
CkJsonObject_UpdateString(body,'street_addresses[0].street','100 Main Street');
CkJsonObject_UpdateString(body,'street_addresses[0].city','Boston');
CkJsonObject_UpdateString(body,'street_addresses[0].state','MA');
CkJsonObject_UpdateString(body,'street_addresses[0].country','United States');
success := CkHttp_HttpJson(http,'POST','https://api.cc.email/v3/contacts',body,'application/json',resp);
if (CkHttpResponse_getStatusCode(resp) = 409) then
  begin
    Memo1.Lines.Add('Contact already exists. Look up the email address to obtain its ID.');
    Exit;
  end;
if ((success <> True) or (CkHttpResponse_getStatusCode(resp) <> 201)) then
  begin
    Memo1.Lines.Add('Expected HTTP 201. Received ' + IntToStr(CkHttpResponse_getStatusCode(resp)));
    if (CkHttpResponse_getStatusCode(resp) = 401) then
      begin
        Memo1.Lines.Add('Refresh the saved access token or authorize again.');
      end;
    Exit;
  end;
created := CkJsonObject_Create();
success := CkJsonObject_Load(created,CkHttpResponse__bodyStr(resp));
if (success <> True) then
  begin
    Memo1.Lines.Add('Expected a JSON response.');
    Exit;
  end;
if ((CkJsonObject_JsonTypeOf(created,'contact_id') <> 1) or (CkJsonObject_StringOfEquals(created,'contact_id','',True) = True)) then
  begin
    Memo1.Lines.Add('Missing contact_id in the response.');
    Exit;
  end;
Memo1.Lines.Add('Created contact ID: ' + CkJsonObject__stringOf(created,'contact_id'));

CkSecrets_Dispose(vault);
CkJsonObject_Dispose(entry);
CkJsonObject_Dispose(tokens);
CkHttp_Dispose(http);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(body);
CkJsonObject_Dispose(created);