Get Product by "SKU" using Storefront API

kpatel
New Member
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);
});

 

0 Likes