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

Xbase++
LOCAL nSuccess
LOCAL oVault
LOCAL oEntry
LOCAL oTokens
LOCAL nLoaded
LOCAL oHttp
LOCAL oResp
LOCAL oBody
LOCAL oCreated

nSuccess := 0

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
oVault := CreateObject("Chilkat.Secrets")
oVault:Location := "local_manager"
oVault:AutoSplit := 1
oEntry := CreateObject("Chilkat.JsonObject")
oEntry:UpdateString("appName", "ConstantContactV3")
oEntry:UpdateString("service", "OAuth2")
oEntry:UpdateString("username", "my-account")
oTokens := CreateObject("Chilkat.JsonObject")
nLoaded := oVault:GetSecretJson(oEntry, oTokens)
IF (nLoaded != 1)
    ? "Run the desktop authorization example using this account and store."
    oVault:destroy()
    oEntry:destroy()
    oTokens:destroy()
    RETURN
ENDIF

IF (oTokens:JsonTypeOf("access_token") != 1)
    ? "The saved authorization has no access token."
    oVault:destroy()
    oEntry:destroy()
    oTokens:destroy()
    RETURN
ENDIF

oHttp := CreateObject("Chilkat.Http")
oHttp:RequireSslCertVerify := 1
oHttp:AuthToken := oTokens:StringOf("access_token")
oResp := CreateObject("Chilkat.HttpResponse")

oBody := CreateObject("Chilkat.JsonObject")
oBody:UpdateString("create_source", "Account")
oBody:UpdateString("first_name", "Alex")
oBody:UpdateString("last_name", "Rivera")
oBody:UpdateString("email_address.address", "reader+newsletter@example.com")
//  This sample assumes the account has recorded a qualifying existing relationship.
oBody:UpdateString("email_address.permission_to_send", "implicit")
oBody:UpdateString("street_addresses[0].kind", "work")
oBody:UpdateString("street_addresses[0].street", "100 Main Street")
oBody:UpdateString("street_addresses[0].city", "Boston")
oBody:UpdateString("street_addresses[0].state", "MA")
oBody:UpdateString("street_addresses[0].country", "United States")
nSuccess := oHttp:HttpJson("POST", "https://api.cc.email/v3/contacts", oBody, "application/json", oResp)
IF (oResp:StatusCode == 409)
    ? "Contact already exists. Look up the email address to obtain its ID."
    oVault:destroy()
    oEntry:destroy()
    oTokens:destroy()
    oHttp:destroy()
    oResp:destroy()
    oBody:destroy()
    RETURN
ENDIF

IF ((nSuccess != 1) .OR. (oResp:StatusCode != 201))
    ? "Expected HTTP 201. Received " + Str(oResp:StatusCode)
    IF (oResp:StatusCode == 401)
        ? "Refresh the saved access token or authorize again."
    ENDIF

    oVault:destroy()
    oEntry:destroy()
    oTokens:destroy()
    oHttp:destroy()
    oResp:destroy()
    oBody:destroy()
    RETURN
ENDIF

oCreated := CreateObject("Chilkat.JsonObject")
nSuccess := oCreated:Load(oResp:BodyStr)
IF (nSuccess != 1)
    ? "Expected a JSON response."
    oVault:destroy()
    oEntry:destroy()
    oTokens:destroy()
    oHttp:destroy()
    oResp:destroy()
    oBody:destroy()
    oCreated:destroy()
    RETURN
ENDIF

IF ((oCreated:JsonTypeOf("contact_id") != 1) .OR. (oCreated:StringOfEquals("contact_id", "", 1) == 1))
    ? "Missing contact_id in the response."
    oVault:destroy()
    oEntry:destroy()
    oTokens:destroy()
    oHttp:destroy()
    oResp:destroy()
    oBody:destroy()
    oCreated:destroy()
    RETURN
ENDIF

? "Created contact ID: " + oCreated:StringOf("contact_id")

oVault:destroy()
oEntry:destroy()
oTokens:destroy()
oHttp:destroy()
oResp:destroy()
oBody:destroy()
oCreated:destroy()