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

CkPython
import sys
import chilkat

success = False

#  Assumes Chilkat is unlocked. See Global Unlock Sample.
success = False
http = chilkat.CkHttp()
http.put_RequireSslCertVerify(True)
#  Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
http.put_AuthToken("SENDGRID_API_KEY")
resp = chilkat.CkHttpResponse()

#  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.
batchToCancel = ""
if (batchToCancel != ""):
    cancellation = chilkat.CkJsonObject()
    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):
        if (resp.get_StatusCode() == 0):
            print(http.lastErrorText())
        else:
            print("HTTP " + str(resp.get_StatusCode()) + ": " + resp.bodyStr())

        sys.exit()

    if (resp.get_StatusCode() != 201):
        print("HTTP " + str(resp.get_StatusCode()) + ": " + resp.bodyStr())
        sys.exit()

    print("Batch cancellation accepted.")
    sys.exit()

success = http.HttpNoBody("POST","https://api.sendgrid.com/v3/mail/batch",resp)
if (success != True):
    if (resp.get_StatusCode() == 0):
        print(http.lastErrorText())
    else:
        print("HTTP " + str(resp.get_StatusCode()) + ": " + resp.bodyStr())

    sys.exit()

if (resp.get_StatusCode() != 201):
    print("HTTP " + str(resp.get_StatusCode()) + ": " + resp.bodyStr())
    sys.exit()

batch = chilkat.CkJsonObject()
success = batch.Load(resp.bodyStr())
if (success != True):
    print("Unexpected response: expected JSON.")
    sys.exit()

batchId = batch.stringOf("batch_id")
if (batch.HasMember("batch_id") != True):
    print("Missing batch_id in response.")
    sys.exit()

#  Allocate one batch per independently cancelable appointment.
print("Save this batch ID with the appointment: " + batchId)
sendTime = chilkat.CkDateTime()
sendTime.SetFromCurrentSystemTime()
sendTime.AddSeconds(3600)
message = chilkat.CkJsonObject()
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):
    if (resp.get_StatusCode() == 0):
        print(http.lastErrorText())
    else:
        print("HTTP " + str(resp.get_StatusCode()) + ": " + resp.bodyStr())

    sys.exit()

if (resp.get_StatusCode() != 202):
    print("HTTP " + str(resp.get_StatusCode()) + ": " + resp.bodyStr())
    sys.exit()

print("Reminder scheduled for " + sendTime.getAsTimestamp(False))