Search code examples
shopifywebhooksshopify-appshopify-api-node

'Unexpected end of JSON input' error while working with Shopify webhooks


I've created an app and try to register for webhooks, and then fetch the list of all webhooks. I use this code for this (/server/middleware/auth.js):

const webhook = new Webhook({ session: session });
      webhook.topic = "products/update";
      webhook.address = "https://api.service.co/items/update";
      webhook.format = "json";
      console.log("registering products/update");
      try {
        await webhook.save({
          update: true,
        });
      } catch (error) {
        console.log(error);
      }

      const webhookSecond = new Webhook({ session: session });
      webhookSecond.topic = "products/create";
      webhookSecond.address = "https://api.service.co/items/webhooks";
      webhookSecond.format = "json";

      console.log("registering products/create");

      try {
        await webhookSecond.save({
          update: true,
        });
      } catch (error) {
        console.log(error);
      }

      console.log("getting all webhooks");
      try {
        let webhooks = await Webhook.all({
          session: session,
        });
        console.log(webhooks);
      } catch (error) {
        console.log(error);
      }

Everything works fine for a development store. However, when I try to launch this script on a third-party customer store, then I get this error:

HttpRequestError: Failed to make Shopify HTTP request: FetchError: invalid json response body at https://shopname.myshopify.com/admin/api/2022-04/webhooks.json reason: Unexpected end of JSON input

The app permissions/scopes are: read_checkouts, read_orders, read_inventory, read_products, read_customers

I got this error 3 times, even for Webhook.all.

Could you please tell me what can cause this error, and how could it be fixed?


Solution

  • This error was caused by the lack of access provided by the owner of the store to my collaborator developer account. Manage settings access was required.