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

Constant Contact V3: Read every page of list members

See more Constant Contact Examples

Filter the contacts collection with lists and a list ID. To query multiple lists together, supply their comma-separated IDs. Follow the returned cursor links and report the total after the last page. The default contacts query excludes deleted records. A list member can still have unsubscribed email permission. Authorize the app first and use the same credential store.

Constant Contact developer guide

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

local success = false

-- 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()

http:set_url_var("list", "LIST_ID")
local url = chilkat.StringBuilder.new()
url:append("https://api.cc.email/v3/contacts?lists={$list}&limit=50")
local page = chilkat.JsonObject.new()
local total = 0
while url.length > 0 do
    success = http:http_no_body("GET", url:get_as_string(), resp)
    if (not success) or (resp.status_code ~= 200) then
        print("Expected HTTP 200. Received " .. resp.status_code)
        if resp.status_code == 401 then
            print("Refresh the saved access token or authorize again.")
        end

        return
    end

    success = page:load(resp.body_str)
    local count = page:size_of_array("contacts")
    if (not success) or (count < 0) then
        print("Expected a page of contacts.")
        return
    end

    local i = 0
    while i < count do
        page.i = i
        print(page:string_of("contacts[i].contact_id") .. "  " .. page:string_of("contacts[i].email_address.address"))
        i = i + 1
    end

    total = total + count
    url:clear()
    if page:json_type_of("_links.next.href") == 1 then
        url:append(page:string_of("_links.next.href"))
        -- The documented cursor link is relative to api.cc.email.
        if not url:starts_with("/v3/contacts?", true) then
            print("Unexpected pagination link. Stopped before following it.")
            return
        end

        url:prepend("https://api.cc.email")
    end
end

print("List members read: " .. total)