Chilkat Online Tools

C / ShipEngine Walkthrough / Create a label from scratch USPS

Back to Collection Items

#include <C_CkHttp.h>
#include <C_CkJsonObject.h>
#include <C_CkHttpResponse.h>
#include <C_CkStringBuilder.h>

void ChilkatSample(void)
    {
    HCkHttp http;
    BOOL success;
    HCkJsonObject json;
    HCkHttpResponse resp;
    HCkStringBuilder sbResponseBody;
    HCkJsonObject jResp;
    int respStatusCode;
    int Value;
    const char *Unit;
    const char *dimensionsUnit;
    int Length;
    int Width;
    int Height;
    const char *insured_valueCurrency;
    int insured_valueAmount;
    const char *Reference1;
    const char *Reference2;
    const char *Reference3;
    const char *external_package_id;
    const char *label_id;
    const char *status;
    const char *shipment_id;
    const char *ship_date;
    const char *created_at;
    const char *v_Currency;
    const char *Amount;
    const char *insurance_costCurrency;
    int insurance_costAmount;
    const char *tracking_number;
    BOOL is_return_label;
    const char *rma_number;
    BOOL is_international;
    const char *batch_id;
    const char *carrier_id;
    const char *service_code;
    const char *package_code;
    BOOL voided;
    const char *voided_at;
    const char *label_format;
    const char *label_layout;
    BOOL trackable;
    const char *carrier_code;
    const char *tracking_status;
    const char *v_Pdf;
    const char *Png;
    const char *Zpl;
    const char *Href;
    const char *form_download;
    const char *insurance_claim;
    int i;
    int count_i;

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

    // {
    //   "shipment": {
    //     "service_code": "usps_priority_mail",
    //     "ship_from": {
    //       "name": "John Doe",
    //       "company_name": "Example Corp.",
    //       "address_line1": "4009 Marathon Blvd",
    //       "city_locality": "Austin",
    //       "state_province": "TX",
    //       "postal_code": "78756",
    //       "country_code": "US",
    //       "phone": "512-555-5555"
    //     },
    //     "ship_to": {
    //       "name": "Amanda Miller",
    //       "address_line1": "525 S Winchester Blvd",
    //       "city_locality": "San Jose",
    //       "state_province": "CA",
    //       "postal_code": "95128",
    //       "country_code": "US"
    //     },
    //     "packages": [
    //       {
    //         "weight": {
    //           "value": 17,
    //           "unit": "pound"
    //         },
    //         "dimensions": {
    //           "length": 36,
    //           "width": 12,
    //           "height": 24,
    //           "unit": "inch"
    //         }
    //       }
    //     ]
    //   }
    // }

    json = CkJsonObject_Create();
    CkJsonObject_UpdateString(json,"shipment.service_code","usps_priority_mail");
    CkJsonObject_UpdateString(json,"shipment.ship_from.name","John Doe");
    CkJsonObject_UpdateString(json,"shipment.ship_from.company_name","Example Corp.");
    CkJsonObject_UpdateString(json,"shipment.ship_from.address_line1","4009 Marathon Blvd");
    CkJsonObject_UpdateString(json,"shipment.ship_from.city_locality","Austin");
    CkJsonObject_UpdateString(json,"shipment.ship_from.state_province","TX");
    CkJsonObject_UpdateString(json,"shipment.ship_from.postal_code","78756");
    CkJsonObject_UpdateString(json,"shipment.ship_from.country_code","US");
    CkJsonObject_UpdateString(json,"shipment.ship_from.phone","512-555-5555");
    CkJsonObject_UpdateString(json,"shipment.ship_to.name","Amanda Miller");
    CkJsonObject_UpdateString(json,"shipment.ship_to.address_line1","525 S Winchester Blvd");
    CkJsonObject_UpdateString(json,"shipment.ship_to.city_locality","San Jose");
    CkJsonObject_UpdateString(json,"shipment.ship_to.state_province","CA");
    CkJsonObject_UpdateString(json,"shipment.ship_to.postal_code","95128");
    CkJsonObject_UpdateString(json,"shipment.ship_to.country_code","US");
    CkJsonObject_UpdateInt(json,"shipment.packages[0].weight.value",17);
    CkJsonObject_UpdateString(json,"shipment.packages[0].weight.unit","pound");
    CkJsonObject_UpdateInt(json,"shipment.packages[0].dimensions.length",36);
    CkJsonObject_UpdateInt(json,"shipment.packages[0].dimensions.width",12);
    CkJsonObject_UpdateInt(json,"shipment.packages[0].dimensions.height",24);
    CkJsonObject_UpdateString(json,"shipment.packages[0].dimensions.unit","inch");

    CkHttp_SetRequestHeader(http,"API-Key","{{API_KEY}}");
    CkHttp_SetRequestHeader(http,"Content-Type","application/json");

    resp = CkHttp_PostJson3(http,"https://api.shipengine.com/v1/labels","application/json",json);
    if (CkHttp_getLastMethodSuccess(http) == FALSE) {
        printf("%s\n",CkHttp_lastErrorText(http));
        CkHttp_Dispose(http);
        CkJsonObject_Dispose(json);
        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");
        CkHttpResponse_Dispose(resp);
        CkHttp_Dispose(http);
        CkJsonObject_Dispose(json);
        CkStringBuilder_Dispose(sbResponseBody);
        CkJsonObject_Dispose(jResp);
        return;
    }

    CkHttpResponse_Dispose(resp);

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

    // {
    //   "label_id": "se-1375910",
    //   "status": "completed",
    //   "shipment_id": "se-4139930",
    //   "ship_date": "2019-09-17T00:00:00Z",
    //   "created_at": "2019-09-17T21:01:44.6228265Z",
    //   "shipment_cost": {
    //     "currency": "usd",
    //     "amount": 98.14
    //   },
    //   "insurance_cost": {
    //     "currency": "usd",
    //     "amount": 0
    //   },
    //   "tracking_number": "9405511899564298817982",
    //   "is_return_label": false,
    //   "rma_number": null,
    //   "is_international": false,
    //   "batch_id": "",
    //   "carrier_id": "se-121861",
    //   "service_code": "usps_priority_mail",
    //   "package_code": "package",
    //   "voided": false,
    //   "voided_at": null,
    //   "label_format": "pdf",
    //   "label_layout": "4x6",
    //   "trackable": true,
    //   "carrier_code": "stamps_com",
    //   "tracking_status": "in_transit",
    //   "label_download": {
    //     "pdf": "https://api.shipengine.com/v1/downloads/10/1VAXvK24_0qOSw0q8zQEJQ/label-1375910.pdf",
    //     "png": "https://api.shipengine.com/v1/downloads/10/1VAXvK24_0qOSw0q8zQEJQ/label-1375910.png",
    //     "zpl": "https://api.shipengine.com/v1/downloads/10/1VAXvK24_0qOSw0q8zQEJQ/label-1375910.zpl",
    //     "href": "https://api.shipengine.com/v1/downloads/10/1VAXvK24_0qOSw0q8zQEJQ/label-1375910.pdf"
    //   },
    //   "form_download": null,
    //   "insurance_claim": null,
    //   "packages": [
    //     {
    //       "package_code": "package",
    //       "weight": {
    //         "value": 17,
    //         "unit": "pound"
    //       },
    //       "dimensions": {
    //         "unit": "inch",
    //         "length": 36,
    //         "width": 12,
    //         "height": 24
    //       },
    //       "insured_value": {
    //         "currency": "usd",
    //         "amount": 0
    //       },
    //       "tracking_number": "9405511899564298817982",
    //       "label_messages": {
    //         "reference1": null,
    //         "reference2": null,
    //         "reference3": null
    //       },
    //       "external_package_id": null
    //     }
    //   ]
    // }

    // Sample code for parsing the JSON response...
    // Use this online tool to generate parsing code from sample JSON: Generate JSON Parsing Code

    // Chilkat functions returning "const char *" return a pointer to temporary internal memory owned and managed by Chilkat.

    label_id = CkJsonObject_stringOf(jResp,"label_id");
    status = CkJsonObject_stringOf(jResp,"status");
    shipment_id = CkJsonObject_stringOf(jResp,"shipment_id");
    ship_date = CkJsonObject_stringOf(jResp,"ship_date");
    created_at = CkJsonObject_stringOf(jResp,"created_at");
    v_Currency = CkJsonObject_stringOf(jResp,"shipment_cost.currency");
    Amount = CkJsonObject_stringOf(jResp,"shipment_cost.amount");
    insurance_costCurrency = CkJsonObject_stringOf(jResp,"insurance_cost.currency");
    insurance_costAmount = CkJsonObject_IntOf(jResp,"insurance_cost.amount");
    tracking_number = CkJsonObject_stringOf(jResp,"tracking_number");
    is_return_label = CkJsonObject_BoolOf(jResp,"is_return_label");
    rma_number = CkJsonObject_stringOf(jResp,"rma_number");
    is_international = CkJsonObject_BoolOf(jResp,"is_international");
    batch_id = CkJsonObject_stringOf(jResp,"batch_id");
    carrier_id = CkJsonObject_stringOf(jResp,"carrier_id");
    service_code = CkJsonObject_stringOf(jResp,"service_code");
    package_code = CkJsonObject_stringOf(jResp,"package_code");
    voided = CkJsonObject_BoolOf(jResp,"voided");
    voided_at = CkJsonObject_stringOf(jResp,"voided_at");
    label_format = CkJsonObject_stringOf(jResp,"label_format");
    label_layout = CkJsonObject_stringOf(jResp,"label_layout");
    trackable = CkJsonObject_BoolOf(jResp,"trackable");
    carrier_code = CkJsonObject_stringOf(jResp,"carrier_code");
    tracking_status = CkJsonObject_stringOf(jResp,"tracking_status");
    v_Pdf = CkJsonObject_stringOf(jResp,"label_download.pdf");
    Png = CkJsonObject_stringOf(jResp,"label_download.png");
    Zpl = CkJsonObject_stringOf(jResp,"label_download.zpl");
    Href = CkJsonObject_stringOf(jResp,"label_download.href");
    form_download = CkJsonObject_stringOf(jResp,"form_download");
    insurance_claim = CkJsonObject_stringOf(jResp,"insurance_claim");
    i = 0;
    count_i = CkJsonObject_SizeOfArray(jResp,"packages");
    while (i < count_i) {
        CkJsonObject_putI(jResp,i);
        package_code = CkJsonObject_stringOf(jResp,"packages[i].package_code");
        Value = CkJsonObject_IntOf(jResp,"packages[i].weight.value");
        Unit = CkJsonObject_stringOf(jResp,"packages[i].weight.unit");
        dimensionsUnit = CkJsonObject_stringOf(jResp,"packages[i].dimensions.unit");
        Length = CkJsonObject_IntOf(jResp,"packages[i].dimensions.length");
        Width = CkJsonObject_IntOf(jResp,"packages[i].dimensions.width");
        Height = CkJsonObject_IntOf(jResp,"packages[i].dimensions.height");
        insured_valueCurrency = CkJsonObject_stringOf(jResp,"packages[i].insured_value.currency");
        insured_valueAmount = CkJsonObject_IntOf(jResp,"packages[i].insured_value.amount");
        tracking_number = CkJsonObject_stringOf(jResp,"packages[i].tracking_number");
        Reference1 = CkJsonObject_stringOf(jResp,"packages[i].label_messages.reference1");
        Reference2 = CkJsonObject_stringOf(jResp,"packages[i].label_messages.reference2");
        Reference3 = CkJsonObject_stringOf(jResp,"packages[i].label_messages.reference3");
        external_package_id = CkJsonObject_stringOf(jResp,"packages[i].external_package_id");
        i = i + 1;
    }



    CkHttp_Dispose(http);
    CkJsonObject_Dispose(json);
    CkStringBuilder_Dispose(sbResponseBody);
    CkJsonObject_Dispose(jResp);

    }

