Sample code for 30+ languages & platforms
Delphi DLL Requires Chilkat v11.0.0+

Constant Contact V3: Find a contact by email address

See more Constant Contact Examples

Look up an exact email address across contact states, including deleted contacts. Pass the address as a query parameter so a literal plus sign survives URL encoding. Read the stored contact ID, email permission, and deletion marker. An empty contacts array means no match. 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;
query: HCkHttpRequest;
reply: HCkJsonObject;
count: Integer;
i: Integer;

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

query := CkHttpRequest_Create();
CkHttpRequest_putHttpVerb(query,'GET');
CkHttpRequest_AddParam(query,'email','reader+newsletter@example.com');
CkHttpRequest_AddParam(query,'status','all');
success := CkHttp_HttpReq(http,'https://api.cc.email/v3/contacts',query,resp);
if ((success <> True) or (CkHttpResponse_getStatusCode(resp) <> 200)) then
  begin
    Memo1.Lines.Add('Expected HTTP 200. 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;
reply := CkJsonObject_Create();
success := CkJsonObject_Load(reply,CkHttpResponse__bodyStr(resp));
if (success <> True) then
  begin
    Memo1.Lines.Add('Expected a JSON response.');
    Exit;
  end;
count := CkJsonObject_SizeOfArray(reply,'contacts');
if (count < 0) then
  begin
    Memo1.Lines.Add('The reply contained no contacts array.');
    Exit;
  end;
if (count = 0) then
  begin
    Memo1.Lines.Add('No contact has that email address.');
    Exit;
  end;
i := 0;
while (i < count) do
  begin
    CkJsonObject_putI(reply,i);
    Memo1.Lines.Add('Contact ID: ' + CkJsonObject__stringOf(reply,'contacts[i].contact_id'));
    Memo1.Lines.Add('Email permission: ' + CkJsonObject__stringOf(reply,'contacts[i].email_address.permission_to_send'));
    if (CkJsonObject_JsonTypeOf(reply,'contacts[i].deleted_at') = 1) then
      begin
        Memo1.Lines.Add('Deleted at: ' + CkJsonObject__stringOf(reply,'contacts[i].deleted_at'));
      end;
    i := i + 1;
  end;

CkSecrets_Dispose(vault);
CkJsonObject_Dispose(entry);
CkJsonObject_Dispose(tokens);
CkHttp_Dispose(http);
CkHttpResponse_Dispose(resp);
CkHttpRequest_Dispose(query);
CkJsonObject_Dispose(reply);