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

Constant Contact V3: Add existing contacts to a list

See more Constant Contact Examples

Submit two existing contact IDs and one destination list ID. Constant Contact queues the change as a bulk activity. Save its ID, check the activity state and item errors, and resume checking that ID if the first few polls are inconclusive. Authorize the app first and use the same credential store.

Constant Contact developer guide

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- Assumes Chilkat is unlocked. See Global Unlock Sample.
local vault = chilkat.Secrets.new()
vault.location = "local_manager"
vault.auto_split = true
local entry = chilkat.JsonObject.new()
entry:update_string("appName", "ConstantContactV3")
entry:update_string("service", "OAuth2")
entry:update_string("username", "my-account")
local tokens = chilkat.JsonObject.new()
local loaded = vault:get_secret_json(entry, tokens)
if not loaded then
    print("Run the desktop authorization example using this account and store.")
    return
end

if tokens:json_type_of("access_token") ~= 1 then
    print("The saved authorization has no access token.")
    return
end

local http = chilkat.Http.new()
http.require_ssl_cert_verify = true
http.auth_token = tokens:string_of("access_token")
local resp = chilkat.HttpResponse.new()

-- Use contact IDs from email lookups and the list ID from the selected destination.
local body = chilkat.JsonObject.new()
body:update_string("source.contact_ids[0]", "FIRST_CONTACT_ID")
body:update_string("source.contact_ids[1]", "SECOND_CONTACT_ID")
body:update_string("list_ids[0]", "DESTINATION_LIST_ID")
local success = http:http_json("POST", "https://api.cc.email/v3/activities/add_list_memberships", body, "application/json", resp)
if (not success) or ((resp.status_code < 200) or (resp.status_code >= 300)) then
    print("Could not start the membership activity. HTTP " .. resp.status_code)
    return
end

local job = chilkat.JsonObject.new()
success = job:load(resp.body_str)
if not success then
    print("Expected a JSON response.")
    return
end

if (job:json_type_of("activity_id") ~= 1) or (job:string_of_equals("activity_id", "", true)) then
    print("Missing activity_id in the response.")
    return
end

local activityId = job:string_of("activity_id")
print("Submitted activity ID: " .. activityId)
http:set_url_var("job", activityId)
-- Poll briefly. A longer-running job can be checked later using the same ID.
local attempt = 0
while attempt < 5 do
    success = http:http_no_body("GET", "https://api.cc.email/v3/activities/{$job}", resp)
    if (not success) or (resp.status_code ~= 200) then
        print("Status check returned HTTP " .. resp.status_code .. ". Keep the activity ID and check again later.")
        return
    end

    success = job:load(resp.body_str)
    if (not success) or (job:json_type_of("state") ~= 1) then
        print("Missing activity state. Keep the activity ID.")
        return
    end

    local state = job:string_of("state")
    if (state ~= "initialized") and (state ~= "processing") then
        print("Activity state: " .. state)
        local i = 0
        while i < job:size_of_array("activity_errors") do
            job.i = i
            print(job:string_of("activity_errors[i]"))
            i = i + 1
        end

        return
    end

    attempt = attempt + 1
    if attempt < 5 then
        http:sleep_ms(2000)
    end
end

print("Still processing. Recheck the activity ID before submitting another request.")