Shopify Graphql 401 after first fly deploy

Topic summary

A developer encounters a recurring 401 error when submitting a GraphQL mutation (webpixelcreate) in their Shopify Remix app, but only on the first request immediately after deploying to Fly.io via flyctl deploy. Subsequent requests work correctly, and the issue doesn’t occur during app reinstallation—only post-deployment.

Key Details:

  • The app uses Shopify’s default Remix template scaffold with standard authentication handled by Shopify libraries
  • Network inspection shows access keys and headers are present even when the 401 occurs
  • The developer attempted a dummy request in useEffect() but this caused the entire homepage to show a 401 error

Suggested Causes:

  • Session invalidation after deployment (Fly.io treating it as a new instance)
  • GraphQL client using outdated/invalid session tokens on first request
  • Fly.io instance warm-up delays
  • Stale deployment state or environment variable persistence issues

Recommended Solutions:

  • Implement retry mechanism for the first GraphQL request
  • Check Fly.io logs (fly logs) for session errors
  • Ensure OAuth flow properly refreshes sessions after deployment
  • Verify SHOPIFY_API_KEY and SHOPIFY_API_SECRET persist correctly

The issue remains unresolved with no definitive fix identified.

Summarized with AI on October 30. AI used: claude-sonnet-4-5-20250929.

Hi,
I have a form with input and a button in my shopify remix app homepage, which runs a graphql for webpixelcreate mutation.

The first time when I submit button after doing any changes and running flyctl deploy. I always get a 401 page.. If I check network, there are all access keys and headers getting passed still.

From second request, I am not getting error.. I installed app on multiple stores and it works, if its not the first time after deploying to fly.io. This doesnt happen everytime on app reinstallation, but happens for sure after I run flyctl deploy.

What can be the issue, how to fix this..

1 Like

Hi @fedora_the_expl

Maybe the reason due to these things

  • Session Invalid After Deployment

    • When you deploy with flyctl deploy, Shopify might treat it as a new instance, invalidating previous auth tokens.
    • Fix: Ensure your OAuth flow properly refreshes the session after deployment. You may need to force a new authentication when detecting a 401 error.
  • GraphQL Client Not Retaining Tokens Immediately

    • The first request may be using an outdated or invalid session token.
    • Fix: Try manually fetching a fresh access token before the first request. Ensure you’re correctly retrieving the session using AppSessionStorage.
  • Fly.io Instance Warm-Up Delay

    • Sometimes, Fly.io instances take a moment to fully start, causing the first request to fail.
    • Fix: Add a small delay (setTimeout) or a retry mechanism for the first API call after deployment.
  • Stale Deployment State

    • If you’re using SHOPIFY_API_KEY and SHOPIFY_API_SECRET as environment variables, ensure they persist correctly between deployments.
    • Fix: Restart your app’s backend services after deployment or reinitialize Shopify session storage.
1 Like

So, you can consider some debugging steps below:

  • Check Fly.io logs (fly logs) right after deployment for session-related errors.
  • Add a retry mechanism for the first GraphQL request after deployment

I am using the default scaffold app which is generated by shopify cli remix template, which randomly generates 10 products. I have modified the code for webpixel.. didnt changed anything related to authentication. Its handled by shopify library.

Here is some code I am using :

export const action = async ({ request }) => {
const { admin } = await authenticate.admin(request);

const response = await admin.graphql(…)

I already tried doing the first dummy request, without user interaction in useEffect(), but my homepage changes to 401 screen if I do that..