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

Constant Contact V3: Restore a deleted contact to a list

See more Constant Contact Examples

Look up the deleted contact by email, then restore it by adding its existing ID to a chosen list. Constant Contact documents this membership activity as a restoration method. It changes only membership, preserves the saved profile, and makes no email-permission claim. Select the intended destination list explicitly; previous memberships were removed by deletion. 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","deleted");
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 matches = new CkJsonObject();
success = matches.Load(resp.BodyStr);
if (success !== true) {
    console.log("Expected a JSON response.");
    return;
}

if ((matches.SizeOfArray("contacts") !== 1) || (matches.JsonTypeOf("contacts[0].deleted_at") !== 1)) {
    console.log("Expected one deleted contact for this email. No restoration submitted.");
    return;
}

if ((matches.JsonTypeOf("contacts[0].contact_id") !== 1) || (matches.StringOfEquals("contacts[0].contact_id","",true) == true)) {
    console.log("Missing contacts[0].contact_id in the response.");
    return;
}

var body = new CkJsonObject();
body.UpdateString("source.contact_ids[0]",matches.StringOf("contacts[0].contact_id"));
body.UpdateString("list_ids[0]","DESTINATION_LIST_ID");
success = http.HttpJson("POST","https://api.cc.email/v3/activities/add_list_memberships",body,"application/json",resp);
if ((success !== true) || (resp.StatusCode < 200) || (resp.StatusCode >= 300)) {
    console.log("Could not start the membership activity. HTTP " + resp.StatusCode);
    return;
}

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

if ((job.JsonTypeOf("activity_id") !== 1) || (job.StringOfEquals("activity_id","",true) == true)) {
    console.log("Missing activity_id in the response.");
    return;
}

var activityId = job.StringOf("activity_id");
console.log("Submitted activity ID: " + activityId);
http.SetUrlVar("job",activityId);
//  Poll briefly. A longer-running job can be checked later using the same ID.
var attempt = 0;
while ((attempt < 5)) {
    success = http.HttpNoBody("GET","https://api.cc.email/v3/activities/{$job}",resp);
    if ((success !== true) || (resp.StatusCode !== 200)) {
        console.log("Status check returned HTTP " + resp.StatusCode + ". Keep the activity ID and check again later.");
        return;
    }

    success = job.Load(resp.BodyStr);
    if ((success !== true) || (job.JsonTypeOf("state") !== 1)) {
        console.log("Missing activity state. Keep the activity ID.");
        return;
    }

    var state = job.StringOf("state");
    if ((state !== "initialized") && (state !== "processing")) {
        console.log("Activity state: " + state);
        var i = 0;
        while ((i < job.SizeOfArray("activity_errors"))) {
            job.I = i;
            console.log(job.StringOf("activity_errors[i]"));
            i = i+1;
        }

        return;
    }

    attempt = attempt+1;
    if (attempt < 5) {
        http.SleepMs(2000);
    }

}

console.log("Still processing. Recheck the activity ID before submitting another request.");