Sample code for 30+ languages & platforms
Unicode 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 Unicode C++ Downloads

Unicode C++
#include <CkHttpW.h>
#include <CkJsonObjectW.h>
#include <CkHttpResponseW.h>
#include <CkStringBuilderW.h>

void ChilkatSample(void)
    {
    bool success = false;

    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    CkHttpW http;

    // 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"
    //     }
    //   ]
    // }

    CkJsonObjectW json;
    json.UpdateString(L"default_carrier_account",L"078870331023437cb917f5187429b093");
    json.UpdateString(L"default_servicelevel_token",L"usps_priority");
    json.UpdateString(L"label_filetype",L"PDF_4x6");
    json.UpdateString(L"metadata",L"BATCH #170");
    json.UpdateString(L"batch_shipments[0].shipment.address_from.name",L"Mr Hippo");
    json.UpdateString(L"batch_shipments[0].shipment.address_from.street1",L"965 Mission St");
    json.UpdateString(L"batch_shipments[0].shipment.address_from.street2",L"Ste 201");
    json.UpdateString(L"batch_shipments[0].shipment.address_from.city",L"San Francisco");
    json.UpdateString(L"batch_shipments[0].shipment.address_from.state",L"CA");
    json.UpdateString(L"batch_shipments[0].shipment.address_from.zip",L"94103");
    json.UpdateString(L"batch_shipments[0].shipment.address_from.country",L"US");
    json.UpdateString(L"batch_shipments[0].shipment.address_from.phone",L"4151234567");
    json.UpdateString(L"batch_shipments[0].shipment.address_from.email",L"mrhippo@goshippo.com");
    json.UpdateString(L"batch_shipments[0].shipment.address_to.name",L"Mrs Hippo");
    json.UpdateString(L"batch_shipments[0].shipment.address_to.company",L"");
    json.UpdateString(L"batch_shipments[0].shipment.address_to.street1",L"Broadway 1");
    json.UpdateString(L"batch_shipments[0].shipment.address_to.street2",L"");
    json.UpdateString(L"batch_shipments[0].shipment.address_to.city",L"New York");
    json.UpdateString(L"batch_shipments[0].shipment.address_to.state",L"NY");
    json.UpdateString(L"batch_shipments[0].shipment.address_to.zip",L"10007");
    json.UpdateString(L"batch_shipments[0].shipment.address_to.country",L"US");
    json.UpdateString(L"batch_shipments[0].shipment.address_to.phone",L"4151234567");
    json.UpdateString(L"batch_shipments[0].shipment.address_to.email",L"mrshippo@goshippo.com");
    json.UpdateString(L"batch_shipments[0].shipment.parcels[0].length",L"5");
    json.UpdateString(L"batch_shipments[0].shipment.parcels[0].width",L"5");
    json.UpdateString(L"batch_shipments[0].shipment.parcels[0].height",L"5");
    json.UpdateString(L"batch_shipments[0].shipment.parcels[0].distance_unit",L"in");
    json.UpdateString(L"batch_shipments[0].shipment.parcels[0].weight",L"2");
    json.UpdateString(L"batch_shipments[0].shipment.parcels[0].mass_unit",L"oz");
    json.UpdateString(L"batch_shipments[1].shipment.address_from.name",L"Mr Hippo");
    json.UpdateString(L"batch_shipments[1].shipment.address_from.street1",L"1092 Indian Summer Ct");
    json.UpdateString(L"batch_shipments[1].shipment.address_from.city",L"San Jose");
    json.UpdateString(L"batch_shipments[1].shipment.address_from.state",L"CA");
    json.UpdateString(L"batch_shipments[1].shipment.address_from.zip",L"95122");
    json.UpdateString(L"batch_shipments[1].shipment.address_from.country",L"US");
    json.UpdateString(L"batch_shipments[1].shipment.address_from.phone",L"4151234567");
    json.UpdateString(L"batch_shipments[1].shipment.address_from.email",L"mrhippo@goshippo.com");
    json.UpdateString(L"batch_shipments[1].shipment.address_to.name",L"Mrs Hippo");
    json.UpdateString(L"batch_shipments[1].shipment.address_to.company",L"");
    json.UpdateString(L"batch_shipments[1].shipment.address_to.street1",L"Broadway 1");
    json.UpdateString(L"batch_shipments[1].shipment.address_to.street2",L"");
    json.UpdateString(L"batch_shipments[1].shipment.address_to.city",L"New York");
    json.UpdateString(L"batch_shipments[1].shipment.address_to.state",L"NY");
    json.UpdateString(L"batch_shipments[1].shipment.address_to.zip",L"10007");
    json.UpdateString(L"batch_shipments[1].shipment.address_to.country",L"US");
    json.UpdateString(L"batch_shipments[1].shipment.address_to.phone",L"4151234567");
    json.UpdateString(L"batch_shipments[1].shipment.address_to.email",L"mrshippo@goshippo.com");
    json.UpdateString(L"batch_shipments[1].shipment.parcels[0].length",L"5");
    json.UpdateString(L"batch_shipments[1].shipment.parcels[0].width",L"5");
    json.UpdateString(L"batch_shipments[1].shipment.parcels[0].height",L"5");
    json.UpdateString(L"batch_shipments[1].shipment.parcels[0].distance_unit",L"in");
    json.UpdateString(L"batch_shipments[1].shipment.parcels[0].weight",L"20");
    json.UpdateString(L"batch_shipments[1].shipment.parcels[0].mass_unit",L"lb");
    json.UpdateString(L"batch_shipments[1].carrier_account",L"a4391cd4ab974f478f55dc08b5c8e3b3");
    json.UpdateString(L"batch_shipments[1].servicelevel_token",L"fedex_2_day");

    http.SetRequestHeader(L"Authorization",L"ShippoToken shippo_test_3af5d574e2f845d30efcaf9b2f47d9c2aef4807a");
    http.SetRequestHeader(L"Content-Type",L"application/json");

    CkHttpResponseW resp;
    success = http.HttpJson(L"POST",L"https://api.goshippo.com/batches/",json,L"application/json",resp);
    if (success == false) {
        wprintf(L"%s\n",http.lastErrorText());
        return;
    }

    CkStringBuilderW sbResponseBody;
    resp.GetBodySb(sbResponseBody);
    CkJsonObjectW jResp;
    jResp.LoadSb(sbResponseBody);
    jResp.put_EmitCompact(false);

    wprintf(L"Response Body:\n");
    wprintf(L"%s\n",jResp.emit());

    int respStatusCode = resp.get_StatusCode();
    wprintf(L"Response Status Code = %d\n",respStatusCode);
    if (respStatusCode >= 400) {
        wprintf(L"Response Header:\n");
        wprintf(L"%s\n",resp.header());
        wprintf(L"Failed.\n");
        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.

    const wchar_t *object_id = jResp.stringOf(L"object_id");
    const wchar_t *object_owner = jResp.stringOf(L"object_owner");
    const wchar_t *status = jResp.stringOf(L"status");
    const wchar_t *object_created = jResp.stringOf(L"object_created");
    const wchar_t *object_updated = jResp.stringOf(L"object_updated");
    const wchar_t *metadata = jResp.stringOf(L"metadata");
    const wchar_t *default_carrier_account = jResp.stringOf(L"default_carrier_account");
    const wchar_t *default_servicelevel_token = jResp.stringOf(L"default_servicelevel_token");
    const wchar_t *label_filetype = jResp.stringOf(L"label_filetype");
    const wchar_t *batch_shipmentsNext = jResp.stringOf(L"batch_shipments.next");
    const wchar_t *batch_shipmentsPrevious = jResp.stringOf(L"batch_shipments.previous");
    int object_resultsPurchase_succeeded = jResp.IntOf(L"object_results.purchase_succeeded");
    int object_resultsPurchase_failed = jResp.IntOf(L"object_results.purchase_failed");
    int object_resultsCreation_failed = jResp.IntOf(L"object_results.creation_failed");
    int object_resultsCreation_succeeded = jResp.IntOf(L"object_results.creation_succeeded");
    int i = 0;
    int count_i = jResp.SizeOfArray(L"batch_shipments.results");
    while (i < count_i) {
        jResp.put_I(i);
        i = i + 1;
    }

    i = 0;
    count_i = jResp.SizeOfArray(L"label_url");
    while (i < count_i) {
        jResp.put_I(i);
        i = i + 1;
    }
    }