Sample code for 30+ languages & platforms
VBScript 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 VBScript Downloads

VBScript
Dim fso, outFile
Set fso = CreateObject("Scripting.FileSystemObject")
'Create a Unicode (utf-16) output text file.
Set outFile = fso.CreateTextFile("output.txt", True, True)

success = 0

'  Assumes Chilkat is unlocked. See Global Unlock Sample.
set vault = CreateObject("Chilkat.Secrets")
vault.Location = "local_manager"
vault.AutoSplit = 1
set entry = CreateObject("Chilkat.JsonObject")
success = entry.UpdateString("appName","ConstantContactV3")
success = entry.UpdateString("service","OAuth2")
success = entry.UpdateString("username","my-account")
set tokens = CreateObject("Chilkat.JsonObject")
loaded = vault.GetSecretJson(entry,tokens)
If (loaded <> 1) Then
    outFile.WriteLine("Run the desktop authorization example using this account and store.")
    WScript.Quit
End If

If (tokens.JsonTypeOf("access_token") <> 1) Then
    outFile.WriteLine("The saved authorization has no access token.")
    WScript.Quit
End If

set http = CreateObject("Chilkat.Http")
http.RequireSslCertVerify = 1
http.AuthToken = tokens.StringOf("access_token")
set resp = CreateObject("Chilkat.HttpResponse")

set body = CreateObject("Chilkat.JsonObject")
success = body.UpdateString("create_source","Account")
success = body.UpdateString("first_name","Alex")
success = body.UpdateString("last_name","Rivera")
success = body.UpdateString("email_address.address","reader+newsletter@example.com")
'  This sample assumes the account has recorded a qualifying existing relationship.
success = body.UpdateString("email_address.permission_to_send","implicit")
success = body.UpdateString("street_addresses[0].kind","work")
success = body.UpdateString("street_addresses[0].street","100 Main Street")
success = body.UpdateString("street_addresses[0].city","Boston")
success = body.UpdateString("street_addresses[0].state","MA")
success = body.UpdateString("street_addresses[0].country","United States")
success = http.HttpJson("POST","https://api.cc.email/v3/contacts",body,"application/json",resp)
If (resp.StatusCode = 409) Then
    outFile.WriteLine("Contact already exists. Look up the email address to obtain its ID.")
    WScript.Quit
End If

If ((success <> 1) Or (resp.StatusCode <> 201)) Then
    outFile.WriteLine("Expected HTTP 201. Received " & resp.StatusCode)
    If (resp.StatusCode = 401) Then
        outFile.WriteLine("Refresh the saved access token or authorize again.")
    End If

    WScript.Quit
End If

set created = CreateObject("Chilkat.JsonObject")
success = created.Load(resp.BodyStr)
If (success <> 1) Then
    outFile.WriteLine("Expected a JSON response.")
    WScript.Quit
End If

If ((created.JsonTypeOf("contact_id") <> 1) Or (created.StringOfEquals("contact_id","",1) = 1)) Then
    outFile.WriteLine("Missing contact_id in the response.")
    WScript.Quit
End If

outFile.WriteLine("Created contact ID: " & created.StringOf("contact_id"))

outFile.Close