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

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 Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

#  Assumes Chilkat is unlocked. See Global Unlock Sample.
set vault [new_CkSecrets]

CkSecrets_put_Location $vault "local_manager"
CkSecrets_put_AutoSplit $vault 1
set entry [new_CkJsonObject]

CkJsonObject_UpdateString $entry "appName" "ConstantContactV3"
CkJsonObject_UpdateString $entry "service" "OAuth2"
CkJsonObject_UpdateString $entry "username" "my-account"
set tokens [new_CkJsonObject]

set loaded [CkSecrets_GetSecretJson $vault $entry $tokens]
if {$loaded != 1} then {
    puts "Run the desktop authorization example using this account and store."
    delete_CkSecrets $vault
    delete_CkJsonObject $entry
    delete_CkJsonObject $tokens
    exit
}

if {[CkJsonObject_JsonTypeOf $tokens "access_token"] != 1} then {
    puts "The saved authorization has no access token."
    delete_CkSecrets $vault
    delete_CkJsonObject $entry
    delete_CkJsonObject $tokens
    exit
}

set http [new_CkHttp]

CkHttp_put_RequireSslCertVerify $http 1
CkHttp_put_AuthToken $http [CkJsonObject_stringOf $tokens "access_token"]
set resp [new_CkHttpResponse]

set body [new_CkJsonObject]

CkJsonObject_UpdateString $body "create_source" "Account"
CkJsonObject_UpdateString $body "first_name" "Alex"
CkJsonObject_UpdateString $body "last_name" "Rivera"
CkJsonObject_UpdateString $body "email_address.address" "reader+newsletter@example.com"
#  This sample assumes the account has recorded a qualifying existing relationship.
CkJsonObject_UpdateString $body "email_address.permission_to_send" "implicit"
CkJsonObject_UpdateString $body "street_addresses[0].kind" "work"
CkJsonObject_UpdateString $body "street_addresses[0].street" "100 Main Street"
CkJsonObject_UpdateString $body "street_addresses[0].city" "Boston"
CkJsonObject_UpdateString $body "street_addresses[0].state" "MA"
CkJsonObject_UpdateString $body "street_addresses[0].country" "United States"
set success [CkHttp_HttpJson $http "POST" "https://api.cc.email/v3/contacts" $body "application/json" $resp]
if {[CkHttpResponse_get_StatusCode $resp] == 409} then {
    puts "Contact already exists. Look up the email address to obtain its ID."
    delete_CkSecrets $vault
    delete_CkJsonObject $entry
    delete_CkJsonObject $tokens
    delete_CkHttp $http
    delete_CkHttpResponse $resp
    delete_CkJsonObject $body
    exit
}

if {expr [$success != 1]  ||  [[CkHttpResponse_get_StatusCode $resp] != 201]} then {
    puts "Expected HTTP 201. Received [CkHttpResponse_get_StatusCode $resp]"
    if {[CkHttpResponse_get_StatusCode $resp] == 401} then {
        puts "Refresh the saved access token or authorize again."
    }

    delete_CkSecrets $vault
    delete_CkJsonObject $entry
    delete_CkJsonObject $tokens
    delete_CkHttp $http
    delete_CkHttpResponse $resp
    delete_CkJsonObject $body
    exit
}

set created [new_CkJsonObject]

set success [CkJsonObject_Load $created [CkHttpResponse_bodyStr $resp]]
if {$success != 1} then {
    puts "Expected a JSON response."
    delete_CkSecrets $vault
    delete_CkJsonObject $entry
    delete_CkJsonObject $tokens
    delete_CkHttp $http
    delete_CkHttpResponse $resp
    delete_CkJsonObject $body
    delete_CkJsonObject $created
    exit
}

if {expr [[CkJsonObject_JsonTypeOf $created "contact_id"] != 1]  ||  [[CkJsonObject_StringOfEquals $created "contact_id" "" 1] == 1]} then {
    puts "Missing contact_id in the response."
    delete_CkSecrets $vault
    delete_CkJsonObject $entry
    delete_CkJsonObject $tokens
    delete_CkHttp $http
    delete_CkHttpResponse $resp
    delete_CkJsonObject $body
    delete_CkJsonObject $created
    exit
}

puts "Created contact ID: [CkJsonObject_stringOf $created contact_id]"

delete_CkSecrets $vault
delete_CkJsonObject $entry
delete_CkJsonObject $tokens
delete_CkHttp $http
delete_CkHttpResponse $resp
delete_CkJsonObject $body
delete_CkJsonObject $created