Objective-C
Objective-C
Shippo Create the Shipment, Get Rates, and Purchase Label
See more Shippo Examples
Demonstrates how retrieve rates and create labels for international shipments.Chilkat Objective-C Downloads
#import <CkoHttp.h>
#import <CkoJsonObject.h>
#import <CkoHttpResponse.h>
#import <CkoStringBuilder.h>
#import <NSString.h>
BOOL success = NO;
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
CkoHttp *http = [[CkoHttp alloc] init];
// Implements the following CURL command:
// curl https://api.goshippo.com/customs/declarations/ \
// -H "Authorization: ShippoToken <API_TOKEN>" \
// -H "Content-Type: application/json" \
// -d '{
// "contents_type": "MERCHANDISE",
// "non_delivery_option": "RETURN",
// "certify": true,
// "certify_signer": "Simon Kreuz",
// "incoterm": "DDU",
// "items": [{
// "description": "T-shirt",
// "quantity": 20,
// "net_weight": "5",
// "mass_unit": "lb",
// "value_amount": "200",
// "value_currency": "USD",
// "tariff_number": "",
// "origin_country": "US"
// }]
// }'
// Use this online tool to generate code from sample JSON:
// Generate Code to Create JSON
// The following JSON is sent in the request body.
// {
// "contents_type": "MERCHANDISE",
// "non_delivery_option": "RETURN",
// "certify": true,
// "certify_signer": "Simon Kreuz",
// "incoterm": "DDU",
// "items": [
// {
// "description": "T-shirt",
// "quantity": 20,
// "net_weight": "5",
// "mass_unit": "lb",
// "value_amount": "200",
// "value_currency": "USD",
// "tariff_number": "",
// "origin_country": "US"
// }
// ]
// }
CkoJsonObject *json = [[CkoJsonObject alloc] init];
[json UpdateString: @"contents_type" value: @"MERCHANDISE"];
[json UpdateString: @"non_delivery_option" value: @"RETURN"];
[json UpdateBool: @"certify" value: YES];
[json UpdateString: @"certify_signer" value: @"Simon Kreuz"];
[json UpdateString: @"incoterm" value: @"DDU"];
[json UpdateString: @"items[0].description" value: @"T-shirt"];
[json UpdateInt: @"items[0].quantity" value: [NSNumber numberWithInt: 20]];
[json UpdateString: @"items[0].net_weight" value: @"5"];
[json UpdateString: @"items[0].mass_unit" value: @"lb"];
[json UpdateString: @"items[0].value_amount" value: @"200"];
[json UpdateString: @"items[0].value_currency" value: @"USD"];
[json UpdateString: @"items[0].tariff_number" value: @""];
[json UpdateString: @"items[0].origin_country" value: @"US"];
[http SetRequestHeader: @"Authorization" value: @"ShippoToken <API_TOKEN>"];
[http SetRequestHeader: @"Content-Type" value: @"application/json"];
CkoHttpResponse *resp = [[CkoHttpResponse alloc] init];
success = [http HttpJson: @"POST" url: @"https://api.goshippo.com/customs/declarations/" json: json contentType: @"application/json" response: resp];
if (success == NO) {
NSLog(@"%@",http.LastErrorText);
return;
}
CkoStringBuilder *sbResponseBody = [[CkoStringBuilder alloc] init];
[resp GetBodySb: sbResponseBody];
CkoJsonObject *jResp = [[CkoJsonObject alloc] init];
[jResp LoadSb: sbResponseBody];
jResp.EmitCompact = NO;
NSLog(@"%@",@"Response Body:");
NSLog(@"%@",[jResp Emit]);
int respStatusCode = [resp.StatusCode intValue];
NSLog(@"%@%d",@"Response Status Code = ",respStatusCode);
if (respStatusCode >= 400) {
NSLog(@"%@",@"Response Header:");
NSLog(@"%@",resp.Header);
NSLog(@"%@",@"Failed.");
return;
}
// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)
// {
// "object_created": "2014-07-17T00:04:06.163Z",
// "object_updated": "2014-07-17T00:04:06.163Z",
// "object_id": "89436997a794439ab47999701e60392e",
// "object_owner": "shippotle@goshippo.com",
// "status": "SUCCESS",
// "address_from": {
// "object_id": "0943ae4e373e4120a99c337e496dcce8",
// "validation_results": {},
// "is_complete": true,
// "company": "",
// "street_no": "",
// "name": "Mr. Hippo",
// "street1": "215 Clayton St.",
// "street2": "",
// "city": "San Francisco",
// "state": "CA",
// "zip": "94117",
// "country": "US",
// "phone": "+15553419393",
// "email": "support@goshippo.com",
// "is_residential": null
// },
// "address_to": {
// "object_id": "4c7185d353764d0985a6a7825aed8ffb",
// "validation_results": {},
// "is_complete": true,
// "name": "Mrs. Hippo",
// "street1": "200 University Ave W",
// "city": "Waterloo",
// "state": "ON",
// "zip": "N2L 3G1",
// "country": "CA",
// "phone": "+1 555 341 9393",
// "email": "support@goshippo.com",
// "is_residential": false
// },
// "address_return": null,
// "parcels": [
// {
// "object_id": "ec952343dd4843c39b42aca620471fd5",
// "object_created": "2013-12-01T06:24:21.121Z",
// "object_updated": "2013-12-01T06:24:21.121Z",
// "object_owner": "shippotle@goshippo.com",
// "template": null,
// "length": "5",
// "width": "5",
// "height": "5",
// "distance_unit": "in",
// "weight": "2",
// "mass_unit": "lb",
// "value_amount": null,
// "value_currency": null,
// "metadata": "",
// "line_items": [
// ],
// "test": true
// }
// ],
// "shipment_date": "2013-12-03T12:00:00Z",
// "extra": {
// "insurance": {
// "currency": "",
// "amount": ""
// },
// "reference_1": "",
// "reference_2": ""
// },
// "customs_declaration": "b741b99f95e841639b54272834bc478c",
// "rates": [
// {
// "object_created": "2014-07-17T00:04:06.263Z",
// "object_id": "545ab0a1a6ea4c9f9adb2512a57d6d8b",
// "object_owner": "shippotle@goshippo.com",
// "shipment": "89436997a794439ab47999701e60392e",
// "attributes": [
// ],
// "amount": "5.50",
// "currency": "USD",
// "amount_local": "5.50",
// "currency_local": "USD",
// "provider": "USPS",
// "provider_image_75": "https://cdn2.goshippo.com/providers/75/USPS.png",
// "provider_image_200": "https://cdn2.goshippo.com/providers/200/USPS.png",
// "servicelevel": {
// "name": "Priority Mail",
// "token": "usps_priority",
// "terms": ""
// },
// "days": 2,
// "arrives_by": null,
// "duration_terms": "Delivery in 1 to 3 business days.",
// "messages": [
// ],
// "carrier_account": "078870331023437cb917f5187429b093",
// "test": false,
// "zone": 1
// },
// ...
// ],
// "carrier_accounts": [
// ],
// "messages": [
// ],
// "metadata": "Customer ID 123456"
// }
// Sample code for parsing the JSON response...
// Use the following online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
NSString *template = 0;
NSString *length = 0;
NSString *width = 0;
NSString *height = 0;
NSString *distance_unit = 0;
NSString *weight = 0;
NSString *mass_unit = 0;
NSString *value_amount = 0;
NSString *value_currency = 0;
BOOL test;
int j;
int count_j;
int intVal;
NSString *object_created = [jResp StringOf: @"object_created"];
NSString *object_updated = [jResp StringOf: @"object_updated"];
NSString *object_id = [jResp StringOf: @"object_id"];
NSString *object_owner = [jResp StringOf: @"object_owner"];
NSString *status = [jResp StringOf: @"status"];
NSString *address_fromObject_id = [jResp StringOf: @"address_from.object_id"];
BOOL address_fromIs_complete = [jResp BoolOf: @"address_from.is_complete"];
NSString *address_fromCompany = [jResp StringOf: @"address_from.company"];
NSString *address_fromStreet_no = [jResp StringOf: @"address_from.street_no"];
NSString *address_fromName = [jResp StringOf: @"address_from.name"];
NSString *address_fromStreet1 = [jResp StringOf: @"address_from.street1"];
NSString *address_fromStreet2 = [jResp StringOf: @"address_from.street2"];
NSString *address_fromCity = [jResp StringOf: @"address_from.city"];
NSString *address_fromState = [jResp StringOf: @"address_from.state"];
NSString *address_fromZip = [jResp StringOf: @"address_from.zip"];
NSString *address_fromCountry = [jResp StringOf: @"address_from.country"];
NSString *address_fromPhone = [jResp StringOf: @"address_from.phone"];
NSString *address_fromEmail = [jResp StringOf: @"address_from.email"];
NSString *address_fromIs_residential = [jResp StringOf: @"address_from.is_residential"];
NSString *address_toObject_id = [jResp StringOf: @"address_to.object_id"];
BOOL address_toIs_complete = [jResp BoolOf: @"address_to.is_complete"];
NSString *address_toName = [jResp StringOf: @"address_to.name"];
NSString *address_toStreet1 = [jResp StringOf: @"address_to.street1"];
NSString *address_toCity = [jResp StringOf: @"address_to.city"];
NSString *address_toState = [jResp StringOf: @"address_to.state"];
NSString *address_toZip = [jResp StringOf: @"address_to.zip"];
NSString *address_toCountry = [jResp StringOf: @"address_to.country"];
NSString *address_toPhone = [jResp StringOf: @"address_to.phone"];
NSString *address_toEmail = [jResp StringOf: @"address_to.email"];
BOOL address_toIs_residential = [jResp BoolOf: @"address_to.is_residential"];
NSString *address_return = [jResp StringOf: @"address_return"];
NSString *shipment_date = [jResp StringOf: @"shipment_date"];
NSString *extraInsuranceCurrency = [jResp StringOf: @"extra.insurance.currency"];
NSString *extraInsuranceAmount = [jResp StringOf: @"extra.insurance.amount"];
NSString *extraReference_1 = [jResp StringOf: @"extra.reference_1"];
NSString *extraReference_2 = [jResp StringOf: @"extra.reference_2"];
NSString *customs_declaration = [jResp StringOf: @"customs_declaration"];
NSString *metadata = [jResp StringOf: @"metadata"];
int i = 0;
int count_i = [[jResp SizeOfArray: @"parcels"] intValue];
while (i < count_i) {
jResp.I = [NSNumber numberWithInt: i];
object_id = [jResp StringOf: @"parcels[i].object_id"];
object_created = [jResp StringOf: @"parcels[i].object_created"];
object_updated = [jResp StringOf: @"parcels[i].object_updated"];
object_owner = [jResp StringOf: @"parcels[i].object_owner"];
template = [jResp StringOf: @"parcels[i].template"];
length = [jResp StringOf: @"parcels[i].length"];
width = [jResp StringOf: @"parcels[i].width"];
height = [jResp StringOf: @"parcels[i].height"];
distance_unit = [jResp StringOf: @"parcels[i].distance_unit"];
weight = [jResp StringOf: @"parcels[i].weight"];
mass_unit = [jResp StringOf: @"parcels[i].mass_unit"];
value_amount = [jResp StringOf: @"parcels[i].value_amount"];
value_currency = [jResp StringOf: @"parcels[i].value_currency"];
metadata = [jResp StringOf: @"parcels[i].metadata"];
test = [jResp BoolOf: @"parcels[i].test"];
j = 0;
count_j = [[jResp SizeOfArray: @"parcels[i].line_items"] intValue];
while (j < count_j) {
jResp.J = [NSNumber numberWithInt: j];
j = j + 1;
}
i = i + 1;
}
i = 0;
count_i = [[jResp SizeOfArray: @"rates"] intValue];
while (i < count_i) {
jResp.I = [NSNumber numberWithInt: i];
intVal = [[jResp IntOf: @"rates[i]"] intValue];
j = 0;
count_j = [[jResp SizeOfArray: @"rates[i].attributes"] intValue];
while (j < count_j) {
jResp.J = [NSNumber numberWithInt: j];
j = j + 1;
}
j = 0;
count_j = [[jResp SizeOfArray: @"rates[i].messages"] intValue];
while (j < count_j) {
jResp.J = [NSNumber numberWithInt: j];
j = j + 1;
}
i = i + 1;
}
i = 0;
count_i = [[jResp SizeOfArray: @"carrier_accounts"] intValue];
while (i < count_i) {
jResp.I = [NSNumber numberWithInt: i];
i = i + 1;
}
i = 0;
count_i = [[jResp SizeOfArray: @"messages"] intValue];
while (i < count_i) {
jResp.I = [NSNumber numberWithInt: i];
i = i + 1;
}