Find a product given the inventory_item_id

Solved
Highlighted
New Member
2 0 0

I'm writing a small inventory level alert system for my shop using the REST API. I want to send an email or sms message when stock levels go below a threshold.

 

I have a webhook set up for the "Inventory level update" event which send a json notification like this:

 

{
"inventory_item_id": 320792377713505,
"location_id": 35139476689,
"available": 31,
"updated_at": "2020-05-22T12:16:46+10:00",
"admin_graphql_api_id": "gid://shopify/InventoryLevel/68938698001?inventory_item_id=326071654305"
}

 

This is great, I have all I need to determine if an alert should be issued however I want to include descriptive product information in the alert message, probably Product Title and Variant Title.

 

So, the question:

 

How do I get the product title and variant title using the API if I only have the inventory_item_id?

 

The product and product_variant resources can't be looked by by inventory_item_id.  The only thing I can think of is to fetch all the products and variants in advance and store them locally along with the inventory_item_id. I'd probably have to register new webhooks to catch product changes and additions when they happen. This smells pretty bad so I'm looking for a better way.

 

Thanks in advance,

Matt

 

 

 

 

 

0 Likes
Highlighted
Shopify Staff (Retired)
Shopify Staff (Retired)
624 102 129

This is an accepted solution.

Hey @MattFitzgerald ,

 

You can accomplish this in one API request if you use the Shopify GraphQL Admin API. Here is the query you would have to call:

 

query {
	inventoryItem(id: "gid://shopify/InventoryItem/{insert ID here}") {
    id
    variant {
      title
      product {
         title
      }
  	}
  }
}

Hassain | Developer Support Specialist @ Shopify
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Click Accept as Solution 

Highlighted
New Member
2 0 0

Excellent, thanks Hassain.

 

This worked perfectly, looks like it's time for me to level up from REST to GraphQL.

 

Cheers,

Matt

 

0 Likes