AutoIt Requires Chilkat v11.0.0+
AutoIt
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 AutoIt Downloads
Local $bSuccess = False
; Assumes Chilkat is unlocked. See Global Unlock Sample.
$bSuccess = False
$oHttp = ObjCreate("Chilkat.Http")
$oHttp.RequireSslCertVerify = True
; Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
$oHttp.AuthToken = "SENDGRID_API_KEY"
$oResp = ObjCreate("Chilkat.HttpResponse")
Local $sJobId = ""
If ($sJobId = "") Then
$oContacts = ObjCreate("Chilkat.JsonObject")
$oContacts.UpdateString("list_ids[0]","YOUR_LIST_UUID")
$oContacts.UpdateString("contacts[0].email","avery@example.com")
$oContacts.UpdateString("contacts[0].first_name","Avery")
$oContacts.UpdateString("contacts[1].email","robin@example.com")
$oContacts.UpdateString("contacts[1].first_name","Robin")
$bSuccess = $oHttp.HttpJson("PUT","https://api.sendgrid.com/v3/marketing/contacts",$oContacts,"application/json",$oResp)
If ($bSuccess <> True) Then
If ($oResp.StatusCode = 0) Then
ConsoleWrite($oHttp.LastErrorText & @CRLF)
Else
ConsoleWrite("HTTP " & $oResp.StatusCode & ": " & $oResp.BodyStr & @CRLF)
EndIf
Exit
EndIf
If ($oResp.StatusCode <> 202) Then
ConsoleWrite("HTTP " & $oResp.StatusCode & ": " & $oResp.BodyStr & @CRLF)
Exit
EndIf
$oQueued = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oQueued.Load($oResp.BodyStr)
If ($bSuccess <> True) Then
ConsoleWrite("Unexpected response: expected JSON." & @CRLF)
Exit
EndIf
If ($oQueued.HasMember("job_id") <> True) Then
ConsoleWrite("No job ID returned." & @CRLF)
Exit
EndIf
$sJobId = $oQueued.StringOf("job_id")
ConsoleWrite("Save this import job ID: " & $sJobId & @CRLF)
EndIf
$oHttp.SetUrlVar("job",$sJobId)
$bSuccess = $oHttp.HttpNoBody("GET","https://api.sendgrid.com/v3/marketing/contacts/imports/{$job}",$oResp)
If ($bSuccess <> True) Then
If ($oResp.StatusCode = 0) Then
ConsoleWrite($oHttp.LastErrorText & @CRLF)
Else
ConsoleWrite("HTTP " & $oResp.StatusCode & ": " & $oResp.BodyStr & @CRLF)
EndIf
Exit
EndIf
If ($oResp.StatusCode <> 200) Then
ConsoleWrite("HTTP " & $oResp.StatusCode & ": " & $oResp.BodyStr & @CRLF)
Exit
EndIf
$oJob = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oJob.Load($oResp.BodyStr)
If ($bSuccess <> True) Then
ConsoleWrite("Unexpected response: expected JSON." & @CRLF)
Exit
EndIf
Local $state = $oJob.StringOf("status")
If ($state = "pending") Then
ConsoleWrite("Still pending. Check again later with jobId = " & $sJobId & @CRLF)
Exit
EndIf
ConsoleWrite("Job status: " & $state & @CRLF)
ConsoleWrite("Created: " & $oJob.IntOf("results.created_count") & ", updated: " & $oJob.IntOf("results.updated_count") & ", errors: " & $oJob.IntOf("results.errored_count") & @CRLF)
If (($state = "errored") Or ($state = "failed")) Then
ConsoleWrite("Review the job before retrying. If present, results.errors_url identifies the private error report." & @CRLF)
EndIf