FROM CACHE - en_header

API to Search Products by SKU

Yoon
Tourist
9 0 4

Pretty sure this is a very popular question being asked around the forum but it didn't get officially resolved. We have emailed Shopify Partner and it's being rejected and instead suggesting us to ask around the forum. We're using an app that heavily depends on retrieving product information and update them, but the only way to find a product by using SKU(because most app users don't specifically put product ID) is to paginate the API call to retrieve all products and search by SKU inside the loop.

We've tried the solutions as suggested in the topics, but none of them work for non-admin access with Private Apps credentials. This API only works on browser that user already logged in as admin.

https://examplestocksync.myshopify.com/admin/products/search.json?query=sku:XYZ0000

For other browsers without logged in as admin, the API will return a JSON error: 

{"errors":"Not Found"}

Is there any way or any other workaround for this to work?

Replies 21 (21)
Jason
Shopify Expert
10918 193 2191

You can't search for a SKU via the API. You can however search for a SKU using the storefront search so perhaps that's of use.

You do want to be careful if you do that since Shopify does have bot protection in place. If this is for just your store just make sure you are not calling the page too often. If you do, your app will be blocked from accessing the front end.

Here's a very old post I wrote on that idea.
https://freakdesign.com.au/blogs/news/search-for-a-shopify-product-by-sku

★ I jump on these forums in my free time to help and share some insights. Not looking to be hired, and not looking for work. http://freakdesign.com.au ★
AnnoyedDevelope
Tourist
3 0 14

I've read all of the responses / reasons why this feature doesn't exist. They all seem like excuses to me for something that is very standard accross other ecommerce  API platforms. We have over 15k products listed and will likely 30k by end of year. Downloading the entire inventory 30-50 times a day is not something Shopify wants me doing!


Here's our terrible work-around:
Download entire inventory once a day. Store the product ID in a database next to the Variant SKU, cross reference for things like quantity / pricing updates. I'm charging my customer a lot of dev time for a missing / standard feature.


At this point, the work is done. I don't care if shopify ever fixes this anymore. But, just a general feedback you are not serving your customers well in this area.

Jelena1
Shopify Partner
17 0 2

Hi!

If you need to update product price or quantity by SKU, there is an app that can help: https://apps.shopify.com/excel-export-import.

You just need to prepare the Excel document with Variant SKUs and data you want to update and import it into your Shopify store.

Here is the detailed tutorial on the update by SKU: https://excelify.io/2018/02/12/update-shopify-product-by-sku/. And you can find many more useful things you can do with the app.

Feel free to ask any question on that topic.

Best regards!

Crayons
Excursionist
11 1 12

Did you try the variants endpoint? So far as I know, the product endpoint does not contain product SKUs, thus the not found error.

https://examplestocksync.myshopify.com/admin/variants/search.json?query=sku:XYZ0000

 

mkhizeryounas
Shopify Partner
1 0 5

I wasn't able to find anything with a REST based solution as Shopify does not provide an API endpoint for this. I solved this by making a GraphQL request using the following body.

 

URL: https://example.myshopify.com/admin/api/<api-version>/graphql

-H 'X-Shopify-Access-Token: <ACCESS_TOKEN>' \
-H 'Content-Type: application/json' \

Body
{
  query: `query($filter: String!) {
    productVariants(first: 1, query: $filter) { 
      edges { 
          node { 
          id 
          sku 
          legacyResourceId 
          displayName 
          inventoryItem { 
            id 
            legacyResourceId 
          } 
        } 
      } 
    } 
  }`,
  variables: {
    filter: `sku:my-custom-sku`
  }
}
Yoon
Tourist
9 0 4

Ah I've overlooked the graphQL query to find product based on SKU. Does it support multiple SKUs or only one SKU is allowed per API call?

Crayons
Excursionist
11 1 12

@Yoon 

Check out the search syntax: https://help.shopify.com/en/api/getting-started/search-syntax

You can use an OR connector to specify multiple SKU's, or other parameters.

 

Example:

"filter" => "sku:ABC123 OR sku:ABC456 OR sku:ABC789"
Kris_Doyle
Shopify Partner
101 0 25

