Lua Requires Chilkat v11.0.0+
Lua
SendGrid: Report daily requests, deliveries, bounces, and blocks
See more SendGrid Examples
Retrieve seven completed UTC dates and print one row per date. The counts are account-level aggregates; a day’s delivered count does not identify which messages sent that day were delivered.
Chilkat Lua Downloads
local chilkat = require("chilkat")
-- Assumes Chilkat is unlocked. See Global Unlock Sample.
local success = false
local http = chilkat.Http.new()
http.require_ssl_cert_verify = true
-- Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
http.auth_token = "SENDGRID_API_KEY"
local resp = chilkat.HttpResponse.new()
local day = chilkat.DateTime.new()
day:set_from_current_system_time()
day:add_days(-1)
http:set_url_var("end", day:get_as_iso8601("YYYY-MM-DD", false))
day:add_days(-6)
http:set_url_var("start", day:get_as_iso8601("YYYY-MM-DD", false))
success = http:http_no_body("GET", "https://api.sendgrid.com/v3/stats?start_date={$start}&end_date={$end}&aggregated_by=day", resp)
if not success then
if resp.status_code == 0 then
print(http.last_error_text)
else
print("HTTP " .. resp.status_code .. ": " .. resp.body_str)
end
return
end
if resp.status_code ~= 200 then
print("HTTP " .. resp.status_code .. ": " .. resp.body_str)
return
end
local days = chilkat.JsonArray.new()
success = days:load(resp.body_str)
if not success then
print("Unexpected response: expected JSON.")
return
end
print("Date,Requests,Delivered,Bounces,Blocks")
local row = chilkat.JsonObject.new()
local i = 0
while i < days.size do
days:object_at2(i, row)
print(row:string_of("date") .. "," .. row:int_of("stats[0].metrics.requests") .. "," .. row:int_of("stats[0].metrics.delivered") .. "," .. row:int_of("stats[0].metrics.bounces") .. "," .. row:int_of("stats[0].metrics.blocks"))
i = i + 1
end