1
votes

I'm trying to create an 'app/uninstall' webhook in my Shopify app. The app boilerplate was generated using the Shopify API gem(https://github.com/Shopify/shopify_app), and I've followed the instructions in their readme to the letter.

To create these webhooks, I'm assuming they'd have to be somewhere when the client shop first connects with the app - but I'm not sure where exactly that takes place in the whole thing. I've pushed the boilerplate code here: https://github.com/shabbirun/shopify-help-app

I first guessed that the code should be in the Shop model, so I tried implementing it using :after_create , but I'm getting an error.

Any ideas as to where I can place the code?

Thank you!

2

2 Answers

0
votes

You will need to post/put a new webhook through the API.

Example code is in node: (ruby will be different)

      shopify.post('/admin/webhooks.json', {
        webhook: {
          topic: 'app/uninstalled',
          address: 'https://yoururl.com/shopify/uninstall',
          format: 'json'
        }
      }, done);

Once the admin webhook has been added, anytime a user uninstalls the app it will hit your api route for shopify uninstalls.

In terms of placing the code we recommend doing it when a user first approves your app, whichever route that may be.

https://docs.shopify.com/api/webhook

0
votes

To register a webhook, you can run the add_webhook generator or add a line manually in the shopify_app.rb initializer:

config.webhooks = [
  {topic: 'app/uninstalled', address: 'https://myapp.url/webhooks/app_uninstalled', format: 'json'},
  {topic: 'orders/create', address: 'https://myapp.url/webhooks/orders_create', format: 'json'},
]

With this setup, your app will register the webhooks automatically when your app gets installed in a shop.