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

Constant Contact V3: Find a contact by email address

See more Constant Contact Examples

Look up an exact email address across contact states, including deleted contacts. Pass the address as a query parameter so a literal plus sign survives URL encoding. Read the stored contact ID, email permission, and deletion marker. An empty contacts array means no match. 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()

local query = chilkat.HttpRequest.new()
query.http_verb = "GET"
query:add_param("email", "reader+newsletter@example.com")
query:add_param("status", "all")
local success = http:http_req("https://api.cc.email/v3/contacts", query, 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

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

local count = reply:size_of_array("contacts")
if count < 0 then
    print("The reply contained no contacts array.")
    return
end

if count == 0 then
    print("No contact has that email address.")
    return
end

local i = 0
while i < count do
    reply.i = i
    print("Contact ID: " .. reply:string_of("contacts[i].contact_id"))
    print("Email permission: " .. reply:string_of("contacts[i].email_address.permission_to_send"))
    if reply:json_type_of("contacts[i].deleted_at") == 1 then
        print("Deleted at: " .. reply:string_of("contacts[i].deleted_at"))
    end

    i = i + 1
end