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

Constant Contact V3: LIVE SEND - Schedule a newsletter for a named list

See more Constant Contact Examples

LIVE SEND. Scheduling this campaign delivers email to eligible contacts on the selected list at the specified time. Use the reviewer test to check the email first. Select the intended newsletter and list in the same account. The example accepts a primary draft, replaces its entire audience with one active email list, and schedules it. List lookup requires contact_data. Editing and scheduling require campaign_data and account privileges to edit and send.

Enter a future ISO 8601 timestamp with a UTC offset or Z. To cancel before delivery starts, keep the same activity selection, change action to unschedule, and run again. Cancellation removes the schedule and keeps the audience assignment. If the scheduling response is lost, check the activity’s schedule before retrying.

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;

//  LIVE SEND: Scheduling this activity delivers email to the selected contact list.
//  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 selection file from the draft or campaign lookup example for this account.
var selection = new CkJsonObject();
success = selection.LoadFile("newsletter-selection.json");
var activityId = selection.StringOf("campaign_activity_id");
if ((success !== true) || (activityId == "")) {
    console.log("Select a campaign and save its primary activity ID first.");
    return;
}

http.SetUrlVar("activity",activityId);
var action = "schedule";
if (action == "unschedule") {
    success = http.HttpNoBody("DELETE","https://api.cc.email/v3/emails/activities/{$activity}/schedules",resp);
    if ((success !== true) && (resp.StatusCode <= 0)) {
        console.log("Remove schedule: HTTP request failed.");
        return;
    }

    if ((success !== true) || (resp.StatusCode !== 204)) {
        console.log("Remove schedule: HTTP " + resp.StatusCode + " " + resp.BodyStr);
        return;
    }

    console.log("Schedule removed for activity " + activityId);
    return;
}

if (action !== "schedule") {
    console.log("Choose schedule or unschedule.");
    return;
}

//  Replace the placeholder with the intended future delivery time, e.g. 2030-05-15T09:00:00-05:00.
var deliveryTime = "ENTER_FUTURE_TIMESTAMP_WITH_OFFSET";
var delivery = new CkDateTime();
var now = new CkDateTime();
now.SetFromCurrentSystemTime();
success = delivery.SetFromTimestamp(deliveryTime);
if ((success !== true) || (delivery.DiffSeconds(now) <= 0)) {
    console.log("Enter a valid future delivery time with a UTC offset or Z.");
    return;
}

//  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"));

//  PUT replaces the activity. Read it first to preserve the subject, sender and other settings.
//  Constant Contact retains omitted subresources such as html_content and the physical footer.
success = http.HttpNoBody("GET","https://api.cc.email/v3/emails/activities/{$activity}",resp);
if ((success !== true) && (resp.StatusCode <= 0)) {
    console.log("Read draft: HTTP request failed.");
    return;
}

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

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

if ((draft.StringOfEquals("role","primary_email",true) !== true) || (draft.StringOfEquals("current_status","DRAFT",true) !== true)) {
    console.log("Select a primary activity in DRAFT status.");
    return;
}

console.log("LIVE SEND: " + draft.StringOf("subject") + " | Activity: " + activityId + " | List: " + listName + " | Delivery: " + deliveryTime);
draft.UpdateNewArray("contact_list_ids");
draft.UpdateString("contact_list_ids[0]",listId);
draft.UpdateNewArray("segment_ids");
success = http.HttpJson("PUT","https://api.cc.email/v3/emails/activities/{$activity}",draft,"application/json",resp);
if ((success !== true) && (resp.StatusCode <= 0)) {
    console.log("Assign audience: HTTP request failed.");
    return;
}

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

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

if ((updated.SizeOfArray("contact_list_ids") !== 1) || (updated.StringOfEquals("contact_list_ids[0]",listId,true) !== true) || (updated.SizeOfArray("segment_ids") > 0)) {
    console.log("The saved audience did not match the requested list. Scheduling stopped.");
    return;
}

var schedule = new CkJsonObject();
schedule.UpdateString("scheduled_date",delivery.GetAsTimestamp(false));
success = http.HttpJson("POST","https://api.cc.email/v3/emails/activities/{$activity}/schedules",schedule,"application/json",resp);
if ((success !== true) || (resp.StatusCode < 200) || (resp.StatusCode >= 300)) {
    console.log("Schedule not confirmed. HTTP " + resp.StatusCode);
    console.log("The audience was saved. Check the schedule before retrying.");
    return;
}

console.log("Live delivery scheduled for " + schedule.StringOf("scheduled_date") + " to " + listName);