Use SKU to query product handle, keep getting 404 Not Found error

Topic summary

A developer is encountering a persistent 404 Not Found error when attempting to query Shopify’s GraphQL API to retrieve a product handle using a SKU.

Technical Setup:

  • Using Express.js with Shopify Admin API (version 2024-07)
  • Implementing a GraphQL query to search productVariants by SKU
  • Authentication via X-Shopify-Access-Token header

Attempted Solutions:

  • Another user suggested reformatting the query to pass the SKU search string as a GraphQL variable ($queryStr) instead of directly interpolating it into the query string
  • The original poster tried this approach but reports the 404 error persists

Current Status:

  • The issue remains unresolved
  • A follow-up question was raised about verifying the SHOPIFY_SHOP environment variable is correctly configured
  • The conversation suggests potential issues with either the API endpoint URL construction, authentication credentials, or query syntax

The discussion is ongoing with troubleshooting focused on configuration validation.

Summarized with AI on November 6. AI used: claude-sonnet-4-5-20250929.

I use Express.js, and below is my code:

const SHOPIFY_SHOP = process.env.SHOPIFY_SHOP;
const ACCESS_TOKEN = process.env.SHOPIFY_ACCESS_TOKEN;

const getProductHandleBySKU = async (sku) => {
  const query = `
    query getProductHandleBySKU($sku: String!) {
      productVariants(first: 1, query: "sku:${sku}") {
        edges {
          node {
            product {
              handle
            }
          }
        }
      }
    }
  `;

  const variables = { sku: `${sku}` };

  try {
    console.log(`Sending query for SKU: ${sku}`);  // Log the SKU being queried

    const response = await axios({
      url: `https://${SHOPIFY_SHOP}.myshopify.com/admin/api/2024-07/graphql.json`,
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'X-Shopify-Access-Token': ACCESS_TOKEN,
      },
      data: JSON.stringify({ query, variables }),
    });

    // Check if response contains product handle
    const productHandle = response.data.data?.productVariants?.edges?.[0]?.node?.product?.handle;

    if (productHandle) {
      console.log(`Found product handle: ${productHandle} for SKU: ${sku}`);
      return productHandle;
    } else {
      console.log(`No product found for SKU: ${sku}`);
      return null;
    }

  } catch (error) {
    if (error.response) {
      console.error(`Error fetching product handle for SKU: ${sku}`);
      console.error('Error Response:', error.response.data);
      console.error(`Status: ${error.response.status} | StatusText: ${error.response.statusText}`);
    } else {
      console.error('Error Message:', error.message);
    }
    return null;
  }
};

Hi @hippsc

Try this format

const query = `
    query getProductHandleBySKU($queryStr:String!) {
      productVariants(first: 1, query: $queryStr) {
        edges {
          node {
            product {
              handle
            }
          }
        }
      }
    }
  `;
  const variables = { queryStr: `sku:${sku}` };

Hey Kyle,

Thank you for your response. But it’s still not working, still 404 error.

Is the value of the variable 'SHOPIFY_SHOP ’ correct?