Navigating Shopify's GraphQL API: Solving the "Invalid Access Token" Error

Ever found yourself staring at an error message that just doesn't make sense, especially when you're sure you've followed all the steps? If you're building custom integrations or migrating to Shopify's GraphQL Admin API, you're not alone. We recently saw a fantastic discussion in the Shopify Community that perfectly illustrates a common pitfall, and I wanted to break it down for you.

Our fellow store owner and developer, lkates, kicked off a thread titled "New store (GraphQL) Invalid API key or access token." They were setting up a brand new store, moving away from the old REST API, and hitting a wall with a 401 Unauthorized error despite diligently following the process to generate a new token. It's a frustrating spot to be in, especially when an existing token works just fine, but the new one doesn't.

The Head-Scratching Scenario: What Went Wrong?

lkates walked us through their detailed process, which looked pretty standard on the surface:

  • Logging into the Shopify admin.
  • Navigating to Settings > Apps.
  • Clicking "Build Apps in Dev Dashboard" (which takes you to the Shopify Partner Dashboard).
  • Creating a new app, giving it a name, and defining the necessary scopes.
  • "Releasing" the app version.
  • Then, on the dev app page, going to Settings > Credentials.
  • And here's where the crucial step happened: copying the "Secret" value.
  • Finally, using this "Secret" in the X-Shopify-Access-Token header for a GraphQL request to their store's admin API endpoint.

The result? A classic 401 with this error message:

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

Sounds familiar, right? It's that moment you feel like you're going crazy because you've done everything "by the book."

The "Aha!" Moment: Secret vs. Access Token

This is where the collective wisdom of the community shines. Eva Greene, another helpful expert, jumped in with the precise clarification that solved lkates's dilemma. The core issue was a subtle but critical misunderstanding between two different types of credentials:

  • The App Secret: This is primarily for app authentication. It's used when your app needs to authenticate itself to Shopify, often during the OAuth flow or for specific server-to-server communications where your app identifies itself.
  • The Admin API Access Token: This is what you need for making actual requests to the Shopify Admin API (whether REST or GraphQL) on behalf of a store. It's the key that grants your app permission to read or write data based on the scopes you've defined.

As Eva pointed out, lkates was using the app secret in the X-Shopify-Access-Token header, which is incorrect for Admin API requests. You need the Admin API access token.

How to Correctly Get Your Admin API Access Token for Custom Apps

Let's refine lkates's process to ensure you grab the right credential. This is especially important for custom apps you build for a single store, which is often the case for migrations or specific integrations.

  1. Log into your Shopify Admin: Go to your store's admin panel.
  2. Navigate to Apps: Click on Settings (bottom left) and then Apps and sales channels.
  3. Access the Develop Apps Section: Click on Develop apps. You'll see a list of your custom apps.
  4. Select or Create Your Custom App: If you haven't already, click Create an app. Give it a name and proceed. If you have an existing one, click on its name.
  5. Configure API Scopes: Under the API credentials tab, scroll down to Admin API access scopes. Click Configure Admin API scopes and select all the permissions your app needs (e.g., read_products, write_orders, etc.). Save your changes.
  6. Install Your App (Crucial Step for Token Generation): After configuring scopes, you'll see a button to Install app. Click this button. This is the action that generates your Admin API access token for the first time.
  7. Reveal and Copy Your Admin API Access Token: Once the app is installed, you'll be redirected back to the API credentials page. Under the Admin API access token section, there will be a button or link to Reveal token once. Click this and immediately copy the token! This token is only shown once for security reasons, so make sure to store it securely.
  8. Use the Token in Your Requests: Now, use this newly copied Admin API access token in your X-Shopify-Access-Token header when making GraphQL or REST Admin API calls.

Following these steps ensures you're using the correct credential for your API requests, bypassing that pesky 401 error.

A Forward-Looking Insight: Expiring Tokens

While Eva's answer solved the immediate problem, Tim Tairli chimed in with another crucial piece of information that developers building robust integrations need to be aware of: Shopify is now forcing expiring tokens.

What does this mean for you? It means that the good old days of grabbing a token once and using it forever are likely gone. For custom apps and private app integrations, this might require you to periodically re-authenticate or implement a token refresh mechanism if your app is designed to run for extended periods without manual intervention. For custom apps built via the "Develop apps" section, the Admin API access token typically doesn't expire quickly, but it's a strong indicator of Shopify's general direction towards enhanced security through token expiration and rotation. Always consult the latest Shopify API documentation for specific expiry policies and best practices for managing tokens.

So, the key takeaways from this community discussion are clear: always double-check which credential you're using – the app secret for authentication versus the Admin API access token for actual API calls. And keep an eye on Shopify's evolving security practices, like the move towards expiring tokens, to ensure your integrations remain robust and secure. It's a reminder that even seasoned developers can stumble on these subtle differences, and the Shopify community is an invaluable resource for navigating them.

Share:

Use cases

Explore use cases

Agencies, store owners, enterprise — find the migration path that fits.

Explore use cases