Sample code for 30+ languages & platforms
Dart

Etsy: Get the Inventory for a Listing

See more Etsy Examples

Gets the inventory for a listing.

Chilkat Dart Downloads

Dart
import 'package:chilkat/chilkat.dart';

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

  final http = CkHttp();

  // Implements the following CURL command:

  // curl -X GET \
  //   https://openapi.etsy.com/v2/listings/listing_id/inventory?api_key=MY_ETSY_KEYSTRING

  final sbResponseBody = CkStringBuilder();
  try {
    http.quickGetSb('https://openapi.etsy.com/v2/listings/listing_id/inventory?api_key=MY_ETSY_KEYSTRING', sbResponseBody);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  final jResp = CkJsonObject();
  jResp.loadSb(sbResponseBody);
  jResp.emitCompact = false;

  print('Response Body:');
  print(jResp.emit());

  final respStatusCode = http.lastStatus;
  print('Response Status Code = $respStatusCode');
  if (respStatusCode >= 400) {
    print('Response Header:');
    print(http.lastHeader);
    print('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

  var productId = 0;
  var j = 0;
  var countJ = 0;
  var offeringId = 0;
  var priceAmount = 0;
  var priceDivisor = 0;
  var priceCurrencyCode = '';
  var priceCurrencyFormattedShort = '';
  var priceCurrencyFormattedLong = '';
  var priceCurrencyFormattedRaw = '';
  var quantity = 0;

  final count = jResp.intOf('count');
  final paramsListingId = jResp.stringOf('params.listing_id');
  final vType = jResp.stringOf('type');
  var i = 0;
  final 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++;
  }
}