Recreate shopify webhooks

sugiarto - Sep 10 - - Dev Community

When developing custom Shopify apps, I usually use ngrok as a reverse proxy for webhooks integration. Since I always use a free service plan from Ngrok, then the URL address always changes. Here is the sample generated Ngrok URL when running

ngrok http 3000
Enter fullscreen mode Exit fullscreen mode

Generated endpoint
https://73d3-2001-448a-3032-c93e-bc56-f800-e58d-8a98.ngrok-free.app

When we hit ctrl+c and rerun the command, we will get a different new URL.

So what I did was just change the .env file of my Rails project, go to console, and recreate Shopify webhooks.

bundle exec rails c
Enter fullscreen mode Exit fullscreen mode
Shop.first.recreate_webhooks!
Enter fullscreen mode Exit fullscreen mode

And here is the code for recreate_webhooks!

# app/models/shop.rb

  def recreate_webhooks!
    ShopifyAPI::Webhook.all.each do |webhook|
      webhook.destroy
    end

    ShopifyApp.configuration.webhooks.each do |item|
      webhook = ShopifyAPI::Webhook.new(item)
      if webhook.save
        puts "webhook #{item} created."
      else
        puts "webhook #{item} failed."
      end
    end
  end
Enter fullscreen mode Exit fullscreen mode

So every time you need to regenerate webhooks, you can just run this method from rails console.

. . . . . . .
Terabox Video Player