Shopify Graphql 401 after first fly deploy

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..