Is there any way to search by SKU in Storefront API?

Hi,

I checked the documentation but couldn’t find any way to fetch a specific variant by its SKU.

Is there any other way we could achieve this without using the Admin API?

Thanks!

i think you are looking at the wrong API endpoint. This should be what you need.
Below is an example query for getting variant sku’s for your products object

query getProductWithSKUs($handle: String!) {
  product(handle: $handle) {
    id
    title
    variants(first: 50) {
      edges {
        node {
          id
          title
          sku
        }
      }
    }
  }
}

You can also access the json data for your product if you add .json at the end of your product url like this: https://your-store.com/products/product-name.json
SKU should be available there so you cat fetch that with js.

Hope this helps

Thanks for the quick reply.

What I’m searching for is actually a way to find the product by its variant SKU.

For example: I have a variant with SKU SKU-123. I don’t know what is the handle, nor the product ID.

I’d then need to get the product details only from the sku SKU-123.

But I couldn’t find any way so far for fetching the product with the SKU alone :frowning:

oooo, is ee what you mean - i think you can use the .json endpoint i have shared as second option.

Here is something to get you started:

fetch('https://your-store.myshopify.com/products.json')
  .then(response => response.json())
  .then(data => {
    // Search through products and variants for specific SKU
    const findBySKU = (sku) => {
      for (const product of data.products) {
        for (const variant of product.variants) {
          if (variant.sku === sku) {
            return { product, variant };
          }
        }
      }
      return null;
    };
    
    const result = findBySKU('YOUR-SKU-HERE');
    console.log(result);
  });

Limitations:
pagination - default 50 products and maximum 250 at a time can be fetched this way
performance - this grabs the json for all your store products so if you have a ton of products it might be slow.
cache - you might want to cache the results and store them locally after a search is perfromed

Hope this helps - otherwise the solution is sadly admin api

Try running this query

query {
  productVariants(first: 10, query: "sku:YOUR-SKU-HERE") {
    edges {
      node {
        id
        title
        sku
        product {
          id
          title
        }
      }
    }
  }
}

This alternative uses wildcard format, but it could still be helpful:

query {
  productVariants(first: 10, query: "sku:element*") {
    edges {
      node {
        id
        title
        sku
      }
    }