Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can we use products webhook ? #25

Closed
Anshu-Shar opened this issue Mar 5, 2024 · 9 comments
Closed

How can we use products webhook ? #25

Anshu-Shar opened this issue Mar 5, 2024 · 9 comments
Assignees

Comments

@Anshu-Shar
Copy link

Hello @Mini-Sylar ,
How can we use products and order_create webhooks like 'topic' => 'products/update','topic' => 'orders/create' in this template code ?

@Mini-Sylar Mini-Sylar self-assigned this Mar 5, 2024
@Mini-Sylar
Copy link
Owner

Mini-Sylar commented Mar 5, 2024

@Anshu-Shar

First of all when testing on local, shopify won't allow you to send webhooks through their cloudflare tunnels

so remember we installed ngrok if you havent refer to download ngrok

after you have ngrok setup do the following

Step 1

in your terminal run nrgok http 3000

Step 2

Modify the dev command in your root package.json the when next to README.md as follows

"dev": "shopify app dev --tunnel-url=https://[YOUR NGROK APP].ngrok-free.app:3000

  • note: the :3000 at the end is important
  • note 2: You may have to update the ngrok url everytime you close ngrok

Step 3

run npm run dev -- --reset to reset the preview links (don't forget to revert when in production)

Step 4

  • You can see an example of the webhook call with gdpr.js in web, but I prefer to have all of this in a folder called webhooks move gdpr,js to that folder and rename to webhook-handlers.js if you want but I will use webhook-handlers.js
  • modify the import statements in index.js as follows
 import webhookHooks from "./webhooks/webhook-handlers.js";
// Set up Shopify authentication and webhook handling

app.post(
  shopify.config.webhooks.path,
// change from `GDPRWebhookHandlers` to `webhookHooks`
  shopify.processWebhooks({
    webhookHandlers: webhookHooks,
  })
);

Step 5

in webhook-handlers.js we can listen for ORDERS_CREATE for instance as follows

 ORDERS_CREATE: {
    deliveryMethod: DeliveryMethod.Http,
    callbackUrl: "/api/webhooks",
    callback: async (topic, shop, body, webhookId) => {
      do something with that data like console.log(topic, shop, body)
    },
  },

VERY IMPORTANT

According to shopify webhook data can be duplicated, so one way to handle that will be store the webhookID in your database, when you receive one check if that webhookID already exists. If it does throw an error

Also to get your app approved you need to verify your webhooks,
Validating webhooks
write a function to validate your webhooks using the HMAC header lets say validateWebhookRequest

use it as a middleware in index.js

import { validateWebhookRequest } from "somewhere";

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

@Anshu-Shar
Copy link
Author

Thanks for the quick reply

@Anshu-Shar
Copy link
Author

@Anshu-Shar

First of all when testing on local, shopify won't allow you to send webhooks through their cloudflare tunnels

so remember we installed ngrok if you havent refer to download ngrok

after you have ngrok setup do the following

Step 1

in your terminal run nrgok http 3000

Step 2

Modify the dev command in your root package.json the when next to README.md as follows

"dev": "shopify app dev --tunnel-url=https://[YOUR NGROK APP].ngrok-free.app:3000

  • note: the :3000 at the end is important
  • note 2: You may have to update the ngrok url everytime you close ngrok

Step 3

run npm run dev -- --reset to reset the preview links (don't forget to revert when in production)

Step 4

  • You can see an example of the webhook call with gdpr.js in web, but I prefer to have all of this in a folder called webhooks move gdpr,js to that folder and rename to webhook-handlers.js if you want but I will use webhook-handlers.js
  • modify the import statements in index.js as follows
 import webhookHooks from "./webhooks/webhook-handlers.js";
// Set up Shopify authentication and webhook handling

app.post(
  shopify.config.webhooks.path,
// change from `GDPRWebhookHandlers` to `webhookHooks`
  shopify.processWebhooks({
    webhookHandlers: webhookHooks,
  })
);

Step 5

in webhook-handlers.js we can listen for ORDERS_CREATE for instance as follows

 ORDERS_CREATE: {
    deliveryMethod: DeliveryMethod.Http,
    callbackUrl: "/api/webhooks",
    callback: async (topic, shop, body, webhookId) => {
      do something with that data like console.log(topic, shop, body)
    },
  },

VERY IMPORTANT

According to shopify webhook data can be duplicated, so one way to handle that will be store the webhookID in your database, when you receive one check if that webhookID already exists. If it does throw an error

Also to get your app approved you need to verify your webhooks, Validating webhooks write a function to validate your webhooks using the HMAC header lets say validateWebhookRequest

use it as a middleware in index.js

import { validateWebhookRequest } from "somewhere";

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

Please provide the code for validateWebhookRequest,

@Mini-Sylar
Copy link
Owner

Since I don't know exactly how your app works, I can't provide a "validateWebhookRequest" implementation

However you can take a look at this github issue to get started Validating Webhooks

@Mini-Sylar
Copy link
Owner

Marking this as closed

@Anshu-Shar
Copy link
Author

Since I don't know exactly how your app works, I can't provide a "validateWebhookRequest" implementation

However you can take a look at this github issue to get started Validating Webhooks

@Anshu-Shar
Copy link
Author

OK,I have implemented the above steps in my code for webhook.If I am trying to uninstall the app getting the error.
Failed To Process Webhook: Error: No Body Was Received When Processing Webhook

@Mini-Sylar Mini-Sylar reopened this Mar 17, 2024
@Mini-Sylar
Copy link
Owner

Mini-Sylar commented Mar 17, 2024

the webhooks take a string as the body and parse them, make sure any middleware that parses the data to JSON comes AFTER

const app = express();

app.get(shopify.config.auth.path, shopify.auth.begin());
app.get(
  shopify.config.auth.callbackPath,
  shopify.auth.callback(),
  shopify.redirectToShopifyOrAppRoot()
);
app.post(
  shopify.config.webhooks.path,
  shopify.processWebhooks({ webhookHandlers: YOUR_WEBHOOK_HANDLERS})
);

// middlewares should come after this

@Anshu-Shar
Copy link
Author

the webhooks take a string as the body and parse them, make sure any middleware that parses the data to JSON comes AFTER

const app = express();

app.get(shopify.config.auth.path, shopify.auth.begin());
app.get(
  shopify.config.auth.callbackPath,
  shopify.auth.callback(),
  shopify.redirectToShopifyOrAppRoot()
);
app.post(
  shopify.config.webhooks.path,
  shopify.processWebhooks({ webhookHandlers: YOUR_WEBHOOK_HANDLERS})
);

// middlewares should come after this

Thanks for response
It is working now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants