find inventory_item_id with only SKU

Solved
Tony49
Shopify Partner
22 2 3

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?

Accepted Solutions (2)

Accepted Solutions
Greg_Kujawa
Shopify Partner
1016 83 249

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

 

View solution in original post

Tony49
Shopify Partner
22 2 3

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
                }
            }
        }
    }
}
'

View solution in original post

Replies 8 (8)
Tony49
Shopify Partner
22 2 3

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.

Greg_Kujawa
Shopify Partner
1016 83 249

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

 

View solution in original post

Tony49
Shopify Partner
22 2 3

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.

Tony49
Shopify Partner
22 2 3

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.

Tony49
Shopify Partner
22 2 3

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
                }
            }
        }
    }
}
'

View solution in original post

BenoitBlanchon
New Member
1 0 1

Here is a simpler version of this query that requires fewer permissions:

{
    inventoryItems(query: "sku:IT-9284-1000-BSK-LG", first: 1) {
        edges {
            node {
                id
                legacyResourceId
                sku
            }
        }
    }
}
As you can see, it doesn't go through the "productVariants" connection, so it doesn't need the "read_product_listings" access scope.
Tony49
Shopify Partner
22 2 3

Ok, that actually worked.  The output from my original solution:

{
    "data": {
        "productVariants": {
            "edges": [
                {
                    "node": {
                        "displayName": "Retro Sweet Tea - test website - L", 
                        "id": "gid://shopify/ProductVariant/32562933629012", 
                        "inventoryItem": {
                            "id": "gid://shopify/InventoryItem/34193764810836", 
                            "legacyResourceId": "34193764810836"
                        }, 
                        "legacyResourceId": "32562933629012", 
                        "sku": "IT-9284-1000-BSK-LG"
                    }
                }
            ]
        }
    }, 
    "extensions": {
        "cost": {
            "actualQueryCost": 4, 
            "requestedQueryCost": 4, 
            "throttleStatus": {
                "currentlyAvailable": 996, 
                "maximumAvailable": 1000.0, 
                "restoreRate": 50.0
            }
        }
    }
}

Note the `"legacyResourceId": "34193764810836",` under "node", which is what I need later in my code.  Now, with the simpler version, we have this, with the same legacyRecourceId and much of the same other information:

{
    "data": {
        "inventoryItems": {
            "edges": [
                {
                    "node": {
                        "id": "gid://shopify/InventoryItem/34193764810836", 
                        "legacyResourceId": "34193764810836", 
                        "sku": "IT-9284-1000-BSK-LG"
                    }
                }
            ]
        }
    }, 
    "extensions": {
        "cost": {
            "actualQueryCost": 3, 
            "requestedQueryCost": 3, 
            "throttleStatus": {
                "currentlyAvailable": 997, 
                "maximumAvailable": 1000.0, 
                "restoreRate": 50.0
            }
        }
    }
}

So thanks, this would be a good alternative to my current code.

NinoOroa
New Member
1 0 0

This works like a charm, I used this but with a query for product tags instead of sku. I read a couple different posts and none of them worked aside from this one. Thank you