Lua Requires Chilkat v11.0.0+
Lua
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
Chilkat Lua Downloads
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 the full display name. AddParam encodes spaces and punctuation.
local listName = "Workshop announcements"
local query = chilkat.HttpRequest.new()
query.http_verb = "GET"
query:add_param("name", listName)
query:add_param("status", "active")
query:add_param("include_membership_count", "active")
local success = http:http_req("https://api.cc.email/v3/contact_lists", query, resp)
if (not success) and (resp.status_code <= 0) then
print("Look up list: HTTP request failed.")
return
end
if (not success) or (resp.status_code ~= 200) then
print("Look up list: HTTP " .. resp.status_code .. " " .. resp.body_str)
return
end
local matches = chilkat.JsonObject.new()
success = matches:load(resp.body_str)
if not success then
print("Expected a JSON object in the response.")
return
end
if matches:size_of_array("lists") == 0 then
print("No active email list named " .. listName)
return
end
if (matches:size_of_array("lists") ~= 1) or (not matches:string_of_equals("lists[0].name", listName, true)) then
print("Expected one exact list name.")
return
end
local listId = matches:string_of("lists[0].list_id")
if listId == "" then
print("Missing list ID.")
return
end
print(listName .. " | List ID: " .. listId .. " | Mailable contacts: " .. matches:int_of("lists[0].membership_count"))