Dart Requires Chilkat v11.0.0+
Dart
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 Dart Downloads
import 'package:chilkat/chilkat.dart';
void main() {
// Assumes Chilkat is unlocked. See Global Unlock Sample.
final http = CkHttp();
http.requireSslCertVerify = true;
// Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
http.authToken = 'SENDGRID_API_KEY';
final resp = CkHttpResponse();
final day = CkDateTime();
day.setFromCurrentSystemTime();
day.addDays(-1);
http.setUrlVar('end', day.getAsIso8601('YYYY-MM-DD', false));
day.addDays(-6);
http.setUrlVar('start', day.getAsIso8601('YYYY-MM-DD', false));
try {
http.httpNoBody('GET', 'https://api.sendgrid.com/v3/stats?start_date={\$start}&end_date={\$end}&aggregated_by=day', resp);
} on ChilkatException catch (e) {
if (resp.statusCode == 0) {
print(e.lastErrorText);
} else {
print('HTTP ${resp.statusCode}: ${resp.bodyStr}');
}
return;
}
if (resp.statusCode != 200) {
print('HTTP ${resp.statusCode}: ${resp.bodyStr}');
return;
}
final days = CkJsonArray();
try {
days.load(resp.bodyStr);
} on ChilkatException {
print('Unexpected response: expected JSON.');
return;
}
print('Date,Requests,Delivered,Bounces,Blocks');
final row = CkJsonObject();
var i = 0;
while (i < days.size) {
days.objectAt2(i, row);
print('${row.stringOf('date')},${row.intOf('stats[0].metrics.requests')},${row.intOf('stats[0].metrics.delivered')},${row.intOf('stats[0].metrics.bounces')},${row.intOf('stats[0].metrics.blocks')}');
i++;
}
}