Curl Command

curl -X POST
	-H "API-Key: {{API_KEY}}"
	-H "Content-Type: application/json"
	-d '{
	"shipment": {
		"service_code": "usps_priority_mail",
		"ship_from": {
			"name": "John Doe",
			"company_name": "Example Corp.",
			"address_line1": "4009 Marathon Blvd",
			"city_locality": "Austin",
			"state_province": "TX",
			"postal_code": "78756",
			"country_code": "US",
			"phone": "512-555-5555"
		},
		"ship_to": {
			"name": "Amanda Miller",
			"address_line1": "525 S Winchester Blvd",
			"city_locality": "San Jose",
			"state_province": "CA",
			"postal_code": "95128",
			"country_code": "US"
		},
		"packages": [
			{
				"weight": {
					"value": 17,
					"unit": "pound"
				},
				"dimensions": {
					"length": 36,
					"width": 12,
					"height": 24,
					"unit": "inch"
				}
			}
		]
	}
}'
https://api.shipengine.com/v1/labels

Postman Collection Item JSON

{
  "name": "Create a label from scratch USPS",
  "event": [
    {
      "listen": "test",
      "script": {
        "exec": [
          ""
        ],
        "type": "text/javascript"
      }
    }
  ],
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "Content-Type",
        "name": "Content-Type",
        "type": "text",
        "value": "application/json"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n\t\"shipment\": {\n\t\t\"service_code\": \"usps_priority_mail\",\n\t\t\"ship_from\": {\n\t\t\t\"name\": \"John Doe\",\n\t\t\t\"company_name\": \"Example Corp.\",\n\t\t\t\"address_line1\": \"4009 Marathon Blvd\",\n\t\t\t\"city_locality\": \"Austin\",\n\t\t\t\"state_province\": \"TX\",\n\t\t\t\"postal_code\": \"78756\",\n\t\t\t\"country_code\": \"US\",\n\t\t\t\"phone\": \"512-555-5555\"\n\t\t},\n\t\t\"ship_to\": {\n\t\t\t\"name\": \"Amanda Miller\",\n\t\t\t\"address_line1\": \"525 S Winchester Blvd\",\n\t\t\t\"city_locality\": \"San Jose\",\n\t\t\t\"state_province\": \"CA\",\n\t\t\t\"postal_code\": \"95128\",\n\t\t\t\"country_code\": \"US\"\n\t\t},\n\t\t\"packages\": [\n\t\t\t{\n\t\t\t\t\"weight\": {\n\t\t\t\t\t\"value\": 17,\n\t\t\t\t\t\"unit\": \"pound\"\n\t\t\t\t},\n\t\t\t\t\"dimensions\": {\n\t\t\t\t\t\"length\": 36,\n\t\t\t\t\t\"width\": 12,\n\t\t\t\t\t\"height\": 24,\n\t\t\t\t\t\"unit\": \"inch\"\n\t\t\t\t}\n\t\t\t}\n\t\t]\n\t}\n}"
    },
    "url": {
      "raw": "https://api.shipengine.com/v1/labels",
      "protocol": "https",
      "host": [
        "api",
        "shipengine",
        "com"
      ],
      "path": [
        "v1",
        "labels"
      ]
    },
    "description": "This request shows how to create a label by specifying all the information in a single request.  It's the easiest way to create a label if you already know exactly what you want..."
  },
  "response": [
    {
      "name": "Create a label from scratch",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "name": "Content-Type",
            "type": "text",
            "value": "application/json"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n\t\"shipment\": {\n\t\t\"service_code\": \"usps_priority_mail\",\n\t\t\"ship_from\": {\n\t\t\t\"name\": \"John Doe\",\n\t\t\t\"company_name\": \"Example Corp.\",\n\t\t\t\"address_line1\": \"4009 Marathon Blvd\",\n\t\t\t\"city_locality\": \"Austin\",\n\t\t\t\"state_province\": \"TX\",\n\t\t\t\"postal_code\": \"78756\",\n\t\t\t\"country_code\": \"US\",\n\t\t\t\"phone\": \"512-555-5555\"\n\t\t},\n\t\t\"ship_to\": {\n\t\t\t\"name\": \"Amanda Miller\",\n\t\t\t\"address_line1\": \"525 S Winchester Blvd\",\n\t\t\t\"city_locality\": \"San Jose\",\n\t\t\t\"state_province\": \"CA\",\n\t\t\t\"postal_code\": \"95128\",\n\t\t\t\"country_code\": \"US\"\n\t\t},\n\t\t\"packages\": [\n\t\t\t{\n\t\t\t\t\"weight\": {\n\t\t\t\t\t\"value\": 17,\n\t\t\t\t\t\"unit\": \"pound\"\n\t\t\t\t},\n\t\t\t\t\"dimensions\": {\n\t\t\t\t\t\"length\": 36,\n\t\t\t\t\t\"width\": 12,\n\t\t\t\t\t\"height\": 24,\n\t\t\t\t\t\"unit\": \"inch\"\n\t\t\t\t}\n\t\t\t}\n\t\t]\n\t}\n}",
          "options": {
            "raw": {
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "https://api.shipengine.com/v1/labels",
          "protocol": "https",
          "host": [
            "api",
            "shipengine",
            "com"
          ],
          "path": [
            "v1",
            "labels"
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Date",
          "value": "Tue, 17 Sep 2019 21:01:45 GMT"
        },
        {
          "key": "Content-Type",
          "value": "application/json; charset=utf-8"
        },
        {
          "key": "Content-Length",
          "value": "1820"
        },
        {
          "key": "Connection",
          "value": "keep-alive"
        },
        {
          "key": "Access-Control-Allow-Origin",
          "value": "https://www.shipengine.com"
        },
        {
          "key": "Vary",
          "value": "Origin"
        },
        {
          "key": "server",
          "value": "Microsoft-IIS/8.0"
        },
        {
          "key": "x-newrelic-app-data",
          "value": "PxQGVFZXCgITVVZbAwMCV1YGFB9AMQYAZBBZDEtZV0ZaCldOZgRRIzR/GC0HAV1fQ0sxF0VSXwcXVHpSAAMPQBxSFlIUCRoABFwMWQFVBE5TBwVNRFF8dwVTUSMFAw4nJXd3d1JETwRRDksHZQ=="
        },
        {
          "key": "x-powered-by",
          "value": "ASP.NET"
        },
        {
          "key": "x-shipengine-environment",
          "value": "j"
        },
        {
          "key": "x-shipengine-requestid",
          "value": "2183cd26-587e-4a1d-a89c-e873abcd97ef"
        },
        {
          "key": "x-shipengine-server",
          "value": "SS-J-WEB03"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"label_id\": \"se-1375910\",\n    \"status\": \"completed\",\n    \"shipment_id\": \"se-4139930\",\n    \"ship_date\": \"2019-09-17T00:00:00Z\",\n    \"created_at\": \"2019-09-17T21:01:44.6228265Z\",\n    \"shipment_cost\": {\n        \"currency\": \"usd\",\n        \"amount\": 98.14\n    },\n    \"insurance_cost\": {\n        \"currency\": \"usd\",\n        \"amount\": 0\n    },\n    \"tracking_number\": \"9405511899564298817982\",\n    \"is_return_label\": false,\n    \"rma_number\": null,\n    \"is_international\": false,\n    \"batch_id\": \"\",\n    \"carrier_id\": \"se-121861\",\n    \"service_code\": \"usps_priority_mail\",\n    \"package_code\": \"package\",\n    \"voided\": false,\n    \"voided_at\": null,\n    \"label_format\": \"pdf\",\n    \"label_layout\": \"4x6\",\n    \"trackable\": true,\n    \"carrier_code\": \"stamps_com\",\n    \"tracking_status\": \"in_transit\",\n    \"label_download\": {\n        \"pdf\": \"https://api.shipengine.com/v1/downloads/10/1VAXvK24_0qOSw0q8zQEJQ/label-1375910.pdf\",\n        \"png\": \"https://api.shipengine.com/v1/downloads/10/1VAXvK24_0qOSw0q8zQEJQ/label-1375910.png\",\n        \"zpl\": \"https://api.shipengine.com/v1/downloads/10/1VAXvK24_0qOSw0q8zQEJQ/label-1375910.zpl\",\n        \"href\": \"https://api.shipengine.com/v1/downloads/10/1VAXvK24_0qOSw0q8zQEJQ/label-1375910.pdf\"\n    },\n    \"form_download\": null,\n    \"insurance_claim\": null,\n    \"packages\": [\n        {\n            \"package_code\": \"package\",\n            \"weight\": {\n                \"value\": 17,\n                \"unit\": \"pound\"\n            },\n            \"dimensions\": {\n                \"unit\": \"inch\",\n                \"length\": 36,\n                \"width\": 12,\n                \"height\": 24\n            },\n            \"insured_value\": {\n                \"currency\": \"usd\",\n                \"amount\": 0\n            },\n            \"tracking_number\": \"9405511899564298817982\",\n            \"label_messages\": {\n                \"reference1\": null,\n                \"reference2\": null,\n                \"reference3\": null\n            },\n            \"external_package_id\": null\n        }\n    ]\n}"
    }
  ]
}