Objective-C Requires Chilkat v11.0.0+
Objective-C
SendGrid: Add or update list contacts and check the import job
See more SendGrid Examples
Use a Marketing Campaigns API key with contact access and the ID of an existing list. A 202 response queues the upsert; it does not confirm that the contacts were saved. Save the job ID and use it to recheck a pending import without submitting the contacts again. The error report may contain contact data; handle it privately.
Add or update contacts ยท Import job status
Chilkat Objective-C Downloads
#import <CkoHttp.h>
#import <CkoHttpResponse.h>
#import <NSString.h>
#import <CkoJsonObject.h>
BOOL success = NO;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success = NO;
CkoHttp *http = [[CkoHttp alloc] init];
http.RequireSslCertVerify = YES;
// Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
http.AuthToken = @"SENDGRID_API_KEY";
CkoHttpResponse *resp = [[CkoHttpResponse alloc] init];
NSString *jobId = @"";
if (jobId == @"") {
CkoJsonObject *contacts = [[CkoJsonObject alloc] init];
[contacts UpdateString: @"list_ids[0]" value: @"YOUR_LIST_UUID"];
[contacts UpdateString: @"contacts[0].email" value: @"avery@example.com"];
[contacts UpdateString: @"contacts[0].first_name" value: @"Avery"];
[contacts UpdateString: @"contacts[1].email" value: @"robin@example.com"];
[contacts UpdateString: @"contacts[1].first_name" value: @"Robin"];
success = [http HttpJson: @"PUT" url: @"https://api.sendgrid.com/v3/marketing/contacts" json: contacts contentType: @"application/json" response: resp];
if (success != YES) {
if ([resp.StatusCode intValue] == 0) {
NSLog(@"%@",http.LastErrorText);
}
else {
NSLog(@"%@%d%@%@",@"HTTP ",[resp.StatusCode intValue],@": ",resp.BodyStr);
}
return;
}
if ([resp.StatusCode intValue] != 202) {
NSLog(@"%@%d%@%@",@"HTTP ",[resp.StatusCode intValue],@": ",resp.BodyStr);
return;
}
CkoJsonObject *queued = [[CkoJsonObject alloc] init];
success = [queued Load: resp.BodyStr];
if (success != YES) {
NSLog(@"%@",@"Unexpected response: expected JSON.");
return;
}
if ([queued HasMember: @"job_id"] != YES) {
NSLog(@"%@",@"No job ID returned.");
return;
}
jobId = [queued StringOf: @"job_id"];
NSLog(@"%@%@",@"Save this import job ID: ",jobId);
}
[http SetUrlVar: @"job" value: jobId];
success = [http HttpNoBody: @"GET" url: @"https://api.sendgrid.com/v3/marketing/contacts/imports/{$job}" response: resp];
if (success != YES) {
if ([resp.StatusCode intValue] == 0) {
NSLog(@"%@",http.LastErrorText);
}
else {
NSLog(@"%@%d%@%@",@"HTTP ",[resp.StatusCode intValue],@": ",resp.BodyStr);
}
return;
}
if ([resp.StatusCode intValue] != 200) {
NSLog(@"%@%d%@%@",@"HTTP ",[resp.StatusCode intValue],@": ",resp.BodyStr);
return;
}
CkoJsonObject *job = [[CkoJsonObject alloc] init];
success = [job Load: resp.BodyStr];
if (success != YES) {
NSLog(@"%@",@"Unexpected response: expected JSON.");
return;
}
NSString *state = [job StringOf: @"status"];
if (state == @"pending") {
NSLog(@"%@%@",@"Still pending. Check again later with jobId = ",jobId);
return;
}
NSLog(@"%@%@",@"Job status: ",state);
NSLog(@"%@%d%@%d%@%d",@"Created: ",[[job IntOf: @"results.created_count"] intValue],@", updated: ",[[job IntOf: @"results.updated_count"] intValue],@", errors: "
,[[job IntOf: @"results.errored_count"] intValue]);
if ((state == @"errored") || (state == @"failed")) {
NSLog(@"%@",@"Review the job before retrying. If present, results.errors_url identifies the private error report.");
}