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

SendGrid: Add or update list contacts and check the import job

See more SendGrid Examples

Use a Marketing Campaigns API key with contact access and the ID of an existing list. A 202 response queues the upsert; it does not confirm that the contacts were saved. Save the job ID and use it to recheck a pending import without submitting the contacts again. The error report may contain contact data; handle it privately.

Add or update contacts ยท Import job status

Chilkat Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loHttp
LOCAL loResp
LOCAL lcJobId
LOCAL loContacts
LOCAL loQueued
LOCAL loJob
LOCAL lcState

lnSuccess = 0

*  Assumes Chilkat is unlocked. See Global Unlock Sample.
lnSuccess = 0
loHttp = CreateObject('Chilkat.Http')
loHttp.RequireSslCertVerify = 1
*  Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
loHttp.AuthToken = "SENDGRID_API_KEY"
loResp = CreateObject('Chilkat.HttpResponse')

lcJobId = ""
IF (lcJobId = "") THEN
    loContacts = CreateObject('Chilkat.JsonObject')
    loContacts.UpdateString("list_ids[0]","YOUR_LIST_UUID")
    loContacts.UpdateString("contacts[0].email","avery@example.com")
    loContacts.UpdateString("contacts[0].first_name","Avery")
    loContacts.UpdateString("contacts[1].email","robin@example.com")
    loContacts.UpdateString("contacts[1].first_name","Robin")
    lnSuccess = loHttp.HttpJson("PUT","https://api.sendgrid.com/v3/marketing/contacts",loContacts,"application/json",loResp)
    IF (lnSuccess <> 1) THEN
        IF (loResp.StatusCode = 0) THEN
            ? loHttp.LastErrorText
        ELSE
            ? "HTTP " + STR(loResp.StatusCode) + ": " + loResp.BodyStr
        ENDIF

        RELEASE loHttp
        RELEASE loResp
        RELEASE loContacts
        CANCEL
    ENDIF

    IF (loResp.StatusCode <> 202) THEN
        ? "HTTP " + STR(loResp.StatusCode) + ": " + loResp.BodyStr
        RELEASE loHttp
        RELEASE loResp
        RELEASE loContacts
        CANCEL
    ENDIF

    loQueued = CreateObject('Chilkat.JsonObject')
    lnSuccess = loQueued.Load(loResp.BodyStr)
    IF (lnSuccess <> 1) THEN
        ? "Unexpected response: expected JSON."
        RELEASE loHttp
        RELEASE loResp
        RELEASE loContacts
        RELEASE loQueued
        CANCEL
    ENDIF

    IF (loQueued.HasMember("job_id") <> 1) THEN
        ? "No job ID returned."
        RELEASE loHttp
        RELEASE loResp
        RELEASE loContacts
        RELEASE loQueued
        CANCEL
    ENDIF

    lcJobId = loQueued.StringOf("job_id")
    ? "Save this import job ID: " + lcJobId
ENDIF

loHttp.SetUrlVar("job",lcJobId)

lnSuccess = loHttp.HttpNoBody("GET","https://api.sendgrid.com/v3/marketing/contacts/imports/{$job}",loResp)
IF (lnSuccess <> 1) THEN
    IF (loResp.StatusCode = 0) THEN
        ? loHttp.LastErrorText
    ELSE
        ? "HTTP " + STR(loResp.StatusCode) + ": " + loResp.BodyStr
    ENDIF

    RELEASE loHttp
    RELEASE loResp
    RELEASE loContacts
    RELEASE loQueued
    CANCEL
ENDIF

IF (loResp.StatusCode <> 200) THEN
    ? "HTTP " + STR(loResp.StatusCode) + ": " + loResp.BodyStr
    RELEASE loHttp
    RELEASE loResp
    RELEASE loContacts
    RELEASE loQueued
    CANCEL
ENDIF

loJob = CreateObject('Chilkat.JsonObject')
lnSuccess = loJob.Load(loResp.BodyStr)
IF (lnSuccess <> 1) THEN
    ? "Unexpected response: expected JSON."
    RELEASE loHttp
    RELEASE loResp
    RELEASE loContacts
    RELEASE loQueued
    RELEASE loJob
    CANCEL
ENDIF

lcState = loJob.StringOf("status")
IF (lcState = "pending") THEN
    ? "Still pending. Check again later with jobId = " + lcJobId
    RELEASE loHttp
    RELEASE loResp
    RELEASE loContacts
    RELEASE loQueued
    RELEASE loJob
    CANCEL
ENDIF

? "Job status: " + lcState
? "Created: " + STR(loJob.IntOf("results.created_count")) + ", updated: " + STR(loJob.IntOf("results.updated_count")) + ", errors: " + STR(loJob.IntOf("results.errored_count"))
IF ((lcState = "errored") OR (lcState = "failed")) THEN
    ? "Review the job before retrying. If present, results.errors_url identifies the private error report."
ENDIF

RELEASE loHttp
RELEASE loResp
RELEASE loContacts
RELEASE loQueued
RELEASE loJob