DEV Community

sugiarto
sugiarto

Posted on

Recreate shopify webhooks

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.

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay