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

Constant Contact V3: Find a contact list by name

See more Constant Contact Examples

Start with the full list name from the Constant Contact account. Read its list_id and the number of active, mailable contacts. Use the ID for list membership operations or campaign targeting. This lookup requires contact_data.

Authorize the application using the same account and 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();

//  Use the full display name. AddParam encodes spaces and punctuation.
var listName = "Workshop announcements";
var query = new CkHttpRequest();
query.HttpVerb = "GET";
query.AddParam("name",listName);
query.AddParam("status","active");
query.AddParam("include_membership_count","active");
success = http.HttpReq("https://api.cc.email/v3/contact_lists",query,resp);
if ((success !== true) && (resp.StatusCode <= 0)) {
    console.log("Look up list: HTTP request failed.");
    return;
}

if ((success !== true) || (resp.StatusCode !== 200)) {
    console.log("Look up list: HTTP " + resp.StatusCode + " " + resp.BodyStr);
    return;
}

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

if (matches.SizeOfArray("lists") == 0) {
    console.log("No active email list named " + listName);
    return;
}

if ((matches.SizeOfArray("lists") !== 1) || (matches.StringOfEquals("lists[0].name",listName,true) !== true)) {
    console.log("Expected one exact list name.");
    return;
}

var listId = matches.StringOf("lists[0].list_id");
if (listId == "") {
    console.log("Missing list ID.");
    return;
}

console.log(listName + " | List ID: " + listId + " | Mailable contacts: " + matches.IntOf("lists[0].membership_count"));