Topics covering webhook creation & management, event handling, Pub/Sub, and Eventbridge, in Shopify apps.
I am not able to register the webhook, I read the documentation and I am not able to figure out as to in which file I have to write the webhook post method and do I have to fetch the post method in the frontend in order to invoke it.
To register a webhook in your Shopify app, you need to use the Shopify REST API to create the webhook and specify the URL endpoint for receiving the webhook. The webhook handling code should be in the backend using a server-side programming language like Node.js or Ruby.
Here's the code to register a webhook:
const webhook = new shopify.rest.Webhook({session: session});
webhook.address = "https://your-app.com/webhook-receiver-address";
webhook.topic = "customers/update";
webhook.format = "json";
await webhook.save({
update: true,
});
You can find more details on how to register webhooks in the Shopify documentation:
https://shopify.dev/docs/api/admin-rest/2023-01/resources/webhook
And here's an article on configuring HTTPS for webhooks:
https://shopify.dev/docs/apps/webhooks/configuration/https
I wrote the same piece of code in my Index.js file where all other end points are defined, now what do I have to do next
I recommend you to take the Shopify App tutorial first; it includes the webhook setup as well:
https://shopify.dev/docs/apps/getting-started/build-app-example
I checked and if I am not wrong, the sample app doesn't contain any code related to webhooks.
If you can just tell me if I am doing everything right in below code
The code that I've sent is for registering the webhook address with Shopify; you should execute it right after the user authenticates with your app.
After registering webhook, when the store gets updated, your app will receive webhook data from Shopify; you should create a route to handle it. Here is the document guide:
https://shopify.dev/docs/apps/webhooks/configuration/https#step-4-receive-the-webhook
yeah, in order to receive the HTTP request from shopify, I wrote following code in my frontend
Hey @nikhilthink and @Weaverse,
Just wanted to add a few links to your to conversation here. If you are following the Build an app guide and webhooks documentation, neither cover specific steps for setting up webhooks directly in your app.
If you are building further upon an app that is leveraging the official shopify-api-js library an example is provided in the repo for setting up webhooks, which is expanded on in this workshop.
Hope those links help with your setup - cheers!
@awwdam
Shopify Developer Support
Developer Support @ Shopify
- Was this reply helpful? Click Like to let us know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog
@ShopifyDevSup both of these links are now dead. Are there any other examples that you know of? Thanks
Hey @originmaster , thanks for pointing that out.
This guide here has updated links and resources for managing webhooks in your app https://shopify.dev/docs/apps/webhooks/configuration
Hope this helps
- Kyle G.
Developer Support @ Shopify
- Was this reply helpful? Click Like to let us know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog
@ShopifyDevSup thanks, I was more looking for examples of the setup. I mostly figured it out but one thing that isn't clear is how to register new webhooks when the app is already live and installed on the store. It seems the only way is to uninstall the app and reinstall it because OAuth flow needs to be triggered
Hi @originmaster did you understand how to register new webhooks on a public app?
I try to find a way too, and the classic method don't seems to work!
@ClementOutis I was working on a custom app but public apps should be the same. I'm using a Remix app with the latest Shopify packages and it turned out to be quite straightforward to add the webhooks (described in the docs here), but there were a couple of things that caught me out when getting them registered.
Once you have the webhook added in your shopifyApp config (shopify.server.ts), and the webhook handler (in webhooks.tsx):
- make sure you have added the access scopes you need in both the app toml file and in the .env on your server (or wherever you have these set up in production). If these access scopes don't match between the toml file and the server your app will go into an auth loop and it will display a browser cookies error. I wasted a few hours on this.
Another thing to note - webhooks that have been added after the app is already installed on a store don't seem to register without OAuth being triggered again, so I've found that visiting the /auth route in the app will register the webhook.
Hope that helps!
Hi @originmaster I would like to inquire about the authentication process for this part. I have created a webhook for the "create order" topic, and the webhook's address points to an endpoint within my Shopify app. However, whenever an order is created, the webhook does not trigger the endpoint, and an error is thrown stating that the shop is not provided.
Could you please advise on how to resolve this issue and ensure that the URL is accessible? It is worth noting that the app has multiple endpoints that are authorized and utilize the Shopify access token without any issues.
@Mariam_Rashad22 If I was you, rather than creating an extra endpoint to handle your "create order" webhook, use the existing `/webhooks` endpoint that is already set up within the app and handle the `ORDERS_CREATE` topic in there with the rest of the webhook topics. Hope that helps