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

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

    CkJsonObjectW json;
    json.UpdateString(L"contents_type",L"MERCHANDISE");
    json.UpdateString(L"non_delivery_option",L"RETURN");
    json.UpdateBool(L"certify",true);
    json.UpdateString(L"certify_signer",L"Simon Kreuz");
    json.UpdateString(L"incoterm",L"DDU");
    json.UpdateString(L"items[0].description",L"T-shirt");
    json.UpdateInt(L"items[0].quantity",20);
    json.UpdateString(L"items[0].net_weight",L"5");
    json.UpdateString(L"items[0].mass_unit",L"lb");
    json.UpdateString(L"items[0].value_amount",L"200");
    json.UpdateString(L"items[0].value_currency",L"USD");
    json.UpdateString(L"items[0].tariff_number",L"");
    json.UpdateString(L"items[0].origin_country",L"US");

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

    CkHttpResponseW resp;
    success = http.HttpJson(L"POST",L"https://api.goshippo.com/customs/declarations/",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_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

    // 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 *template = 0;
    const wchar_t *length = 0;
    const wchar_t *width = 0;
    const wchar_t *height = 0;
    const wchar_t *distance_unit = 0;
    const wchar_t *weight = 0;
    const wchar_t *mass_unit = 0;
    const wchar_t *value_amount = 0;
    const wchar_t *value_currency = 0;
    bool test;
    int j;
    int count_j;
    int intVal;

    const wchar_t *object_created = jResp.stringOf(L"object_created");
    const wchar_t *object_updated = jResp.stringOf(L"object_updated");
    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 *address_fromObject_id = jResp.stringOf(L"address_from.object_id");
    bool address_fromIs_complete = jResp.BoolOf(L"address_from.is_complete");
    const wchar_t *address_fromCompany = jResp.stringOf(L"address_from.company");
    const wchar_t *address_fromStreet_no = jResp.stringOf(L"address_from.street_no");
    const wchar_t *address_fromName = jResp.stringOf(L"address_from.name");
    const wchar_t *address_fromStreet1 = jResp.stringOf(L"address_from.street1");
    const wchar_t *address_fromStreet2 = jResp.stringOf(L"address_from.street2");
    const wchar_t *address_fromCity = jResp.stringOf(L"address_from.city");
    const wchar_t *address_fromState = jResp.stringOf(L"address_from.state");
    const wchar_t *address_fromZip = jResp.stringOf(L"address_from.zip");
    const wchar_t *address_fromCountry = jResp.stringOf(L"address_from.country");
    const wchar_t *address_fromPhone = jResp.stringOf(L"address_from.phone");
    const wchar_t *address_fromEmail = jResp.stringOf(L"address_from.email");
    const wchar_t *address_fromIs_residential = jResp.stringOf(L"address_from.is_residential");
    const wchar_t *address_toObject_id = jResp.stringOf(L"address_to.object_id");
    bool address_toIs_complete = jResp.BoolOf(L"address_to.is_complete");
    const wchar_t *address_toName = jResp.stringOf(L"address_to.name");
    const wchar_t *address_toStreet1 = jResp.stringOf(L"address_to.street1");
    const wchar_t *address_toCity = jResp.stringOf(L"address_to.city");
    const wchar_t *address_toState = jResp.stringOf(L"address_to.state");
    const wchar_t *address_toZip = jResp.stringOf(L"address_to.zip");
    const wchar_t *address_toCountry = jResp.stringOf(L"address_to.country");
    const wchar_t *address_toPhone = jResp.stringOf(L"address_to.phone");
    const wchar_t *address_toEmail = jResp.stringOf(L"address_to.email");
    bool address_toIs_residential = jResp.BoolOf(L"address_to.is_residential");
    const wchar_t *address_return = jResp.stringOf(L"address_return");
    const wchar_t *shipment_date = jResp.stringOf(L"shipment_date");
    const wchar_t *extraInsuranceCurrency = jResp.stringOf(L"extra.insurance.currency");
    const wchar_t *extraInsuranceAmount = jResp.stringOf(L"extra.insurance.amount");
    const wchar_t *extraReference_1 = jResp.stringOf(L"extra.reference_1");
    const wchar_t *extraReference_2 = jResp.stringOf(L"extra.reference_2");
    const wchar_t *customs_declaration = jResp.stringOf(L"customs_declaration");
    const wchar_t *metadata = jResp.stringOf(L"metadata");
    int i = 0;
    int count_i = jResp.SizeOfArray(L"parcels");
    while (i < count_i) {
        jResp.put_I(i);
        object_id = jResp.stringOf(L"parcels[i].object_id");
        object_created = jResp.stringOf(L"parcels[i].object_created");
        object_updated = jResp.stringOf(L"parcels[i].object_updated");
        object_owner = jResp.stringOf(L"parcels[i].object_owner");
        template = jResp.stringOf(L"parcels[i].template");
        length = jResp.stringOf(L"parcels[i].length");
        width = jResp.stringOf(L"parcels[i].width");
        height = jResp.stringOf(L"parcels[i].height");
        distance_unit = jResp.stringOf(L"parcels[i].distance_unit");
        weight = jResp.stringOf(L"parcels[i].weight");
        mass_unit = jResp.stringOf(L"parcels[i].mass_unit");
        value_amount = jResp.stringOf(L"parcels[i].value_amount");
        value_currency = jResp.stringOf(L"parcels[i].value_currency");
        metadata = jResp.stringOf(L"parcels[i].metadata");
        test = jResp.BoolOf(L"parcels[i].test");
        j = 0;
        count_j = jResp.SizeOfArray(L"parcels[i].line_items");
        while (j < count_j) {
            jResp.put_J(j);
            j = j + 1;
        }

        i = i + 1;
    }

    i = 0;
    count_i = jResp.SizeOfArray(L"rates");
    while (i < count_i) {
        jResp.put_I(i);
        intVal = jResp.IntOf(L"rates[i]");
        j = 0;
        count_j = jResp.SizeOfArray(L"rates[i].attributes");
        while (j < count_j) {
            jResp.put_J(j);
            j = j + 1;
        }

        j = 0;
        count_j = jResp.SizeOfArray(L"rates[i].messages");
        while (j < count_j) {
            jResp.put_J(j);
            j = j + 1;
        }

        i = i + 1;
    }

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

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