How can I set up a product change notification webhook using the new CLI?

HeoTer
Shopify Partner
1 0 0

I'm trying to extend the code provided by the Shopify CLI for app creation and able to get webhook notifications from Shopify. 

app.post(
  shopify.config.webhooks.path,
  shopify.processWebhooks({ webhookHandlers: GDPRWebhookHandlers })
);

GDPRWebhookHandlers file

import { DeliveryMethod } from "@shopify/shopify-api";

export default {

  CUSTOMERS_DATA_REQUEST: {
    deliveryMethod: DeliveryMethod.Http,
    callbackUrl: "/api/webhooks",
    callback: async (topic, shop, body, webhookId) => {
      const payload = JSON.parse(body);
      console.log(payload)
    
    },
  },
  CUSTOMERS_REDACT: {
    deliveryMethod: DeliveryMethod.Http,
    callbackUrl: "/api/webhooks",
    callback: async (topic, shop, body, webhookId) => {
      const payload = JSON.parse(body);
   
    },
  },

  SHOP_REDACT: {
    deliveryMethod: DeliveryMethod.Http,
    callbackUrl: "/api/webhooks",
    callback: async (topic, shop, body, webhookId) => {
      const payload = JSON.parse(body);
    },
  },
  PRODUCTS_UPDATE: {
    deliveryMethod: DeliveryMethod.Http,
    callbackUrl: "/api/webhooks",
    callback: async (topic, shop, body, webhookId) => {
      const payload = JSON.parse(body);
      console.log('PRODUCTS_UPDATE', payload);
    },
  },
};

What do I need to do to set up a webhook that fires when a product changes and so that I can get information about the changed product?

Replies 4 (4)

Spring_Season
Shopify Partner
2 0 0

As I see you already added the new webhook you want to subscribe to and added the callback function to it. You just have to uninstall and install again the app, so the webhook subscription can be made by the app.

btrain-004
Shopify Partner
12 0 3

Adding onto what @Spring_Season said. You are only adding the route and is ready to listen, but you haven't registered the webhook yet. Unfortunately listing the webhooks in the GDPRWebhookHandlers file will only "automatically" register on installation.

 

This is fine if you have a simple app and never really upgrade. But if you do you'll have to register the webhook.

 

const callbackResponse = await shopify.auth.callback({
   rawRequest: req,
   rawResponse: res,
});

const
response = await shopify.webhooks.register({ session: callbackResponse.session, });

 I found the snippet here in their docs. Setting up webhooks

btrain-004
Shopify Partner
12 0 3
jairogm
Shopify Partner
1 0 0

So, let's say I want to add a Webhook for when the user uninstalls the app, it will be okay to add it on the GDPRWebhooksHandlers Right?