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

Note
This example is intended for running within a Chilkat.Js embedded JavaScript engine. All Chilkat JavaScript examples require Chilkat v11.4.0 or greater.
JavaScript
var success = false;

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
var vault = new CkSecrets();
vault.Location = "local_manager";
vault.AutoSplit = true;
var entry = new CkJsonObject();
entry.UpdateString("appName","ConstantContactV3");
entry.UpdateString("service","OAuth2");
entry.UpdateString("username","my-account");
var tokens = new CkJsonObject();
var loaded = vault.GetSecretJson(entry,tokens);
if (loaded !== true) {
    console.log("Run the desktop authorization example using this account and store.");
    return;
}

if (tokens.JsonTypeOf("access_token") !== 1) {
    console.log("The saved authorization has no access token.");
    return;
}

var http = new CkHttp();
http.RequireSslCertVerify = true;
http.AuthToken = tokens.StringOf("access_token");
var resp = new CkHttpResponse();

var query = new CkHttpRequest();
query.HttpVerb = "GET";
query.AddParam("email","reader+newsletter@example.com");
query.AddParam("status","all");
success = http.HttpReq("https://api.cc.email/v3/contacts",query,resp);
if ((success !== true) || (resp.StatusCode !== 200)) {
    console.log("Expected HTTP 200. Received " + resp.StatusCode);
    if (resp.StatusCode == 401) {
        console.log("Refresh the saved access token or authorize again.");
    }

    return;
}

var reply = new CkJsonObject();
success = reply.Load(resp.BodyStr);
if (success !== true) {
    console.log("Expected a JSON response.");
    return;
}

var count = reply.SizeOfArray("contacts");
if (count < 0) {
    console.log("The reply contained no contacts array.");
    return;
}

if (count == 0) {
    console.log("No contact has that email address.");
    return;
}

var i = 0;
while ((i < count)) {
    reply.I = i;
    console.log("Contact ID: " + reply.StringOf("contacts[i].contact_id"));
    console.log("Email permission: " + reply.StringOf("contacts[i].email_address.permission_to_send"));
    if (reply.JsonTypeOf("contacts[i].deleted_at") == 1) {
        console.log("Deleted at: " + reply.StringOf("contacts[i].deleted_at"));
    }

    i = i+1;
}