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

Visual FoxPro
LOCAL lnSuccess
LOCAL loVault
LOCAL loEntry
LOCAL loTokens
LOCAL lnLoaded
LOCAL loHttp
LOCAL loResp
LOCAL loBody
LOCAL loCreated

lnSuccess = 0

*  Assumes Chilkat is unlocked. See Global Unlock Sample.
loVault = CreateObject('Chilkat.Secrets')
loVault.Location = "local_manager"
loVault.AutoSplit = 1
loEntry = CreateObject('Chilkat.JsonObject')
loEntry.UpdateString("appName","ConstantContactV3")
loEntry.UpdateString("service","OAuth2")
loEntry.UpdateString("username","my-account")
loTokens = CreateObject('Chilkat.JsonObject')
lnLoaded = loVault.GetSecretJson(loEntry,loTokens)
IF (lnLoaded <> 1) THEN
    ? "Run the desktop authorization example using this account and store."
    RELEASE loVault
    RELEASE loEntry
    RELEASE loTokens
    CANCEL
ENDIF

IF (loTokens.JsonTypeOf("access_token") <> 1) THEN
    ? "The saved authorization has no access token."
    RELEASE loVault
    RELEASE loEntry
    RELEASE loTokens
    CANCEL
ENDIF

loHttp = CreateObject('Chilkat.Http')
loHttp.RequireSslCertVerify = 1
loHttp.AuthToken = loTokens.StringOf("access_token")
loResp = CreateObject('Chilkat.HttpResponse')

loBody = CreateObject('Chilkat.JsonObject')
loBody.UpdateString("create_source","Account")
loBody.UpdateString("first_name","Alex")
loBody.UpdateString("last_name","Rivera")
loBody.UpdateString("email_address.address","reader+newsletter@example.com")
*  This sample assumes the account has recorded a qualifying existing relationship.
loBody.UpdateString("email_address.permission_to_send","implicit")
loBody.UpdateString("street_addresses[0].kind","work")
loBody.UpdateString("street_addresses[0].street","100 Main Street")
loBody.UpdateString("street_addresses[0].city","Boston")
loBody.UpdateString("street_addresses[0].state","MA")
loBody.UpdateString("street_addresses[0].country","United States")
lnSuccess = loHttp.HttpJson("POST","https://api.cc.email/v3/contacts",loBody,"application/json",loResp)
IF (loResp.StatusCode = 409) THEN
    ? "Contact already exists. Look up the email address to obtain its ID."
    RELEASE loVault
    RELEASE loEntry
    RELEASE loTokens
    RELEASE loHttp
    RELEASE loResp
    RELEASE loBody
    CANCEL
ENDIF

IF ((lnSuccess <> 1) OR (loResp.StatusCode <> 201)) THEN
    ? "Expected HTTP 201. Received " + STR(loResp.StatusCode)
    IF (loResp.StatusCode = 401) THEN
        ? "Refresh the saved access token or authorize again."
    ENDIF

    RELEASE loVault
    RELEASE loEntry
    RELEASE loTokens
    RELEASE loHttp
    RELEASE loResp
    RELEASE loBody
    CANCEL
ENDIF

loCreated = CreateObject('Chilkat.JsonObject')
lnSuccess = loCreated.Load(loResp.BodyStr)
IF (lnSuccess <> 1) THEN
    ? "Expected a JSON response."
    RELEASE loVault
    RELEASE loEntry
    RELEASE loTokens
    RELEASE loHttp
    RELEASE loResp
    RELEASE loBody
    RELEASE loCreated
    CANCEL
ENDIF

IF ((loCreated.JsonTypeOf("contact_id") <> 1) OR (loCreated.StringOfEquals("contact_id","",1) = 1)) THEN
    ? "Missing contact_id in the response."
    RELEASE loVault
    RELEASE loEntry
    RELEASE loTokens
    RELEASE loHttp
    RELEASE loResp
    RELEASE loBody
    RELEASE loCreated
    CANCEL
ENDIF

? "Created contact ID: " + loCreated.StringOf("contact_id")

RELEASE loVault
RELEASE loEntry
RELEASE loTokens
RELEASE loHttp
RELEASE loResp
RELEASE loBody
RELEASE loCreated