Sample code for 30+ languages & platforms
React Native

Etsy: Get the Inventory for a Listing

See more Etsy Examples

Gets the inventory for a listing.

Chilkat React Native Downloads

React Native
import { Http, 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 -X GET \
  //   https://openapi.etsy.com/v2/listings/listing_id/inventory?api_key=MY_ETSY_KEYSTRING

  const sbResponseBody = new StringBuilder();
  try {
    await http.quickGetSbAsync('https://openapi.etsy.com/v2/listings/listing_id/inventory?api_key=MY_ETSY_KEYSTRING', sbResponseBody);
  } catch {
    console.log(http.lastErrorText);
    return;
  }

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

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

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

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

  // {
  //   "count": 1,
  //   "results": {
  //     "products": [
  //       {
  //         "product_id": 3361120103,
  //         "property_values": [
  //         ],
  //         "offerings": [
  //           {
  //             "offering_id": 3579642570,
  //             "price": {
  //               "amount": 16000,
  //               "divisor": 100,
  //               "currency_code": "USD",
  //               "currency_formatted_short": "$160.00",
  //               "currency_formatted_long": "$160.00 USD",
  //               "currency_formatted_raw": "160.00"
  //             },
  //             "quantity": 1
  //           }
  //         ]
  //       }
  //     ]
  //   },
  //   "params": {
  //     "listing_id": "720138253",
  //     "write_missing_inventory": false
  //   },
  //   "type": "ListingInventory",
  //   "pagination": {}
  // }

  // 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 productId = 0;
  let j = 0;
  let countJ = 0;
  let offeringId = 0;
  let priceAmount = 0;
  let priceDivisor = 0;
  let priceCurrencyCode = '';
  let priceCurrencyFormattedShort = '';
  let priceCurrencyFormattedLong = '';
  let priceCurrencyFormattedRaw = '';
  let quantity = 0;

  const count = jResp.intOf('count');
  const paramsListingId = jResp.stringOf('params.listing_id');
  const vType = jResp.stringOf('type');
  let i = 0;
  const countI = jResp.sizeOfArray('results.products');
  while (i < countI) {
    jResp.i = i;
    productId = jResp.intOf('results.products[i].product_id');
    j = 0;
    countJ = jResp.sizeOfArray('results.products[i].property_values');
    while (j < countJ) {
      jResp.j = j;
      j++;
    }

    j = 0;
    countJ = jResp.sizeOfArray('results.products[i].offerings');
    while (j < countJ) {
      jResp.j = j;
      offeringId = jResp.intOf('results.products[i].offerings[j].offering_id');
      priceAmount = jResp.intOf('results.products[i].offerings[j].price.amount');
      priceDivisor = jResp.intOf('results.products[i].offerings[j].price.divisor');
      priceCurrencyCode = jResp.stringOf('results.products[i].offerings[j].price.currency_code');
      priceCurrencyFormattedShort = jResp.stringOf('results.products[i].offerings[j].price.currency_formatted_short');
      priceCurrencyFormattedLong = jResp.stringOf('results.products[i].offerings[j].price.currency_formatted_long');
      priceCurrencyFormattedRaw = jResp.stringOf('results.products[i].offerings[j].price.currency_formatted_raw');
      quantity = jResp.intOf('results.products[i].offerings[j].quantity');
      j++;
    }

    i++;
  }
}