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

Swift

func chilkatTest() {
    var success: Bool = false

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    success = false
    let http = CkoHttp()!
    http.requireSslCertVerify = true
    //  Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
    http.authToken = "SENDGRID_API_KEY"
    let resp = CkoHttpResponse()!

    //  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.
    var batchToCancel: String? = ""
    if batchToCancel != "" {
        let cancellation = CkoJsonObject()!
        cancellation.updateString(jsonPath: "batch_id", value: batchToCancel)
        cancellation.updateString(jsonPath: "status", value: "cancel")
        success = http.httpJson(verb: "POST", url: "https://api.sendgrid.com/v3/user/scheduled_sends", json: cancellation, contentType: "application/json", response: resp)
        if success != true {
            if resp.statusCode.intValue == 0 {
                print("\(http.lastErrorText!)")
            }
            else {
                print("HTTP \(resp.statusCode.intValue): \(resp.bodyStr!)")
            }

            return
        }

        if resp.statusCode.intValue != 201 {
            print("HTTP \(resp.statusCode.intValue): \(resp.bodyStr!)")
            return
        }

        print("Batch cancellation accepted.")
        return
    }

    success = http.httpNoBody(verb: "POST", url: "https://api.sendgrid.com/v3/mail/batch", response: resp)
    if success != true {
        if resp.statusCode.intValue == 0 {
            print("\(http.lastErrorText!)")
        }
        else {
            print("HTTP \(resp.statusCode.intValue): \(resp.bodyStr!)")
        }

        return
    }

    if resp.statusCode.intValue != 201 {
        print("HTTP \(resp.statusCode.intValue): \(resp.bodyStr!)")
        return
    }

    let batch = CkoJsonObject()!
    success = batch.load(json: resp.bodyStr)
    if success != true {
        print("Unexpected response: expected JSON.")
        return
    }

    var batchId: String? = batch.string(of: "batch_id")
    if batch.hasMember(jsonPath: "batch_id") != true {
        print("Missing batch_id in response.")
        return
    }

    //  Allocate one batch per independently cancelable appointment.
    print("Save this batch ID with the appointment: \(batchId!)")
    let sendTime = CkoDateTime()!
    sendTime.setFromCurrentSystemTime()
    sendTime.addSeconds(numSeconds: 3600)
    let message = CkoJsonObject()!
    message.updateString(jsonPath: "batch_id", value: batchId)
    message.updateInt(jsonPath: "send_at", value: sendTime.get(asUnixTime: false))
    message.updateString(jsonPath: "from.email", value: "sender@example.com")
    message.updateString(jsonPath: "personalizations[0].to[0].email", value: "recipient@example.com")
    message.updateString(jsonPath: "subject", value: "Appointment reminder")
    message.updateString(jsonPath: "content[0].type", value: "text/plain")
    message.updateString(jsonPath: "content[0].value", value: "Your appointment is tomorrow at 9:30 AM Central.")

    success = http.httpJson(verb: "POST", url: "https://api.sendgrid.com/v3/mail/send", json: message, contentType: "application/json", response: resp)
    if success != true {
        if resp.statusCode.intValue == 0 {
            print("\(http.lastErrorText!)")
        }
        else {
            print("HTTP \(resp.statusCode.intValue): \(resp.bodyStr!)")
        }

        return
    }

    if resp.statusCode.intValue != 202 {
        print("HTTP \(resp.statusCode.intValue): \(resp.bodyStr!)")
        return
    }

    print("Reminder scheduled for \(sendTime.get(asTimestamp: false)!)")

}