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

C
#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 *template;
    const char *length;
    const char *width;
    const char *height;
    const char *distance_unit;
    const char *weight;
    const char *mass_unit;
    const char *value_amount;
    const char *value_currency;
    BOOL test;
    int j;
    int count_j;
    int intVal;
    const char *object_created;
    const char *object_updated;
    const char *object_id;
    const char *object_owner;
    const char *status;
    const char *address_fromObject_id;
    BOOL address_fromIs_complete;
    const char *address_fromCompany;
    const char *address_fromStreet_no;
    const char *address_fromName;
    const char *address_fromStreet1;
    const char *address_fromStreet2;
    const char *address_fromCity;
    const char *address_fromState;
    const char *address_fromZip;
    const char *address_fromCountry;
    const char *address_fromPhone;
    const char *address_fromEmail;
    const char *address_fromIs_residential;
    const char *address_toObject_id;
    BOOL address_toIs_complete;
    const char *address_toName;
    const char *address_toStreet1;
    const char *address_toCity;
    const char *address_toState;
    const char *address_toZip;
    const char *address_toCountry;
    const char *address_toPhone;
    const char *address_toEmail;
    BOOL address_toIs_residential;
    const char *address_return;
    const char *shipment_date;
    const char *extraInsuranceCurrency;
    const char *extraInsuranceAmount;
    const char *extraReference_1;
    const char *extraReference_2;
    const char *customs_declaration;
    const char *metadata;
    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();

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

    json = CkJsonObject_Create();
    CkJsonObject_UpdateString(json,"contents_type","MERCHANDISE");
    CkJsonObject_UpdateString(json,"non_delivery_option","RETURN");
    CkJsonObject_UpdateBool(json,"certify",TRUE);
    CkJsonObject_UpdateString(json,"certify_signer","Simon Kreuz");
    CkJsonObject_UpdateString(json,"incoterm","DDU");
    CkJsonObject_UpdateString(json,"items[0].description","T-shirt");
    CkJsonObject_UpdateInt(json,"items[0].quantity",20);
    CkJsonObject_UpdateString(json,"items[0].net_weight","5");
    CkJsonObject_UpdateString(json,"items[0].mass_unit","lb");
    CkJsonObject_UpdateString(json,"items[0].value_amount","200");
    CkJsonObject_UpdateString(json,"items[0].value_currency","USD");
    CkJsonObject_UpdateString(json,"items[0].tariff_number","");
    CkJsonObject_UpdateString(json,"items[0].origin_country","US");

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

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

    object_created = CkJsonObject_stringOf(jResp,"object_created");
    object_updated = CkJsonObject_stringOf(jResp,"object_updated");
    object_id = CkJsonObject_stringOf(jResp,"object_id");
    object_owner = CkJsonObject_stringOf(jResp,"object_owner");
    status = CkJsonObject_stringOf(jResp,"status");
    address_fromObject_id = CkJsonObject_stringOf(jResp,"address_from.object_id");
    address_fromIs_complete = CkJsonObject_BoolOf(jResp,"address_from.is_complete");
    address_fromCompany = CkJsonObject_stringOf(jResp,"address_from.company");
    address_fromStreet_no = CkJsonObject_stringOf(jResp,"address_from.street_no");
    address_fromName = CkJsonObject_stringOf(jResp,"address_from.name");
    address_fromStreet1 = CkJsonObject_stringOf(jResp,"address_from.street1");
    address_fromStreet2 = CkJsonObject_stringOf(jResp,"address_from.street2");
    address_fromCity = CkJsonObject_stringOf(jResp,"address_from.city");
    address_fromState = CkJsonObject_stringOf(jResp,"address_from.state");
    address_fromZip = CkJsonObject_stringOf(jResp,"address_from.zip");
    address_fromCountry = CkJsonObject_stringOf(jResp,"address_from.country");
    address_fromPhone = CkJsonObject_stringOf(jResp,"address_from.phone");
    address_fromEmail = CkJsonObject_stringOf(jResp,"address_from.email");
    address_fromIs_residential = CkJsonObject_stringOf(jResp,"address_from.is_residential");
    address_toObject_id = CkJsonObject_stringOf(jResp,"address_to.object_id");
    address_toIs_complete = CkJsonObject_BoolOf(jResp,"address_to.is_complete");
    address_toName = CkJsonObject_stringOf(jResp,"address_to.name");
    address_toStreet1 = CkJsonObject_stringOf(jResp,"address_to.street1");
    address_toCity = CkJsonObject_stringOf(jResp,"address_to.city");
    address_toState = CkJsonObject_stringOf(jResp,"address_to.state");
    address_toZip = CkJsonObject_stringOf(jResp,"address_to.zip");
    address_toCountry = CkJsonObject_stringOf(jResp,"address_to.country");
    address_toPhone = CkJsonObject_stringOf(jResp,"address_to.phone");
    address_toEmail = CkJsonObject_stringOf(jResp,"address_to.email");
    address_toIs_residential = CkJsonObject_BoolOf(jResp,"address_to.is_residential");
    address_return = CkJsonObject_stringOf(jResp,"address_return");
    shipment_date = CkJsonObject_stringOf(jResp,"shipment_date");
    extraInsuranceCurrency = CkJsonObject_stringOf(jResp,"extra.insurance.currency");
    extraInsuranceAmount = CkJsonObject_stringOf(jResp,"extra.insurance.amount");
    extraReference_1 = CkJsonObject_stringOf(jResp,"extra.reference_1");
    extraReference_2 = CkJsonObject_stringOf(jResp,"extra.reference_2");
    customs_declaration = CkJsonObject_stringOf(jResp,"customs_declaration");
    metadata = CkJsonObject_stringOf(jResp,"metadata");
    i = 0;
    count_i = CkJsonObject_SizeOfArray(jResp,"parcels");
    while (i < count_i) {
        CkJsonObject_putI(jResp,i);
        object_id = CkJsonObject_stringOf(jResp,"parcels[i].object_id");
        object_created = CkJsonObject_stringOf(jResp,"parcels[i].object_created");
        object_updated = CkJsonObject_stringOf(jResp,"parcels[i].object_updated");
        object_owner = CkJsonObject_stringOf(jResp,"parcels[i].object_owner");
        template = CkJsonObject_stringOf(jResp,"parcels[i].template");
        length = CkJsonObject_stringOf(jResp,"parcels[i].length");
        width = CkJsonObject_stringOf(jResp,"parcels[i].width");
        height = CkJsonObject_stringOf(jResp,"parcels[i].height");
        distance_unit = CkJsonObject_stringOf(jResp,"parcels[i].distance_unit");
        weight = CkJsonObject_stringOf(jResp,"parcels[i].weight");
        mass_unit = CkJsonObject_stringOf(jResp,"parcels[i].mass_unit");
        value_amount = CkJsonObject_stringOf(jResp,"parcels[i].value_amount");
        value_currency = CkJsonObject_stringOf(jResp,"parcels[i].value_currency");
        metadata = CkJsonObject_stringOf(jResp,"parcels[i].metadata");
        test = CkJsonObject_BoolOf(jResp,"parcels[i].test");
        j = 0;
        count_j = CkJsonObject_SizeOfArray(jResp,"parcels[i].line_items");
        while (j < count_j) {
            CkJsonObject_putJ(jResp,j);
            j = j + 1;
        }

        i = i + 1;
    }

    i = 0;
    count_i = CkJsonObject_SizeOfArray(jResp,"rates");
    while (i < count_i) {
        CkJsonObject_putI(jResp,i);
        intVal = CkJsonObject_IntOf(jResp,"rates[i]");
        j = 0;
        count_j = CkJsonObject_SizeOfArray(jResp,"rates[i].attributes");
        while (j < count_j) {
            CkJsonObject_putJ(jResp,j);
            j = j + 1;
        }

        j = 0;
        count_j = CkJsonObject_SizeOfArray(jResp,"rates[i].messages");
        while (j < count_j) {
            CkJsonObject_putJ(jResp,j);
            j = j + 1;
        }

        i = i + 1;
    }

    i = 0;
    count_i = CkJsonObject_SizeOfArray(jResp,"carrier_accounts");
    while (i < count_i) {
        CkJsonObject_putI(jResp,i);
        i = i + 1;
    }

    i = 0;
    count_i = CkJsonObject_SizeOfArray(jResp,"messages");
    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);

    }