Sample code for 30+ languages & platforms
Unicode C

Shippo Create an Order

See more Shippo Examples

Demonstrates how to create an order with all the information about your order by sending a POST request to the orders endpoint.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkHttpW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkHttpResponseW.h>
#include <C_CkStringBuilderW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkHttpW http;
    HCkJsonObjectW json;
    HCkHttpResponseW resp;
    HCkStringBuilderW sbResponseBody;
    HCkJsonObjectW jResp;
    int respStatusCode;

    success = FALSE;

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

    http = CkHttpW_Create();

    // Implements the following CURL command:

    // curl https://api.goshippo.com/orders/ \
    //     -H "Authorization: ShippoToken <API_Token>" \
    //     -H "Content-Type: application/json"  \
    //     -d '{
    //             "to_address": {
    //                 "city": "San Francisco",
    //                 "company": "Shippo",
    //                 "country": "US",
    //                 "email": "shippotle@goshippo.com",
    //                 "name": "Mr Hippo",
    //                 "phone": "15553419393",
    //                 "state": "CA",
    //                 "street1": "215 Clayton St.",
    //                 "zip": "94117"
    //             },
    //             "line_items": [
    //                 {
    //                     "quantity": 1,
    //                     "sku": "HM-123",
    //                     "title": "Hippo Magazines",
    //                     "total_price": "12.10",
    //                     "currency": "USD",
    //                     "weight": "0.40",
    //                     "weight_unit": "lb"
    //                 }
    //             ],
    //             "placed_at": "2016-09-23T01:28:12Z",
    //             "order_number": "#1068",
    //             "order_status": "PAID",
    //             "shipping_cost": "12.83",
    //             "shipping_cost_currency": "USD",
    //             "shipping_method": "USPS First Class Package",
    //             "subtotal_price": "12.10",
    //             "total_price": "24.93",
    //             "total_tax": "0.00",
    //             "currency": "USD",
    //             "weight": "0.40",
    //             "weight_unit": "lb"
    //         }'

    // Use this online tool to generate code from sample JSON:
    // Generate Code to Create JSON

    // The following JSON is sent in the request body.

    // {
    //   "to_address": {
    //     "city": "San Francisco",
    //     "company": "Shippo",
    //     "country": "US",
    //     "email": "shippotle@goshippo.com",
    //     "name": "Mr Hippo",
    //     "phone": "15553419393",
    //     "state": "CA",
    //     "street1": "215 Clayton St.",
    //     "zip": "94117"
    //   },
    //   "line_items": [
    //     {
    //       "quantity": 1,
    //       "sku": "HM-123",
    //       "title": "Hippo Magazines",
    //       "total_price": "12.10",
    //       "currency": "USD",
    //       "weight": "0.40",
    //       "weight_unit": "lb"
    //     }
    //   ],
    //   "placed_at": "2016-09-23T01:28:12Z",
    //   "order_number": "#1068",
    //   "order_status": "PAID",
    //   "shipping_cost": "12.83",
    //   "shipping_cost_currency": "USD",
    //   "shipping_method": "USPS First Class Package",
    //   "subtotal_price": "12.10",
    //   "total_price": "24.93",
    //   "total_tax": "0.00",
    //   "currency": "USD",
    //   "weight": "0.40",
    //   "weight_unit": "lb"
    // }

    json = CkJsonObjectW_Create();
    CkJsonObjectW_UpdateString(json,L"to_address.city",L"San Francisco");
    CkJsonObjectW_UpdateString(json,L"to_address.company",L"Shippo");
    CkJsonObjectW_UpdateString(json,L"to_address.country",L"US");
    CkJsonObjectW_UpdateString(json,L"to_address.email",L"shippotle@goshippo.com");
    CkJsonObjectW_UpdateString(json,L"to_address.name",L"Mr Hippo");
    CkJsonObjectW_UpdateString(json,L"to_address.phone",L"15553419393");
    CkJsonObjectW_UpdateString(json,L"to_address.state",L"CA");
    CkJsonObjectW_UpdateString(json,L"to_address.street1",L"215 Clayton St.");
    CkJsonObjectW_UpdateString(json,L"to_address.zip",L"94117");
    CkJsonObjectW_UpdateInt(json,L"line_items[0].quantity",1);
    CkJsonObjectW_UpdateString(json,L"line_items[0].sku",L"HM-123");
    CkJsonObjectW_UpdateString(json,L"line_items[0].title",L"Hippo Magazines");
    CkJsonObjectW_UpdateString(json,L"line_items[0].total_price",L"12.10");
    CkJsonObjectW_UpdateString(json,L"line_items[0].currency",L"USD");
    CkJsonObjectW_UpdateString(json,L"line_items[0].weight",L"0.40");
    CkJsonObjectW_UpdateString(json,L"line_items[0].weight_unit",L"lb");
    CkJsonObjectW_UpdateString(json,L"placed_at",L"2016-09-23T01:28:12Z");
    CkJsonObjectW_UpdateString(json,L"order_number",L"#1068");
    CkJsonObjectW_UpdateString(json,L"order_status",L"PAID");
    CkJsonObjectW_UpdateString(json,L"shipping_cost",L"12.83");
    CkJsonObjectW_UpdateString(json,L"shipping_cost_currency",L"USD");
    CkJsonObjectW_UpdateString(json,L"shipping_method",L"USPS First Class Package");
    CkJsonObjectW_UpdateString(json,L"subtotal_price",L"12.10");
    CkJsonObjectW_UpdateString(json,L"total_price",L"24.93");
    CkJsonObjectW_UpdateString(json,L"total_tax",L"0.00");
    CkJsonObjectW_UpdateString(json,L"currency",L"USD");
    CkJsonObjectW_UpdateString(json,L"weight",L"0.40");
    CkJsonObjectW_UpdateString(json,L"weight_unit",L"lb");

    CkHttpW_SetRequestHeader(http,L"Authorization",L"ShippoToken <API_Token>");
    CkHttpW_SetRequestHeader(http,L"Content-Type",L"application/json");

    resp = CkHttpResponseW_Create();
    success = CkHttpW_HttpJson(http,L"POST",L"https://api.goshippo.com/orders/",json,L"application/json",resp);
    if (success == FALSE) {
        wprintf(L"%s\n",CkHttpW_lastErrorText(http));
        CkHttpW_Dispose(http);
        CkJsonObjectW_Dispose(json);
        CkHttpResponseW_Dispose(resp);
        return;
    }

    sbResponseBody = CkStringBuilderW_Create();
    CkHttpResponseW_GetBodySb(resp,sbResponseBody);
    jResp = CkJsonObjectW_Create();
    CkJsonObjectW_LoadSb(jResp,sbResponseBody);
    CkJsonObjectW_putEmitCompact(jResp,FALSE);

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

    respStatusCode = CkHttpResponseW_getStatusCode(resp);
    wprintf(L"Response Status Code = %d\n",respStatusCode);
    if (respStatusCode >= 400) {
        wprintf(L"Response Header:\n");
        wprintf(L"%s\n",CkHttpResponseW_header(resp));
        wprintf(L"Failed.\n");
        CkHttpW_Dispose(http);
        CkJsonObjectW_Dispose(json);
        CkHttpResponseW_Dispose(resp);
        CkStringBuilderW_Dispose(sbResponseBody);
        CkJsonObjectW_Dispose(jResp);
        return;
    }

    // Sample JSON response:
    // (Sample code for parsing the JSON response is shown below)

    // {}

    // 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.



    CkHttpW_Dispose(http);
    CkJsonObjectW_Dispose(json);
    CkHttpResponseW_Dispose(resp);
    CkStringBuilderW_Dispose(sbResponseBody);
    CkJsonObjectW_Dispose(jResp);

    }