Quick question on this...for SKU queries, it seems like this query only returns variant results if you enter the exact SKU. Prefix queries don't seem to return anything. This isn't a big deal, just wanted to confirm if that's the expected behaviour.

 

So, for example, let's say we have:

 

Product 1 has 3 SKUs: "SKU1", "SKU2", and "SKU3". 

query: "sku:SKU1 OR sku:SKU2 OR sku:SKU3"

...will return those variants.

 

But, 

query: "sku:SKU*"

(prefix query with *) returns nothing.

 

Is that the expected behaviour specifically with SKU searches?

 

Thanks!

Kris

Crayons
Excursionist
11 1 12

If I recall correctly, partial sku matches don't work.

 

If I may offer some advice:

I have spent a great deal of time implementing workarounds and band-aid solutions for searching products by SKU. I don't know what you're working on, but if you've landed here, then I highly recommend you use GraphQL.

https://help.shopify.com/en/api/graphql-admin-api

 

Shopify now has a handy GraphiQL App which lets you execute graph queries against your shop. I found this notably helpful in order to test and get the ball rolling.

 

With the GraphQL API, you can fetch only the data you are looking for, which makes your calls faster, while easily being able to filter by a particular SKU.

Kris_Doyle
Shopify Partner
101 0 25

Thanks very much. Agreed completely...I've been using it for about a month now and other than some issues with 500s, it's been a dream. Still learning every day though.

 

Glad I'm not going crazy on the partial SKU thing 😉

 

Cheers,

Kris

zhaoguoyuan
New Member
1 0 0

The discussion here is very helpful, I would like to add something new I found:

 

according to : https://help.shopify.com/en/api/getting-started/search-syntax

 

prefix query will match the document, so you can query like this

query: "SKU*"

since it will  query the whole document(usually used by a document database, think it like a row, it means every field) , you can filter the result by the field of SKU again based on the result.

 

for the suffix part, I guess the search engine is implemented with elastic search, and since there are no efficient way to do suffix query by default, so it is not implemented yet.

https://discuss.elastic.co/t/is-there-a-query-filter-for-suffix/49680

 

help this helpful.

 

 

azharsss
Tourist
4 0 1

Can you provide any working example, i have tried using the same but its not working for me

MartinaS
New Member
1 0 0

You can search in the "Products" tab by entering just the numbers of the SKU and no letters. 

 

 

Tony49
Shopify Partner
31 2 4

I cannot get this graphql command to work.  Does it still work for you? Per chance do you have the complete command you use?

Tony49
Shopify Partner
31 2 4

Never mind, my problem was the graphql portion.  Here is the complete command, in case anyone is 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
                }
            }
        }
    }
}
'
dhrumil4u360
Tourist
5 0 4

This one worked like a charm! But sadly, it is not working with REST Admin API!

 

 

https://examplestocksync.myshopify.com/admin/variants/search.json?query=sku:XYZ0000

 

sdprz
Tourist
3 0 1

Nope, does not work. 

ansezz
New Member
1 0 0

use :

https://example.myshopify.com/admin/products/search.json?query=sku:MYSKU

skuharmony
Shopify Partner
27 2 6

Thanks for the recommendation to use the GraphQL API. I was able to look up a productId from a SKU with this GraphQL Query:

 

{
    productVariants(first: 1, query: "sku:GREEN") {
        edges
        {
            node {
                product {
                    id
                    handle
                }
            }
        }
    }
}

 

It returns a productId and handle which I can then use with the REST API to retrieve Product information:

{
    "data": {
        "productVariants": {
            "edges": [
                {
                    "node": {
                        "product": {
                            "id": "gid:\/\/shopify\/Product\/4669837508667",
                            "handle": "apple"
                        }
                    }
                }
            ]
        }
    },
    "extensions": {
        "cost": {
            "requestedQueryCost": 4,
            "actualQueryCost": 4,
            "throttleStatus": {
                "maximumAvailable": 1000,
                "currentlyAvailable": 996,
                "restoreRate": 50
            }
        }
    }
}