Classic ASP Requires Chilkat v11.0.0+
Classic ASP
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 Classic ASP Downloads
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<%
success = 0
' Assumes Chilkat is unlocked. See Global Unlock Sample.
success = 0
set http = Server.CreateObject("Chilkat.Http")
http.RequireSslCertVerify = 1
' Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
http.AuthToken = "SENDGRID_API_KEY"
set resp = Server.CreateObject("Chilkat.HttpResponse")
jobId = ""
If (jobId = "") Then
set contacts = Server.CreateObject("Chilkat.JsonObject")
success = contacts.UpdateString("list_ids[0]","YOUR_LIST_UUID")
success = contacts.UpdateString("contacts[0].email","avery@example.com")
success = contacts.UpdateString("contacts[0].first_name","Avery")
success = contacts.UpdateString("contacts[1].email","robin@example.com")
success = contacts.UpdateString("contacts[1].first_name","Robin")
success = http.HttpJson("PUT","https://api.sendgrid.com/v3/marketing/contacts",contacts,"application/json",resp)
If (success <> 1) Then
If (resp.StatusCode = 0) Then
Response.Write "<pre>" & Server.HTMLEncode( http.LastErrorText) & "</pre>"
Else
Response.Write "<pre>" & Server.HTMLEncode( "HTTP " & resp.StatusCode & ": " & resp.BodyStr) & "</pre>"
End If
Response.End
End If
If (resp.StatusCode <> 202) Then
Response.Write "<pre>" & Server.HTMLEncode( "HTTP " & resp.StatusCode & ": " & resp.BodyStr) & "</pre>"
Response.End
End If
set queued = Server.CreateObject("Chilkat.JsonObject")
success = queued.Load(resp.BodyStr)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Unexpected response: expected JSON.") & "</pre>"
Response.End
End If
If (queued.HasMember("job_id") <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "No job ID returned.") & "</pre>"
Response.End
End If
jobId = queued.StringOf("job_id")
Response.Write "<pre>" & Server.HTMLEncode( "Save this import job ID: " & jobId) & "</pre>"
End If
success = http.SetUrlVar("job",jobId)
success = http.HttpNoBody("GET","https://api.sendgrid.com/v3/marketing/contacts/imports/{$job}",resp)
If (success <> 1) Then
If (resp.StatusCode = 0) Then
Response.Write "<pre>" & Server.HTMLEncode( http.LastErrorText) & "</pre>"
Else
Response.Write "<pre>" & Server.HTMLEncode( "HTTP " & resp.StatusCode & ": " & resp.BodyStr) & "</pre>"
End If
Response.End
End If
If (resp.StatusCode <> 200) Then
Response.Write "<pre>" & Server.HTMLEncode( "HTTP " & resp.StatusCode & ": " & resp.BodyStr) & "</pre>"
Response.End
End If
set job = Server.CreateObject("Chilkat.JsonObject")
success = job.Load(resp.BodyStr)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Unexpected response: expected JSON.") & "</pre>"
Response.End
End If
state = job.StringOf("status")
If (state = "pending") Then
Response.Write "<pre>" & Server.HTMLEncode( "Still pending. Check again later with jobId = " & jobId) & "</pre>"
Response.End
End If
Response.Write "<pre>" & Server.HTMLEncode( "Job status: " & state) & "</pre>"
Response.Write "<pre>" & Server.HTMLEncode( "Created: " & job.IntOf("results.created_count") & ", updated: " & job.IntOf("results.updated_count") & ", errors: " & job.IntOf("results.errored_count")) & "</pre>"
If ((state = "errored") Or (state = "failed")) Then
Response.Write "<pre>" & Server.HTMLEncode( "Review the job before retrying. If present, results.errors_url identifies the private error report.") & "</pre>"
End If
%>
</body>
</html>