skip to Main Content

I have a shopify store mystore and I have an nodejs app myapp. I need to do is when something happens on mystore a webhook will be created/registered in my nodejs app. I have tried https://www.npmjs.com/package/@shopify/koa-shopify-webhooks this package but it is not working for me and I don’t think that it is the same thing that I want. I just want that when let suppose order is created in store a webhook is registered.

2

Answers


  1. You can not create/register a new webhook when the order created.

    Webhooks are a tool for retrieving and storing data from a certain event. They allow you to register an https:// URL where the event data can be stored in JSON or XML formats. Webhooks are commonly used for:

    • Placing an order
    • Changing a product’s price
    • Notifying your IM client or your pager when you are offline
    • Collecting data for data-warehousing
    • Integrating your accounting software
    • Filtering the order items and informing various shippers about the order
    • Removing customer data from your database when they uninstall your app
    Login or Signup to reply.
  2. if you just have to register a webhook you can use this code.
    You just have to change the webhook topic and the endpoint.

    This is for orders/create webhook registration

    add shopify-api-node and request-promise packages and require them

    const ShopifyAPIClient = require("shopify-api-node");
    const request = require("request-promise");
    

    then

    const createOrderWebhook = await registerWebhook(yourShopDomain, yourShopAccessToken, {
        topic: "orders/create",
        address: "Your node app end point" //www.example.com/webhooks/createOrder,
        format: "json",
    });
    

    add your registerWebhook function

    const registerWebhook = async function (shopDomain, accessToken, webhook) {
      const shopify = new ShopifyAPIClient({
        shopName: shopDomain,
        accessToken: accessToken,
      });
      const isCreated = await checkWebhookStatus(shopDomain, accessToken, webhook);
      if (!isCreated) {
        shopify.webhook.create(webhook).then(
          (response) => console.log(`webhook '${webhook.topic}' created`),
          (err) =>
            console.log(
              `Error creating webhook '${webhook.topic}'. ${JSON.stringify(
                err.response.body
              )}`
            )
        );
      }
    };
    

    for checking the webhook already not created at Shopify you can use following code

    const checkWebhookStatus = async function (shopDomain, accessToken, webhook) {
      try {
        const shopifyWebhookUrl =
          "https://" + shopDomain + "/admin/api/2020-07/webhooks.json";
        const webhookListData = {
          method: "GET",
          url: shopifyWebhookUrl,
          json: true,
          headers: {
            "X-Shopify-Access-Token": accessToken,
            "content-type": "application/json",
          },
        };
        let response = await request.get(webhookListData);
        if (response) {
          let webhookTopics = response.webhooks.map((webhook) => {
            return webhook.topic;
          });
          return webhookTopics.includes(webhook.topic);
        } else {
          return false;
        }
      } catch (error) {
        console.log("This is the error", error);
        return false;
      }
    };
    

    Happy coding 🙂

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search