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

B4X
Dim success As Boolean = False

'  Assumes Chilkat is unlocked. See Global Unlock Sample.
success = False
Dim http As ChilkatHttp
http.Initialize("http")
http.RequireSslCertVerify = True
'  Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
http.AuthToken = "SENDGRID_API_KEY"
Dim resp As ChilkatHttpResponse
resp.Initialize

Dim jobId As String = ""
If jobId = "" Then
    Dim contacts As ChilkatJsonObject
    contacts.Initialize
    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 Then
        If resp.StatusCode = 0 Then
            Log(http.LastErrorText)
        Else
            Log("HTTP " & resp.StatusCode & ": " & resp.BodyStr)
        End If

        Return
    End If

    If resp.StatusCode <> 202 Then
        Log("HTTP " & resp.StatusCode & ": " & resp.BodyStr)
        Return
    End If


    Dim queued As ChilkatJsonObject
    queued.Initialize
    success = queued.Load(resp.BodyStr)
    If success <> True Then
        Log("Unexpected response: expected JSON.")
        Return
    End If

    If queued.HasMember("job_id") <> True Then
        Log("No job ID returned.")
        Return
    End If

    jobId = queued.StringOf("job_id")
    Log("Save this import job ID: " & jobId)
End If

http.SetUrlVar("job", jobId)

success = http.HttpNoBody("GET", "https://api.sendgrid.com/v3/marketing/contacts/imports/{$job}", resp)
If success <> True Then
    If resp.StatusCode = 0 Then
        Log(http.LastErrorText)
    Else
        Log("HTTP " & resp.StatusCode & ": " & resp.BodyStr)
    End If

    Return
End If

If resp.StatusCode <> 200 Then
    Log("HTTP " & resp.StatusCode & ": " & resp.BodyStr)
    Return
End If


Dim job As ChilkatJsonObject
job.Initialize
success = job.Load(resp.BodyStr)
If success <> True Then
    Log("Unexpected response: expected JSON.")
    Return
End If

Dim state As String = job.StringOf("status")
If state = "pending" Then
    Log("Still pending. Check again later with jobId = " & jobId)
    Return
End If

Log("Job status: " & state)
Log("Created: " & job.IntOf("results.created_count") & ", updated: " & job.IntOf("results.updated_count") & ", errors: " & job.IntOf("results.errored_count"))
If (state = "errored") Or (state = "failed") Then
    Log("Review the job before retrying. If present, results.errors_url identifies the private error report.")
End If