Ad

Retrieving Data From Shopify Storefront API

- 1 answer

I am make a simple request to grab data from the Shopify Storefront API.

I have confirmed that the store URL and the Storefront access token for my private app are correct. The error I receive is

{ errors:
   '[API] Invalid API key or access token (unrecognized login or wrong password)' }. 

What am I missing here?

const query = `
    {
      shop {
        name
        primaryDomain {
          url
          host
        }
      }
    }
;


    fetch('https://<store>.myshopify.com/admin/api/graphql', {
      method: 'POST',
      body: JSON.stringify(query),
      headers: {
        'Content-Type': 'application/graphql',
        'Accept': 'application/json',
        "X-Shopify-Storefront-Access-Token": "<storefront-access-token>"
      }
    })
    .then(res => res.json())
    .then(res => console.log(res))
    .catch(response => console.log(response))
Ad

Answer

Your mistake is really quite simple. Your GraphQL endpoint is wrong. The Storefront API has nothing to do with the /admin API which of course you cannot call from a publicly authenticated App.

Try calling the correct endpoint and you will certainly have a better shot at getting the data you need.

 https://<store>.myshopify.com/api/graphql

All the gory details are here: https://help.shopify.com/en/api/custom-storefronts/storefront-api/getting-started#accessing-the-storefront-api-graphql-endpoint

Ad
source: stackoverflow.com
Ad