Chilkat Online Tools

unicodeC / ShipEngine Walkthrough / Setup a tracking webhook

Back to Collection Items

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

void ChilkatSample(void)
    {
    HCkHttpW http;
    BOOL success;
    HCkJsonObjectW json;
    HCkHttpResponseW resp;
    HCkStringBuilderW sbResponseBody;
    HCkJsonObjectW jResp;
    int respStatusCode;
    const wchar_t *webhook_id;
    const wchar_t *url;
    const wchar_t *v_event;

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

    http = CkHttpW_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.

    // {
    //   "event": "track",
    //   "url": "https://your-domain-here.com/some/path"
    // }

    json = CkJsonObjectW_Create();
    CkJsonObjectW_UpdateString(json,L"event",L"track");
    CkJsonObjectW_UpdateString(json,L"url",L"https://your-domain-here.com/some/path");

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

    resp = CkHttpW_PostJson3(http,L"https://api.shipengine.com/v1/environment/webhooks",L"application/json",json);
    if (CkHttpW_getLastMethodSuccess(http) == FALSE) {
        wprintf(L"%s\n",CkHttpW_lastErrorText(http));
        CkHttpW_Dispose(http);
        CkJsonObjectW_Dispose(json);
        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");
        CkHttpResponseW_Dispose(resp);
        CkHttpW_Dispose(http);
        CkJsonObjectW_Dispose(json);
        CkStringBuilderW_Dispose(sbResponseBody);
        CkJsonObjectW_Dispose(jResp);
        return;
    }

    CkHttpResponseW_Dispose(resp);

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

    // {
    //   "webhook_id": "102349",
    //   "url": "https://your-domain-here.com/some/path",
    //   "event": "track"
    // }

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

    webhook_id = CkJsonObjectW_stringOf(jResp,L"webhook_id");
    url = CkJsonObjectW_stringOf(jResp,L"url");
    v_event = CkJsonObjectW_stringOf(jResp,L"event");


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

    }

Curl Command

curl -X POST
	-H "API-Key: {{API_KEY}}"
	-H "Content-Type: application/json"
	-d '{
	"event": "track",
	"url": "https://your-domain-here.com/some/path"
}'
https://api.shipengine.com/v1/environment/webhooks

Postman Collection Item JSON

{
  "name": "Setup a tracking webhook",
  "event": [
    {
      "listen": "test",
      "script": {
        "exec": [
          ""
        ],
        "type": "text/javascript"
      }
    }
  ],
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "Content-Type",
        "name": "Content-Type",
        "value": "application/json",
        "type": "text"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n\t\"event\": \"track\",\n\t\"url\": \"https://your-domain-here.com/some/path\"\n}"
    },
    "url": {
      "raw": "https://api.shipengine.com/v1/environment/webhooks",
      "protocol": "https",
      "host": [
        "api",
        "shipengine",
        "com"
      ],
      "path": [
        "v1",
        "environment",
        "webhooks"
      ]
    },
    "description": "To subscribe to real-time tracking updates from ShipEngine, the first step is to setup a webhook.  This is a **one-time step** that can be performed via the API or manually in the ShipEngine dashboard."
  },
  "response": [
    {
      "name": "Setup a tracking webhook",
      "originalRequest": {
        "method": "POST",
        "header": [
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n\t\"event\": \"track\",\n\t\"url\": \"https://your-domain-here.com/some/path\"\n}",
          "options": {
            "raw": {
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "https://api.shipengine.com/v1/environment/webhooks",
          "protocol": "https",
          "host": [
            "api",
            "shipengine",
            "com"
          ],
          "path": [
            "v1",
            "environment",
            "webhooks"
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Date",
          "value": "Wed, 18 Sep 2019 19:00:13 GMT"
        },
        {
          "key": "Content-Type",
          "value": "application/json; charset=utf-8"
        },
        {
          "key": "Content-Length",
          "value": "103"
        },
        {
          "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/GCQIFVFBXwoMB1lFGCUWVFdHBzEGAFgNVwkaFAQcA1UIUQJTB05UGAdSVVcABBxTUE4VBHFfVgVzBSdTIloIVnpSCxoYVFIJSgRs"
        },
        {
          "key": "x-powered-by",
          "value": "ASP.NET"
        },
        {
          "key": "x-shipengine-environment",
          "value": "j"
        },
        {
          "key": "x-shipengine-requestid",
          "value": "629d0a69-ee0a-44f3-8911-b48b0da70985"
        },
        {
          "key": "x-shipengine-server",
          "value": "SS-J-WEB03"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"webhook_id\": \"102349\",\n    \"url\": \"https://your-domain-here.com/some/path\",\n    \"event\": \"track\"\n}"
    }
  ]
}