Using API to pull the inventory count of an item from the SKU on another website

Hi there,

I’m trying to make a chrome browser extension that uses Square’s Inventory API to pull our store’s physical inventory count and displays it in a box on our distributor’s website. However, I’m having trouble understanding how to get only one piece of information (the catalog ID) when searching catalog items.

I don’t really have much programming knowledge, but this is what I have so far:

function get_itemid() {
  try {
    const response = await client.catalogApi.searchCatalogItems({
      textFilter: '[SKU goes here]',
      limit: 100,
     
    console.log(response.result);
  } catch(error) {
    console.log(error);
  }
}

function get_stocklevel() {
  try {
    const response = await 
  client.inventoryApi.retrieveInventoryCount(' [HOW DO I PUT THE CATALOG ID FROM ABOVE HERE?] ');
  }
}

There’s more code for injecting the stock level locally into the other website, but I think I have that down. The main thing I’m missing is how to pull info from one piece of code/function into another. I’m guessing it’s via a ‘var’, but the get_itemid function pulls a lot of extraneous info when all I need is the item ID for the get_stocklevel function.

Thanks in advance!

:wave: You’ll need to get the nested item_variation_id from the response of the SearchCatalogItems call you made. Depending on the language your using there are multiple ways to get the ID and pass it to your next request. :slightly_smiling_face:

Hi Brian,

Thanks! Sorry for the delayed response. I’m using Node.js. Does this look right to you?

function get_itemid() {
  try {
    const itemdesc = await client.catalogApi.searchCatalogItems({
      textFilter: '[SKU goes here]',
      limit: 10,
     
    console.log(itemdesc.result);
  } catch(error) {
    console.log(error);
  }
  var itemid_pulled = itemdesc.result.items.item_data.item_variation_data.item_id; 
}

function get_stocklevel() {
  try {
    const stocklevel = await client.inventoryApi.retrieveInventoryCount('itemid_pulled');

  console.log(stocklevel.result);
  } catch)error) {
   console.log(error)
  }
  var invcount = stocklevel.result.count.quantity;
}

Still working on the code to pull the ISBN from the website and place a text box into the page.