Solved

Webhooks: call external site/URL?

rodrigograca31
Tourist
4 2 0

Hi everyone.
Im a brand new shopify dev.

Im building an app and I need to call our already built backend every time theres a product update on a given shop.

This seems to be the code for it: (which works but doesnt.....)

const response2 = await Shopify.Webhooks.Registry.register({
					path: "/webhook",
					topic: "PRODUCTS_UPDATE",
					accessToken,
					shop,
					webhookHandler: () => true,
				});

				if (!response2.success) {
					console.log(
						`Failed to register PRODUCTS_UPDATE webhook: ${JSON.stringify(
							response
						)}`
					);
				}

 

`/webhook` is relative to the URL that this app runs under... (local tunnel or ngrock or a given URL when its deployed)

what if I want to call another server instead? Its just me or thats not a thing? because after all `/webhook` is turned into https://my-app.loca.lt/webhook here: https://github.com/Shopify/shopify-node-api/blob/975b87985c57cc723488a9f712de39a28ce2c967/src/webhoo... 

what I mean to do is:

const response2 = await Shopify.Webhooks.Registry.register({
					path: "https://MY-SERVER.com/webhook",
					topic: "PRODUCTS_UPDATE",
					accessToken,
					shop,
					webhookHandler: () => true
				});

				if (!response2.success) {
					console.log(
						`Failed to register PRODUCTS_UPDATE webhook: ${JSON.stringify(
							response
						)}`
					);
				}

but this doesnt seem to be working....

something tells me this isnt possible but it should be since theres another non programmatic way of doing the webhooks which literally asks/accepts an URL:

rodrigograca31_0-1635521435816.png

https://help.shopify.com/en/manual/orders/notifications/webhooks 

Looking at the library source code: 
https://github.com/Shopify/shopify-node-api/blob/main/src/webhooks/registry.ts 

It looks like I should be able to pass `deliveryMethod: DeliveryMethod.Http,` but if thats the method it will build the URL for me on line 276....

Welp?! 🤔😂

Accepted Solution (1)
rodrigograca31
Tourist
4 2 0

This is an accepted solution.

From what I figured currently there isnt a way to specify the full path using the shopify library.

So what I ended up doing was saving the access token and making the request from our backend.

View solution in original post

Replies 3 (3)

james-langille
Shopify Staff (Retired)
70 15 27

Hello, @rodrigograca31 ! Welcome to the developer community.

There are two ways to create webhooks at Shopify:

  1. In the shop admin page (the docs you listed at https://help.shopify.com/en/manual/orders/notifications/webhooks), which will create a webhook for a particular shop with an HTTP URL for where to deliver the webhook.
  2. Using our APIs at https://shopify.dev/api/admin-rest/2021-10/resources/webhook for building apps, which will then create subscriptions for every store that your application is installed on.

I think looking into #2 will help you get started down the application path, since #1 is for a very different use case for individual merchants. https://shopify.dev/apps/webhooks May also be useful to get an idea of what webhooks are and how they work.

Hopefully this can help you get started!

To learn more visit the Shopify Help Center or the Community Blog.

rodrigograca31
Tourist
4 2 0

This is an accepted solution.

From what I figured currently there isnt a way to specify the full path using the shopify library.

So what I ended up doing was saving the access token and making the request from our backend.

destpat
Shopify Partner
7 0 1

Hi James,

 

Could you tell me if it's possible to add an option { url: string } in the method Shopify.Webhooks.Registry.register. This will be so helpful especially in a case when you want to trigger your webhook in external server.