Cannot register GDPR webhook with React puplic app

Hello there,

I tried to register the mandatory GDPR webhooks, so I can answer with the status code 200.

I’m working on a React app by following this article: https://shopify.dev/apps/getting-started/create.

Right next to the predefined webhook APP_UNINSTALLED which is already implemented per default when generating the project via CLI and which works perfectly fine

const response = await Shopify.Webhooks.Registry.register({
          shop,
          accessToken,
          path: "/webhooks",
          topic: "APP_UNINSTALLED",
          webhookHandler: async (topic, shop, body) =>
            delete ACTIVE_SHOPIFY_SHOPS[shop],
        });

I tried to do the same for the GDPR webhooks… which look like this in my code:

await Shopify.Webhooks.Registry.register({
          shop,
          accessToken,
          path: "/webhooks",
          topic: "SHOP_REDACT",
          webhookHandler: async (topic, shop, body) => {
            console.log('/webhooks/shop REDACT webhook triggered', topic, shop, body);
          }
        });

        await Shopify.Webhooks.Registry.register({
          shop,
          accessToken,
          path: "/webhooks",
          topic: "CUSTOMER_DATA_REQUEST",
          webhookHandler: async (topic, shop, body) => console.log('/webhooks/customers DATA_REQUEST webhook triggered', topic, shop, body)
        });

        await Shopify.Webhooks.Registry.register({
          shop,
          accessToken,
          path: "/webhooks",
          topic: "CUSTOMERS_REDACT",
          webhookHandler: async (topic, shop, body) =>
            console.log('/webhooks/customers REDACT webhook triggered', topic, shop, body)
        });

But I get the following error:

InternalServerError: Cannot read property ‘webhookSubscriptions’ of undefined

What am I doing wrong here?

So I was able to solve the problem by just not using the Shopify.Webhooks.Registry.register function and instead I’ve defined some endpoints which respond accordingly to the webhooks…

  1. In your Shopify admin/partners go to the app > App setup > GDPR mandatory webhooks
    and define the following endpoints but with your domain

  1. Define GDPR routers in server.js before the general webhook process router (I can post a gist if you find that easier to understand)
    This router only responds to the webhook, but nothing else (perfect for my case, because I don’t do anything with the merchants data…)
router.post("/webhooks/customers/data_request", webhook, async (ctx) => {
    try {
      await handle(ctx.req, ctx.res);
      ctx.respond = false;
      ctx.res.statusCode = 200;
    } catch (error) {
      console.log(`Failed to process webhook: ${error}`);
    }
  });

  router.post("/webhooks/customers/redact", webhook, async (ctx) => {
    try {
      await handle(ctx.req, ctx.res);
      ctx.respond = false;
      ctx.res.statusCode = 200;
    } catch (error) {
      console.log(`Failed to process webhook: ${error}`);
    }
  });

  router.post("/webhooks/customers/redact", webhook, async (ctx) => {
    try {
      await handle(ctx.req, ctx.res);
      ctx.respond = false;
      ctx.res.statusCode = 200;
    } catch (error) {
      console.log(`Failed to process webhook: ${error}`);
    }
  });

  // Handles the rest of the webhooks that are actually registered.
  router.post("/webhooks", webhook, async (ctx) => {
    try {
      await Shopify.Webhooks.Registry.process(ctx.req, ctx.res);
      console.log(`Webhook processed, returned status code 200`);
    } catch (error) {
      console.log(`Failed to process webhook: ${error}`);
    }
  });

Hope this helps

2 Likes

Can you pls share the full code

1 Like

yes help do we need to delete that store data ?