Sample code for 30+ languages & platforms
React Native

Shippo Adding Metadata

See more Shippo Examples

Demonstrates how to add metadata to the tracking request through a POST request.

Chilkat React Native Downloads

React Native
import { Http, HttpRequest, HttpResponse, JsonObject, StringBuilder } from '@chilkat/react-native'

async function chilkatExample() {
  // This example assumes the Chilkat API to have been previously unlocked.
  // See Global Unlock Sample for sample code.

  const http = new Http();

  // Implements the following CURL command:

  // curl https://api.goshippo.com/tracks/ \
  //     -H "Authorization: ShippoToken <API_TOKEN>" \
  //     -d carrier="shippo" \
  //     -d tracking_number="SHIPPO_TRANSIT" \
  //     -d metadata="Order 000123"

  const req = new HttpRequest();
  req.httpVerb = 'POST';
  req.path = '/tracks/';
  req.contentType = 'application/x-www-form-urlencoded';
  req.addParam('carrier', 'shippo');
  req.addParam('tracking_number', 'SHIPPO_TRANSIT');
  req.addParam('metadata', 'Order 000123');

  req.addHeader('Authorization', 'ShippoToken <API_TOKEN>');

  const resp = new HttpResponse();
  try {
    await http.httpReqAsync('https://api.goshippo.com/tracks/', req, resp);
  } catch {
    console.log(http.lastErrorText);
    return;
  }

  const sbResponseBody = new StringBuilder();
  resp.getBodySb(sbResponseBody);
  const jResp = new JsonObject();
  jResp.loadSb(sbResponseBody);
  jResp.emitCompact = false;

  console.log('Response Body:');
  console.log(jResp.emit());

  const respStatusCode = resp.statusCode;
  console.log(`Response Status Code = ${respStatusCode}`);
  if (respStatusCode >= 400) {
    console.log('Response Header:');
    console.log(resp.header);
    console.log('Failed.');
    return;
  }

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

  // {
  //   "messages": [
  //   ],
  //   "carrier": "shippo",
  //   "tracking_number": "SHIPPO_TRANSIT",
  //   "address_from": {
  //     "city": "San Francisco",
  //     "state": "CA",
  //     "zip": "94103",
  //     "country": "US"
  //   },
  //   "address_to": {
  //     "city": "Chicago",
  //     "state": "IL",
  //     "zip": "60611",
  //     "country": "US"
  //   },
  //   "eta": "2019-07-07T17:07:44.989Z",
  //   "original_eta": "2019-07-07T17:07:44.989Z",
  //   "servicelevel": {
  //     "token": "shippo_priority",
  //     "name": "Priority Mail"
  //   },
  //   "metadata": "Shippo test tracking",
  //   "tracking_status": {
  //     "object_created": "2019-07-04T17:07:45.003Z",
  //     "object_updated": null,
  //     "object_id": "ee35fb56f5d04021b36168abedc04573",
  //     "status": "TRANSIT",
  //     "status_details": "Your shipment has departed from the origin.",
  //     "status_date": "2019-07-03T15:02:45.003Z",
  //     "substatus": null,
  //     "location": {
  //       "city": "San Francisco",
  //       "state": "CA",
  //       "zip": "94103",
  //       "country": "US"
  //     }
  //   },
  //   "tracking_history": [
  //     {
  //       "object_created": "2019-07-04T17:07:45.005Z",
  //       "object_updated": null,
  //       "object_id": "2121a59f53ed42e0ae0436f636179156",
  //       "status": "UNKNOWN",
  //       "status_details": "The carrier has received the electronic shipment information.",
  //       "status_date": "2019-07-02T12:57:45.005Z",
  //       "substatus": null,
  //       "location": {
  //         "city": "San Francisco",
  //         "state": "CA",
  //         "zip": "94103",
  //         "country": "US"
  //       }
  //     },
  //     {
  //       "object_created": "2019-07-04T17:07:45.005Z",
  //       "object_updated": null,
  //       "object_id": "06f949db1a8245beaa28df264b368a76",
  //       "status": "TRANSIT",
  //       "status_details": "Your shipment has departed from the origin.",
  //       "status_date": "2019-07-03T15:02:45.005Z",
  //       "substatus": null,
  //       "location": {
  //         "city": "San Francisco",
  //         "state": "CA",
  //         "zip": "94103",
  //         "country": "US"
  //       }
  //     }
  //   ],
  //   "transaction": null,
  //   "test": true
  // }

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

  let objectCreated = '';
  let objectUpdated = '';
  let objectId = '';
  let status = '';
  let statusDetails = '';
  let statusDate = '';
  let substatus = '';
  let locationCity = '';
  let locationState = '';
  let locationZip = '';
  let locationCountry = '';

  const carrier = jResp.stringOf('carrier');
  const trackingNumber = jResp.stringOf('tracking_number');
  const addressFromCity = jResp.stringOf('address_from.city');
  const addressFromState = jResp.stringOf('address_from.state');
  const addressFromZip = jResp.stringOf('address_from.zip');
  const addressFromCountry = jResp.stringOf('address_from.country');
  const addressToCity = jResp.stringOf('address_to.city');
  const addressToState = jResp.stringOf('address_to.state');
  const addressToZip = jResp.stringOf('address_to.zip');
  const addressToCountry = jResp.stringOf('address_to.country');
  const eta = jResp.stringOf('eta');
  const originalEta = jResp.stringOf('original_eta');
  const servicelevelToken = jResp.stringOf('servicelevel.token');
  const servicelevelName = jResp.stringOf('servicelevel.name');
  const metadata = jResp.stringOf('metadata');
  const trackingStatusObjectCreated = jResp.stringOf('tracking_status.object_created');
  const trackingStatusObjectUpdated = jResp.stringOf('tracking_status.object_updated');
  const trackingStatusObjectId = jResp.stringOf('tracking_status.object_id');
  const trackingStatusStatus = jResp.stringOf('tracking_status.status');
  const trackingStatusStatusDetails = jResp.stringOf('tracking_status.status_details');
  const trackingStatusStatusDate = jResp.stringOf('tracking_status.status_date');
  const trackingStatusSubstatus = jResp.stringOf('tracking_status.substatus');
  const trackingStatusLocationCity = jResp.stringOf('tracking_status.location.city');
  const trackingStatusLocationState = jResp.stringOf('tracking_status.location.state');
  const trackingStatusLocationZip = jResp.stringOf('tracking_status.location.zip');
  const trackingStatusLocationCountry = jResp.stringOf('tracking_status.location.country');
  const transaction = jResp.stringOf('transaction');
  let i = 0;
  let countI = jResp.sizeOfArray('messages');
  while (i < countI) {
    jResp.i = i;
    i++;
  }

  i = 0;
  countI = jResp.sizeOfArray('tracking_history');
  while (i < countI) {
    jResp.i = i;
    objectCreated = jResp.stringOf('tracking_history[i].object_created');
    objectUpdated = jResp.stringOf('tracking_history[i].object_updated');
    objectId = jResp.stringOf('tracking_history[i].object_id');
    status = jResp.stringOf('tracking_history[i].status');
    statusDetails = jResp.stringOf('tracking_history[i].status_details');
    statusDate = jResp.stringOf('tracking_history[i].status_date');
    substatus = jResp.stringOf('tracking_history[i].substatus');
    locationCity = jResp.stringOf('tracking_history[i].location.city');
    locationState = jResp.stringOf('tracking_history[i].location.state');
    locationZip = jResp.stringOf('tracking_history[i].location.zip');
    locationCountry = jResp.stringOf('tracking_history[i].location.country');
    i++;
  }
}