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

Tcl

load ./chilkat.dll

set success 0

#  Assumes Chilkat is unlocked. See Global Unlock Sample.
set success 0
set http [new_CkHttp]

CkHttp_put_RequireSslCertVerify $http 1
#  Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
CkHttp_put_AuthToken $http "SENDGRID_API_KEY"
set resp [new_CkHttpResponse]

set jobId ""
if {$jobId == ""} then {
    set contacts [new_CkJsonObject]

    CkJsonObject_UpdateString $contacts "list_ids[0]" "YOUR_LIST_UUID"
    CkJsonObject_UpdateString $contacts "contacts[0].email" "avery@example.com"
    CkJsonObject_UpdateString $contacts "contacts[0].first_name" "Avery"
    CkJsonObject_UpdateString $contacts "contacts[1].email" "robin@example.com"
    CkJsonObject_UpdateString $contacts "contacts[1].first_name" "Robin"
    set success [CkHttp_HttpJson $http "PUT" "https://api.sendgrid.com/v3/marketing/contacts" $contacts "application/json" $resp]
    if {$success != 1} then {
        if {[CkHttpResponse_get_StatusCode $resp] == 0} then {
            puts [CkHttp_lastErrorText $http]
        }         else {
            puts "HTTP [CkHttpResponse_get_StatusCode $resp]: [CkHttpResponse_bodyStr $resp]"
        }

        delete_CkHttp $http
        delete_CkHttpResponse $resp
        delete_CkJsonObject $contacts
        exit
    }

    if {[CkHttpResponse_get_StatusCode $resp] != 202} then {
        puts "HTTP [CkHttpResponse_get_StatusCode $resp]: [CkHttpResponse_bodyStr $resp]"
        delete_CkHttp $http
        delete_CkHttpResponse $resp
        delete_CkJsonObject $contacts
        exit
    }

    set queued [new_CkJsonObject]

    set success [CkJsonObject_Load $queued [CkHttpResponse_bodyStr $resp]]
    if {$success != 1} then {
        puts "Unexpected response: expected JSON."
        delete_CkHttp $http
        delete_CkHttpResponse $resp
        delete_CkJsonObject $contacts
        delete_CkJsonObject $queued
        exit
    }

    if {[CkJsonObject_HasMember $queued "job_id"] != 1} then {
        puts "No job ID returned."
        delete_CkHttp $http
        delete_CkHttpResponse $resp
        delete_CkJsonObject $contacts
        delete_CkJsonObject $queued
        exit
    }

    set jobId [CkJsonObject_stringOf $queued "job_id"]
    puts "Save this import job ID: $jobId"
}

CkHttp_SetUrlVar $http "job" $jobId

set success [CkHttp_HttpNoBody $http "GET" "https://api.sendgrid.com/v3/marketing/contacts/imports/{$job}" $resp]
if {$success != 1} then {
    if {[CkHttpResponse_get_StatusCode $resp] == 0} then {
        puts [CkHttp_lastErrorText $http]
    }     else {
        puts "HTTP [CkHttpResponse_get_StatusCode $resp]: [CkHttpResponse_bodyStr $resp]"
    }

    delete_CkHttp $http
    delete_CkHttpResponse $resp
    delete_CkJsonObject $contacts
    delete_CkJsonObject $queued
    exit
}

if {[CkHttpResponse_get_StatusCode $resp] != 200} then {
    puts "HTTP [CkHttpResponse_get_StatusCode $resp]: [CkHttpResponse_bodyStr $resp]"
    delete_CkHttp $http
    delete_CkHttpResponse $resp
    delete_CkJsonObject $contacts
    delete_CkJsonObject $queued
    exit
}

set job [new_CkJsonObject]

set success [CkJsonObject_Load $job [CkHttpResponse_bodyStr $resp]]
if {$success != 1} then {
    puts "Unexpected response: expected JSON."
    delete_CkHttp $http
    delete_CkHttpResponse $resp
    delete_CkJsonObject $contacts
    delete_CkJsonObject $queued
    delete_CkJsonObject $job
    exit
}

set state [CkJsonObject_stringOf $job "status"]
if {$state == "pending"} then {
    puts "Still pending. Check again later with jobId = $jobId"
    delete_CkHttp $http
    delete_CkHttpResponse $resp
    delete_CkJsonObject $contacts
    delete_CkJsonObject $queued
    delete_CkJsonObject $job
    exit
}

puts "Job status: $state"
puts "Created: [CkJsonObject_IntOf $job results.created_count], updated: [CkJsonObject_IntOf $job results.updated_count], errors: [CkJsonObject_IntOf $job results.errored_count]"
if {expr [$state == "errored"]  ||  [$state == "failed"]} then {
    puts "Review the job before retrying. If present, results.errors_url identifies the private error report."
}


delete_CkHttp $http
delete_CkHttpResponse $resp
delete_CkJsonObject $contacts
delete_CkJsonObject $queued
delete_CkJsonObject $job