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

PowerShell
Add-Type -Path "C:\chilkat\ChilkatDotNet47-x64\ChilkatDotNet47.dll"

$success = $false

#  Assumes Chilkat is unlocked. See Global Unlock Sample.
$success = $false
$http = New-Object Chilkat.Http
$http.RequireSslCertVerify = $true
#  Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
$http.AuthToken = "SENDGRID_API_KEY"
$resp = New-Object Chilkat.HttpResponse

$jobId = ""
if ($jobId -eq "") {
    $contacts = New-Object Chilkat.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 -ne $true) {
        if ($resp.StatusCode -eq 0) {
            $($http.LastErrorText)
        }
        else {
            $("HTTP " + $resp.StatusCode + ": " + $resp.BodyStr)
        }

        exit
    }

    if ($resp.StatusCode -ne 202) {
        $("HTTP " + $resp.StatusCode + ": " + $resp.BodyStr)
        exit
    }

    $queued = New-Object Chilkat.JsonObject
    $success = $queued.Load($resp.BodyStr)
    if ($success -ne $true) {
        $("Unexpected response: expected JSON.")
        exit
    }

    if ($queued.HasMember("job_id") -ne $true) {
        $("No job ID returned.")
        exit
    }

    $jobId = $queued.StringOf("job_id")
    $("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 -ne $true) {
    if ($resp.StatusCode -eq 0) {
        $($http.LastErrorText)
    }
    else {
        $("HTTP " + $resp.StatusCode + ": " + $resp.BodyStr)
    }

    exit
}

if ($resp.StatusCode -ne 200) {
    $("HTTP " + $resp.StatusCode + ": " + $resp.BodyStr)
    exit
}

$job = New-Object Chilkat.JsonObject
$success = $job.Load($resp.BodyStr)
if ($success -ne $true) {
    $("Unexpected response: expected JSON.")
    exit
}

$state = $job.StringOf("status")
if ($state -eq "pending") {
    $("Still pending. Check again later with jobId = " + $jobId)
    exit
}

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