Lua Requires Chilkat v11.0.0+
Lua
Constant Contact V3: Create a contact
See more Constant Contact Examples
Create an account-entered contact and read its new ID. Use the email permission that accurately describes the contact’s relationship with the account. The optional postal address belongs in the street_addresses array. On a duplicate, look up the existing contact before deciding whether an update is appropriate. Authorize the app first and use the same 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()
local body = chilkat.JsonObject.new()
body:update_string("create_source", "Account")
body:update_string("first_name", "Alex")
body:update_string("last_name", "Rivera")
body:update_string("email_address.address", "reader+newsletter@example.com")
-- This sample assumes the account has recorded a qualifying existing relationship.
body:update_string("email_address.permission_to_send", "implicit")
body:update_string("street_addresses[0].kind", "work")
body:update_string("street_addresses[0].street", "100 Main Street")
body:update_string("street_addresses[0].city", "Boston")
body:update_string("street_addresses[0].state", "MA")
body:update_string("street_addresses[0].country", "United States")
local success = http:http_json("POST", "https://api.cc.email/v3/contacts", body, "application/json", resp)
if resp.status_code == 409 then
print("Contact already exists. Look up the email address to obtain its ID.")
return
end
if (not success) or (resp.status_code ~= 201) then
print("Expected HTTP 201. Received " .. resp.status_code)
if resp.status_code == 401 then
print("Refresh the saved access token or authorize again.")
end
return
end
local created = chilkat.JsonObject.new()
success = created:load(resp.body_str)
if not success then
print("Expected a JSON response.")
return
end
if (created:json_type_of("contact_id") ~= 1) or (created:string_of_equals("contact_id", "", true)) then
print("Missing contact_id in the response.")
return
end
print("Created contact ID: " .. created:string_of("contact_id"))