I am stuck on this for more than two days now. I couldn't able to find any resources on this. There are many solutions by using ADMIN API but I don't have Admin access. Problem I am trying to solve is: I have only access to Product's SKU. I need to fetch all other information(title, price, description, featuredImage etc...) from Shopify using Storefront API. Here's the function to get product:
function loadProducts(items) {
let products = [];
items.forEach((item) => {
const sku = item.id;
if (sku !== "undefined") {
/* TODO: Need to figure out this query*/
const query = `{
products(first: 1, query: "sku:<sku>") {
edges {
node {
title
id
description
}
}
}
}`;
const STOREFRONT_ACCESS_TOKEN = 'xxxxxxxxxxxxxxx';
const GRAPHQL_URL = 'https://<my-store>.myshopify.com/api/2021-01/graphql.json';
const GRAPHQL_BODY = {
'method': 'POST',
'headers': {
'X-Shopify-Storefront-Access-Token': STOREFRONT_ACCESS_TOKEN,
'Content-Type': 'application/json',
},
'body': JSON.stringify({ query })
}
products.push(getData(GRAPHQL_URL, GRAPHQL_BODY));
}
});
return Promise.all(products);
}
function getData(url, body) {
return new Promise((resolve, reject) => {
fetch(url, body)
.then(res => res.json())
.then(data => {
resolve(data);
})
.catch((error) => {
reject(error);
});
});
}
I'd really appreciate if you can redirect me to the right direction. PLEASE NOT: I am only suppose to use Storefront API, not the ADMIN API. Thank you!
You can't query the products by SKU using the StoreFront API.
The available query params for the StoreFront products are:
So you can't do this only with the StoreFront API since the SKU is not exposed (like the Admin API).