find inventory_item_id with only SKU

Solved
Highlighted
Shopify Partner
21 2 1

In our system, we only have the SKU.  I need to get the corresponding inventory_item_id so I can update things on Shopify.  How do I do that?

0 Likes
Highlighted
Shopify Partner
21 2 1

Currently I have code that goes through all variants one by one, page by page, to find my SKU, and then I grab that inventory_item_id.  But this is hugely inefficient, so I hope there is a better way to do it.  Does anyone know of a better way?  There has to be, I cannot possibly be the only person who has this need.  Thanks.

0 Likes
Highlighted
Shopify Partner
667 47 134

This is an accepted solution.

Using the GraphQL API you can query for products matching a SKU value. Example below, where I'm pulling the first 5 matching products, then iterating through each match's first 5 variants. The inventory_item_id is at the variant level.

Hope this helps!

 

{
  products(first: 5, query:"sku:{sku_to_find}") {
    edges {
      node {
        id
        title        
        variants(first: 5)
        {
          edges {
            node {
            	id
              title
              sku
              inventoryItem {
                id	#this is the inventory_item_id
              }
            }
          }
        }
      }
    }
  }
}

 

, whe

 

Highlighted
Shopify Partner
21 2 1

Thanks. One of my problems ended up being using "application/json" instead of "application/graphql".

 

That said, it is not searching for my part number, it is bring up all parts, even if SKU does not match.  I guess I need to dig down a little more.  Shocking that such a common activity connected to inventory software is so difficult.

0 Likes
Highlighted
Shopify Partner
21 2 1

Ok, this worked a little better for me:

{productVariants(first: 1, query: "sku:IT-9284-1000-BSK-XL") {  edges {  node { id sku legacyResourceId displayName inventoryItem { id legacyResourceId } } } } }

Then I just go through json structure to get legacyResourceId.  Obviously, as you said, it is also in id, but legacyResouceId saves me some regular expression conversion.

0 Likes
Highlighted
Shopify Partner
21 2 1

This is an accepted solution.

Here is the complete command, for those interested:

curl -i -X POST https://myfakestore.myshopify.com/admin/api/2020-01/graphql.json -H 'Cache-Control: no-cache' -H 'Content-Type: application/graphql' -H 'X-Shopify-Access-Token: APIKEY_TOKEN' -d '{
    productVariants(first: 1, query: "sku:IT-9284-1000-BSK-LG") {
        edges
        {
            node {
                id
                sku
                legacyResourceId
                displayName
                inventoryItem {
                    id
                    legacyResourceId
                }
            }
        }
    }
}
'
0 Likes