Kalpesh Patel
Kalpesh Patel

Reputation: 269

Can we get product by SKU using Shopify Storefront GraphQL API(NOT ADMIN)?

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!

Upvotes: 0

Views: 3753

Answers (3)

Ben Richardson
Ben Richardson

Reputation: 71

Using the Storefront API, version 2024-07, I can use the following query in order to get a product based off the variant SKU. You can pick which ever fields you need from the product and variant.

query ProductBySKU ($filter: String!) {
  products(first: 1, query: $filter) {
    edges {
      node {
        id
        title
        handle
        variants(first: 1) {
          edges {
            node {
              id
              selectedOptions {
                name
                value
              }
            }
          }
        }
      }
    }
  }
}

Variables

{
  "filter": "sku:YOUR-SKU"
}

Upvotes: 1

Beth Pitcher
Beth Pitcher

Reputation: 11

There is a workaround if you tag all of your products with their SKUs and you can search for that instead.

Upvotes: 1

drip
drip

Reputation: 12943

You can't query the products by SKU using the StoreFront API.

The available query params for the StoreFront products are:

  • available_for_sale
  • created_at
  • product_type
  • tag
  • title
  • updated_at
  • variants.price
  • vendor

So you can't do this only with the StoreFront API since the SKU is not exposed (like the Admin API).

Upvotes: 1

Related Questions