Sample code for 30+ languages & platforms
B4X Requires Chilkat v11.0.0+

SendGrid: Schedule a reminder and cancel it by batch ID

See more SendGrid Examples

Schedule a reminder one hour from now. Save its batch ID when it is allocated. If the appointment is canceled, use that saved ID in the cancellation branch. Mail Send accepts a Unix timestamp up to 72 hours ahead; cancel at least 10 minutes before the send time. Cancellation applies to the entire batch.

Create a batch ID ยท Cancel a batch

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

'  On cancellation, set this to the batch ID saved with the original appointment.
'  This branch does not create a new batch or send another reminder.
Dim batchToCancel As String = ""
If batchToCancel <> "" Then
    Dim cancellation As ChilkatJsonObject
    cancellation.Initialize
    cancellation.UpdateString("batch_id", batchToCancel)
    cancellation.UpdateString("status", "cancel")
    success = http.HttpJson("POST", "https://api.sendgrid.com/v3/user/scheduled_sends", cancellation, "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 <> 201 Then
        Log("HTTP " & resp.StatusCode & ": " & resp.BodyStr)
        Return
    End If

    Log("Batch cancellation accepted.")
    Return
End If


success = http.HttpNoBody("POST", "https://api.sendgrid.com/v3/mail/batch", 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 <> 201 Then
    Log("HTTP " & resp.StatusCode & ": " & resp.BodyStr)
    Return
End If


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

Dim batchId As String = batch.StringOf("batch_id")
If batch.HasMember("batch_id") <> True Then
    Log("Missing batch_id in response.")
    Return
End If


'  Allocate one batch per independently cancelable appointment.
Log("Save this batch ID with the appointment: " & batchId)
Dim sendTime As ChilkatDateTime
sendTime.Initialize
sendTime.SetFromCurrentSystemTime
sendTime.AddSeconds(3600)
Dim message As ChilkatJsonObject
message.Initialize
message.UpdateString("batch_id", batchId)
message.UpdateInt("send_at", sendTime.GetAsUnixTime(False))
message.UpdateString("from.email", "sender@example.com")
message.UpdateString("personalizations[0].to[0].email", "recipient@example.com")
message.UpdateString("subject", "Appointment reminder")
message.UpdateString("content[0].type", "text/plain")
message.UpdateString("content[0].value", "Your appointment is tomorrow at 9:30 AM Central.")

success = http.HttpJson("POST", "https://api.sendgrid.com/v3/mail/send", message, "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

Log("Reminder scheduled for " & sendTime.GetAsTimestamp(False))