Sample code for 30+ languages & platforms
Delphi DLL

Etsy: Get the Inventory for a Listing

See more Etsy Examples

Gets the inventory for a listing.

Chilkat Delphi DLL Downloads

Delphi DLL
uses
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
    Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Http, StringBuilder, JsonObject;

...

procedure TForm1.Button1Click(Sender: TObject);
var
success: Boolean;
http: HCkHttp;
sbResponseBody: HCkStringBuilder;
jResp: HCkJsonObject;
respStatusCode: Integer;
product_id: Integer;
j: Integer;
count_j: Integer;
offering_id: Integer;
priceAmount: Integer;
priceDivisor: Integer;
priceCurrency_code: PWideChar;
priceCurrency_formatted_short: PWideChar;
priceCurrency_formatted_long: PWideChar;
priceCurrency_formatted_raw: PWideChar;
quantity: Integer;
count: Integer;
paramsListing_id: PWideChar;
paramsWrite_missing_inventory: Boolean;
v_type: PWideChar;
i: Integer;
count_i: Integer;

begin
success := False;

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

http := CkHttp_Create();

// Implements the following CURL command:

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

sbResponseBody := CkStringBuilder_Create();
success := CkHttp_QuickGetSb(http,'https://openapi.etsy.com/v2/listings/listing_id/inventory?api_key=MY_ETSY_KEYSTRING',sbResponseBody);
if (success = False) then
  begin
    Memo1.Lines.Add(CkHttp__lastErrorText(http));
    Exit;
  end;

jResp := CkJsonObject_Create();
CkJsonObject_LoadSb(jResp,sbResponseBody);
CkJsonObject_putEmitCompact(jResp,False);

Memo1.Lines.Add('Response Body:');
Memo1.Lines.Add(CkJsonObject__emit(jResp));

respStatusCode := CkHttp_getLastStatus(http);
Memo1.Lines.Add('Response Status Code = ' + IntToStr(respStatusCode));
if (respStatusCode >= 400) then
  begin
    Memo1.Lines.Add('Response Header:');
    Memo1.Lines.Add(CkHttp__lastHeader(http));
    Memo1.Lines.Add('Failed.');
    Exit;
  end;

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

count := CkJsonObject_IntOf(jResp,'count');
paramsListing_id := CkJsonObject__stringOf(jResp,'params.listing_id');
paramsWrite_missing_inventory := CkJsonObject_BoolOf(jResp,'params.write_missing_inventory');
v_type := CkJsonObject__stringOf(jResp,'type');
i := 0;
count_i := CkJsonObject_SizeOfArray(jResp,'results.products');
while i < count_i do
  begin
    CkJsonObject_putI(jResp,i);
    product_id := CkJsonObject_IntOf(jResp,'results.products[i].product_id');
    j := 0;
    count_j := CkJsonObject_SizeOfArray(jResp,'results.products[i].property_values');
    while j < count_j do
      begin
        CkJsonObject_putJ(jResp,j);
        j := j + 1;
      end;

    j := 0;
    count_j := CkJsonObject_SizeOfArray(jResp,'results.products[i].offerings');
    while j < count_j do
      begin
        CkJsonObject_putJ(jResp,j);
        offering_id := CkJsonObject_IntOf(jResp,'results.products[i].offerings[j].offering_id');
        priceAmount := CkJsonObject_IntOf(jResp,'results.products[i].offerings[j].price.amount');
        priceDivisor := CkJsonObject_IntOf(jResp,'results.products[i].offerings[j].price.divisor');
        priceCurrency_code := CkJsonObject__stringOf(jResp,'results.products[i].offerings[j].price.currency_code');
        priceCurrency_formatted_short := CkJsonObject__stringOf(jResp,'results.products[i].offerings[j].price.currency_formatted_short');
        priceCurrency_formatted_long := CkJsonObject__stringOf(jResp,'results.products[i].offerings[j].price.currency_formatted_long');
        priceCurrency_formatted_raw := CkJsonObject__stringOf(jResp,'results.products[i].offerings[j].price.currency_formatted_raw');
        quantity := CkJsonObject_IntOf(jResp,'results.products[i].offerings[j].quantity');
        j := j + 1;
      end;

    i := i + 1;
  end;

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

end;