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

Constant Contact V3: Create a contact list

See more Constant Contact Examples

Create a list with a unique name and a short description. Save the returned list_id with your application’s list record and use it in the membership examples. The new list starts empty. 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 body = new CkJsonObject();
body.UpdateString("name","Workshop announcements");
body.UpdateString("description","People who requested workshop updates");
success = http.HttpJson("POST","https://api.cc.email/v3/contact_lists",body,"application/json",resp);
if (resp.StatusCode == 409) {
    console.log("A conflicting list already exists. Check the existing lists before retrying.");
    return;
}

if ((success !== true) || (resp.StatusCode !== 201)) {
    console.log("Expected HTTP 201. Received " + resp.StatusCode);
    if (resp.StatusCode == 401) {
        console.log("Refresh the saved access token or authorize again.");
    }

    return;
}

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

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

console.log("Created " + created.StringOf("name") + ". List ID: " + created.StringOf("list_id"));