Webhooks URLs are not updating with server-restarts

Hi :wave:t4:

I’m currently building a Shopify app and have some struggles with webhooks. My webhook URLs are not updated.

I use the remix template. I start my server with shopify app dev --config <FILENAME>

Sometimes the webhooks work (mostly I need the order/create webhook) but sometimes they don’t.

I tried to start the server also with shopify app dev --reset and select my app in the CLI but that doesn’t help as well.

In my webhook dashboard, I see that they fail because they are arriving an old URL. What should be the typical way of starting the server so that the webhook URLs are also changed?

I defined my webhooks like this:


  webhooks: {
    APP_UNINSTALLED: {
      deliveryMethod: DeliveryMethod.Http,
      callbackUrl: "/webhooks",
    },
    APP_PURCHASES_ONE_TIME_UPDATE: {
      deliveryMethod: DeliveryMethod.Http,
      callbackUrl: "/webhooks",
    },
    ORDERS_CREATE: {
      deliveryMethod: DeliveryMethod.Http,
      callbackUrl: "/webhooks",
    },
  },
  hooks: {
    afterAuth: ({ session }) => {
      console.log("afterAuth", { session });
      shopify.registerWebhooks({ session });
    },
  },

If it works I can see that the afterAuth hook is called. But when it is not working I can see that it is not working. I think they are simply not registered and therefore, have no updated URLs attached. But I don’t know why it won’t run.

1 Like

I think the issue is that the afterAuth hook is not executed. I found this GitHub issue: Shopify afterAuth hook not registering webhooks · Issue #761 · Shopify/shopify-app-js · GitHub

It mentions that the session storage in the DB needs to be removed. I did that and now I get the webhook.

But does that mean that I need to remove the session on every restart of my development server so that my new URLs get updated?

Hi there :wave:

You have correctly diagnosed the problem, if your app URL changes your webhook subscriptions in Shopify will also need to update for your events to be received.

This is generally only a problem for local development because in production your app URL won’t be changing.

For local development you have a couple of options

  1. Don’t change your local development URL
    You can supply your own tunnel to the Shopify CLI so that you can keep a consistent address for local development. Docs here.

  2. Update the webhook subscriptions when your app URL updates
    a) Trigger shopify.registerWebhooks({ session }); to run and update the webhook subscriptions.

If you are using the default template this is only run in the afterAuth hook. You could either call shopify.registerWebhooks somewhere else for local development, or delete the session/unistall to trigger the afterAuth hook to run again

b) Use webhook subscriptions defined in the shopify.app.toml (recommended)
Shopify has released a new way to manage app webhook subscriptions.
Docs here.

If you are using this method, in another terminal while your app is running, you can run shopify app deploy to update the webhook subscription adresses in Shopify.

@Liz-Shopify Hi I am facing a similar issue,

We by mistake made our webhooks go to https://https//url in prod
We fixed the URL, but 50% of the time, our webhooks are still arriving on the old URL, rest 50% of the time, we get it on the correct one.

I still didn’t understand the issue and how to solve it.

@tanay

We fixed the URL,

Where did you fix the URL, and how did you fix it?

If are using webhook subscriptions that are managed by the app, (defined in the ShopifyApp object.) Then you will need to update those subscriptions in Shopify. This would have to be done by making an API call to Shopify to update the address of the webhook subscription.