Recreate shopify webhooks



This content originally appeared on DEV Community and was authored by sugiarto

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

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
Shop.first.recreate_webhooks!

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

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


This content originally appeared on DEV Community and was authored by sugiarto