C
C
Shippo Create a Batch
See more Shippo Examples
Demonstrates how to create and purchase up to 10,000 shipments in a single API request.Chilkat C Downloads
#include <C_CkHttp.h>
#include <C_CkJsonObject.h>
#include <C_CkHttpResponse.h>
#include <C_CkStringBuilder.h>
void ChilkatSample(void)
{
BOOL success;
HCkHttp http;
HCkJsonObject json;
HCkHttpResponse resp;
HCkStringBuilder sbResponseBody;
HCkJsonObject jResp;
int respStatusCode;
const char *object_id;
const char *object_owner;
const char *status;
const char *object_created;
const char *object_updated;
const char *metadata;
const char *default_carrier_account;
const char *default_servicelevel_token;
const char *label_filetype;
const char *batch_shipmentsNext;
const char *batch_shipmentsPrevious;
int object_resultsPurchase_succeeded;
int object_resultsPurchase_failed;
int object_resultsCreation_failed;
int object_resultsCreation_succeeded;
int i;
int count_i;
success = FALSE;
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
http = CkHttp_Create();
// Use this online tool to generate code from sample JSON:
// Generate Code to Create JSON
// The following JSON is sent in the request body.
// {
// "default_carrier_account": "078870331023437cb917f5187429b093",
// "default_servicelevel_token": "usps_priority",
// "label_filetype": "PDF_4x6",
// "metadata": "BATCH #170",
// "batch_shipments": [
// {
// "shipment": {
// "address_from": {
// "name": "Mr Hippo",
// "street1": "965 Mission St",
// "street2": "Ste 201",
// "city": "San Francisco",
// "state": "CA",
// "zip": "94103",
// "country": "US",
// "phone": "4151234567",
// "email": "mrhippo@goshippo.com"
// },
// "address_to": {
// "name": "Mrs Hippo",
// "company": "",
// "street1": "Broadway 1",
// "street2": "",
// "city": "New York",
// "state": "NY",
// "zip": "10007",
// "country": "US",
// "phone": "4151234567",
// "email": "mrshippo@goshippo.com"
// },
// "parcels": [
// {
// "length": "5",
// "width": "5",
// "height": "5",
// "distance_unit": "in",
// "weight": "2",
// "mass_unit": "oz"
// }
// ]
// }
// },
// {
// "shipment": {
// "address_from": {
// "name": "Mr Hippo",
// "street1": "1092 Indian Summer Ct",
// "city": "San Jose",
// "state": "CA",
// "zip": "95122",
// "country": "US",
// "phone": "4151234567",
// "email": "mrhippo@goshippo.com"
// },
// "address_to": {
// "name": "Mrs Hippo",
// "company": "",
// "street1": "Broadway 1",
// "street2": "",
// "city": "New York",
// "state": "NY",
// "zip": "10007",
// "country": "US",
// "phone": "4151234567",
// "email": "mrshippo@goshippo.com"
// },
// "parcels": [
// {
// "length": "5",
// "width": "5",
// "height": "5",
// "distance_unit": "in",
// "weight": "20",
// "mass_unit": "lb"
// }
// ]
// },
// "carrier_account": "a4391cd4ab974f478f55dc08b5c8e3b3",
// "servicelevel_token": "fedex_2_day"
// }
// ]
// }
json = CkJsonObject_Create();
CkJsonObject_UpdateString(json,"default_carrier_account","078870331023437cb917f5187429b093");
CkJsonObject_UpdateString(json,"default_servicelevel_token","usps_priority");
CkJsonObject_UpdateString(json,"label_filetype","PDF_4x6");
CkJsonObject_UpdateString(json,"metadata","BATCH #170");
CkJsonObject_UpdateString(json,"batch_shipments[0].shipment.address_from.name","Mr Hippo");
CkJsonObject_UpdateString(json,"batch_shipments[0].shipment.address_from.street1","965 Mission St");
CkJsonObject_UpdateString(json,"batch_shipments[0].shipment.address_from.street2","Ste 201");
CkJsonObject_UpdateString(json,"batch_shipments[0].shipment.address_from.city","San Francisco");
CkJsonObject_UpdateString(json,"batch_shipments[0].shipment.address_from.state","CA");
CkJsonObject_UpdateString(json,"batch_shipments[0].shipment.address_from.zip","94103");
CkJsonObject_UpdateString(json,"batch_shipments[0].shipment.address_from.country","US");
CkJsonObject_UpdateString(json,"batch_shipments[0].shipment.address_from.phone","4151234567");
CkJsonObject_UpdateString(json,"batch_shipments[0].shipment.address_from.email","mrhippo@goshippo.com");
CkJsonObject_UpdateString(json,"batch_shipments[0].shipment.address_to.name","Mrs Hippo");
CkJsonObject_UpdateString(json,"batch_shipments[0].shipment.address_to.company","");
CkJsonObject_UpdateString(json,"batch_shipments[0].shipment.address_to.street1","Broadway 1");
CkJsonObject_UpdateString(json,"batch_shipments[0].shipment.address_to.street2","");
CkJsonObject_UpdateString(json,"batch_shipments[0].shipment.address_to.city","New York");
CkJsonObject_UpdateString(json,"batch_shipments[0].shipment.address_to.state","NY");
CkJsonObject_UpdateString(json,"batch_shipments[0].shipment.address_to.zip","10007");
CkJsonObject_UpdateString(json,"batch_shipments[0].shipment.address_to.country","US");
CkJsonObject_UpdateString(json,"batch_shipments[0].shipment.address_to.phone","4151234567");
CkJsonObject_UpdateString(json,"batch_shipments[0].shipment.address_to.email","mrshippo@goshippo.com");
CkJsonObject_UpdateString(json,"batch_shipments[0].shipment.parcels[0].length","5");
CkJsonObject_UpdateString(json,"batch_shipments[0].shipment.parcels[0].width","5");
CkJsonObject_UpdateString(json,"batch_shipments[0].shipment.parcels[0].height","5");
CkJsonObject_UpdateString(json,"batch_shipments[0].shipment.parcels[0].distance_unit","in");
CkJsonObject_UpdateString(json,"batch_shipments[0].shipment.parcels[0].weight","2");
CkJsonObject_UpdateString(json,"batch_shipments[0].shipment.parcels[0].mass_unit","oz");
CkJsonObject_UpdateString(json,"batch_shipments[1].shipment.address_from.name","Mr Hippo");
CkJsonObject_UpdateString(json,"batch_shipments[1].shipment.address_from.street1","1092 Indian Summer Ct");
CkJsonObject_UpdateString(json,"batch_shipments[1].shipment.address_from.city","San Jose");
CkJsonObject_UpdateString(json,"batch_shipments[1].shipment.address_from.state","CA");
CkJsonObject_UpdateString(json,"batch_shipments[1].shipment.address_from.zip","95122");
CkJsonObject_UpdateString(json,"batch_shipments[1].shipment.address_from.country","US");
CkJsonObject_UpdateString(json,"batch_shipments[1].shipment.address_from.phone","4151234567");
CkJsonObject_UpdateString(json,"batch_shipments[1].shipment.address_from.email","mrhippo@goshippo.com");
CkJsonObject_UpdateString(json,"batch_shipments[1].shipment.address_to.name","Mrs Hippo");
CkJsonObject_UpdateString(json,"batch_shipments[1].shipment.address_to.company","");
CkJsonObject_UpdateString(json,"batch_shipments[1].shipment.address_to.street1","Broadway 1");
CkJsonObject_UpdateString(json,"batch_shipments[1].shipment.address_to.street2","");
CkJsonObject_UpdateString(json,"batch_shipments[1].shipment.address_to.city","New York");
CkJsonObject_UpdateString(json,"batch_shipments[1].shipment.address_to.state","NY");
CkJsonObject_UpdateString(json,"batch_shipments[1].shipment.address_to.zip","10007");
CkJsonObject_UpdateString(json,"batch_shipments[1].shipment.address_to.country","US");
CkJsonObject_UpdateString(json,"batch_shipments[1].shipment.address_to.phone","4151234567");
CkJsonObject_UpdateString(json,"batch_shipments[1].shipment.address_to.email","mrshippo@goshippo.com");
CkJsonObject_UpdateString(json,"batch_shipments[1].shipment.parcels[0].length","5");
CkJsonObject_UpdateString(json,"batch_shipments[1].shipment.parcels[0].width","5");
CkJsonObject_UpdateString(json,"batch_shipments[1].shipment.parcels[0].height","5");
CkJsonObject_UpdateString(json,"batch_shipments[1].shipment.parcels[0].distance_unit","in");
CkJsonObject_UpdateString(json,"batch_shipments[1].shipment.parcels[0].weight","20");
CkJsonObject_UpdateString(json,"batch_shipments[1].shipment.parcels[0].mass_unit","lb");
CkJsonObject_UpdateString(json,"batch_shipments[1].carrier_account","a4391cd4ab974f478f55dc08b5c8e3b3");
CkJsonObject_UpdateString(json,"batch_shipments[1].servicelevel_token","fedex_2_day");
CkHttp_SetRequestHeader(http,"Authorization","ShippoToken shippo_test_3af5d574e2f845d30efcaf9b2f47d9c2aef4807a");
CkHttp_SetRequestHeader(http,"Content-Type","application/json");
resp = CkHttpResponse_Create();
success = CkHttp_HttpJson(http,"POST","https://api.goshippo.com/batches/",json,"application/json",resp);
if (success == FALSE) {
printf("%s\n",CkHttp_lastErrorText(http));
CkHttp_Dispose(http);
CkJsonObject_Dispose(json);
CkHttpResponse_Dispose(resp);
return;
}
sbResponseBody = CkStringBuilder_Create();
CkHttpResponse_GetBodySb(resp,sbResponseBody);
jResp = CkJsonObject_Create();
CkJsonObject_LoadSb(jResp,sbResponseBody);
CkJsonObject_putEmitCompact(jResp,FALSE);
printf("Response Body:\n");
printf("%s\n",CkJsonObject_emit(jResp));
respStatusCode = CkHttpResponse_getStatusCode(resp);
printf("Response Status Code = %d\n",respStatusCode);
if (respStatusCode >= 400) {
printf("Response Header:\n");
printf("%s\n",CkHttpResponse_header(resp));
printf("Failed.\n");
CkHttp_Dispose(http);
CkJsonObject_Dispose(json);
CkHttpResponse_Dispose(resp);
CkStringBuilder_Dispose(sbResponseBody);
CkJsonObject_Dispose(jResp);
return;
}
// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)
// {
// "object_id": "a015eb693cca465dbb6523ce6d2e3c65",
// "object_owner": "admin",
// "status": "VALIDATING",
// "object_created": "2016-09-12T15:25:43.465Z",
// "object_updated": "2016-09-12T15:25:43.465Z",
// "metadata": "BATCH #170",
// "default_carrier_account": "078870331023437cb917f5187429b093",
// "default_servicelevel_token": "usps_priority",
// "label_filetype": "PDF_4x6",
// "batch_shipments": {
// "next": null,
// "previous": null,
// "results": [
// ]
// },
// "object_results": {
// "purchase_succeeded": 0,
// "purchase_failed": 0,
// "creation_failed": 0,
// "creation_succeeded": 0
// },
// "label_url": [
// ]
// }
// Sample code for parsing the JSON response...
// Use the following online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
// Chilkat functions returning "const char *" return a pointer to temporary internal memory owned and managed by Chilkat.
// See this example explaining how this memory should be used: const char * functions.
object_id = CkJsonObject_stringOf(jResp,"object_id");
object_owner = CkJsonObject_stringOf(jResp,"object_owner");
status = CkJsonObject_stringOf(jResp,"status");
object_created = CkJsonObject_stringOf(jResp,"object_created");
object_updated = CkJsonObject_stringOf(jResp,"object_updated");
metadata = CkJsonObject_stringOf(jResp,"metadata");
default_carrier_account = CkJsonObject_stringOf(jResp,"default_carrier_account");
default_servicelevel_token = CkJsonObject_stringOf(jResp,"default_servicelevel_token");
label_filetype = CkJsonObject_stringOf(jResp,"label_filetype");
batch_shipmentsNext = CkJsonObject_stringOf(jResp,"batch_shipments.next");
batch_shipmentsPrevious = CkJsonObject_stringOf(jResp,"batch_shipments.previous");
object_resultsPurchase_succeeded = CkJsonObject_IntOf(jResp,"object_results.purchase_succeeded");
object_resultsPurchase_failed = CkJsonObject_IntOf(jResp,"object_results.purchase_failed");
object_resultsCreation_failed = CkJsonObject_IntOf(jResp,"object_results.creation_failed");
object_resultsCreation_succeeded = CkJsonObject_IntOf(jResp,"object_results.creation_succeeded");
i = 0;
count_i = CkJsonObject_SizeOfArray(jResp,"batch_shipments.results");
while (i < count_i) {
CkJsonObject_putI(jResp,i);
i = i + 1;
}
i = 0;
count_i = CkJsonObject_SizeOfArray(jResp,"label_url");
while (i < count_i) {
CkJsonObject_putI(jResp,i);
i = i + 1;
}
CkHttp_Dispose(http);
CkJsonObject_Dispose(json);
CkHttpResponse_Dispose(resp);
CkStringBuilder_Dispose(sbResponseBody);
CkJsonObject_Dispose(jResp);
}