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

Python
import sys
import chilkat2

success = False

# Assumes Chilkat is unlocked. See Global Unlock Sample.
success = False
http = chilkat2.Http()
http.RequireSslCertVerify = True
# Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
http.AuthToken = "SENDGRID_API_KEY"
resp = chilkat2.HttpResponse()

jobId = ""
if (jobId == ""):
    contacts = chilkat2.JsonObject()
    contacts.UpdateString("list_ids[0]","YOUR_LIST_UUID")
    contacts.UpdateString("contacts[0].email","avery@example.com")
    contacts.UpdateString("contacts[0].first_name","Avery")
    contacts.UpdateString("contacts[1].email","robin@example.com")
    contacts.UpdateString("contacts[1].first_name","Robin")
    success = http.HttpJson("PUT","https://api.sendgrid.com/v3/marketing/contacts",contacts,"application/json",resp)
    if (success != True):
        if (resp.StatusCode == 0):
            print(http.LastErrorText)
        else:
            print("HTTP " + str(resp.StatusCode) + ": " + resp.BodyStr)

        sys.exit()

    if (resp.StatusCode != 202):
        print("HTTP " + str(resp.StatusCode) + ": " + resp.BodyStr)
        sys.exit()

    queued = chilkat2.JsonObject()
    success = queued.Load(resp.BodyStr)
    if (success != True):
        print("Unexpected response: expected JSON.")
        sys.exit()

    if (queued.HasMember("job_id") != True):
        print("No job ID returned.")
        sys.exit()

    jobId = queued.StringOf("job_id")
    print("Save this import job ID: " + jobId)

http.SetUrlVar("job",jobId)

success = http.HttpNoBody("GET","https://api.sendgrid.com/v3/marketing/contacts/imports/{$job}",resp)
if (success != True):
    if (resp.StatusCode == 0):
        print(http.LastErrorText)
    else:
        print("HTTP " + str(resp.StatusCode) + ": " + resp.BodyStr)

    sys.exit()

if (resp.StatusCode != 200):
    print("HTTP " + str(resp.StatusCode) + ": " + resp.BodyStr)
    sys.exit()

job = chilkat2.JsonObject()
success = job.Load(resp.BodyStr)
if (success != True):
    print("Unexpected response: expected JSON.")
    sys.exit()

state = job.StringOf("status")
if (state == "pending"):
    print("Still pending. Check again later with jobId = " + jobId)
    sys.exit()

print("Job status: " + state)
print("Created: " + str(job.IntOf("results.created_count")) + ", updated: " + str(job.IntOf("results.updated_count")) + ", errors: " + str(job.IntOf("results.errored_count")))
if ((state == "errored") or (state == "failed")):
    print("Review the job before retrying. If present, results.errors_url identifies the private error report.")