Get Product by "SKU" using Storefront API

kpatel
Visitor
1 0 0

I am trying to fetch Product information by 'SKU' from JavaScript. I don't find any option for that. Is that possible just by using Storefront API? If not, Is it possible to use Admin API in the frontend? I don't have any experience with Shopify platform and trying to figure out best way to fetch product by 'SKU'. Below is my JavaScript code:

 

const STOREFRONT_ACCESS_TOKEN = 'xxxxxxxxxxxxxx';
const GRAPHQL_URL = 'https://<mystore>.myshopify.com/api/2021-01/graphql.json';
// NOT WORKING
const query = `productByHandle(handle:"sku:xxxx") {
    id
    title
    description
  }`;
   const GRAPHQL_BODY = {
   'method': 'POST',
   'headers': {
      'X-Shopify-Storefront-Access-Token': STOREFRONT_ACCESS_TOKEN,
      'Content-Type': 'application/json',
   },
   'body': JSON.stringify({ query })
}
fetch(GRAPHQL_URL , GRAPHQL_BODY )
   .then(res => res.json())
   .then(data => {
   return(data);
})
.catch((error) => {
   return(error);
});

 

Replies 2 (2)

mmarienko
Shopify Partner
23 0 11

 

 

query($filter: String!) {
    products(first: 1, query: $filter) {
        edges {
            node {
                id
                title
                handle
            }
        }
    }
}​

 

 

{
    "filter": "sku:1111111111"
}​

 

Regards, Max Marienko - Shopify Expert
ddeniss
Shopify Partner
2 0 0

I believe this answer is incorrect. In my testing I couldn't get it to work, it seems that it is only possible to query product by SKU using Admin API, not Storefront API.