Need help to create API level Webhooks

Shopify Partner
18 0 0

Hi Everyone,

I am new to Shopify, and working on a shop right now. I am developing an app (shopify_app example using RoR) where I need to use the API level Webhooks.

I have read the Shopify Webhook documents, But I am completely dont understand it.

can some one write/help me on how to create for one 'orders/create' topic

few question that I have 

In which controller I have to create the webhooks, How can I confirm that Webhook is created, how can i delete it

I have tried few examples and ended up with frustation. I just need complete example for topic 'orders/create' so that I can explore.

I have tried the following code, But I completely dont understand what is happening 

webhook = ShopifyAPI::Webhook.new(:format => "json", :topic => "products/update", :address => "http://xx.xx.xx.xx/webhooks/api.php";)

raise "Webhook invalid: #{webhook.errors}" unless webhook.valid?

puts webhook # printing #<ShopifyAPI::Webhook:0x007fd7840ace78> which I dont understand what is happening

Please some one help me.

Thanks in advance.

0 Likes
Highlighted
Shopify Staff
Shopify Staff
582 0 45

You are printing out the to_s representation of an instance (which normally looks something like #<Object:MemoryAddress>) of the ShopifyAPI::Webhook resource.

What you want to do is:

# what you did with ShopifyAPI::Webhook.new

puts webhook.persisted? # => false

webhook.save

puts webhook.persisted? => true

ShopifyAPI::Webhook.find(:all).each do |wh|

  puts "Found it! #{wh.id}" if wh.id == webhook.id

end

 

0 Likes
Shopify Partner
18 0 0

Hi Chris,

I have added the code which you have shared and I have got the following output, it seems working. can you please check my out put and let me know everything goes fine .

puts webhook.persisted? # => false
webhook.save
puts webhook.persisted? => true
ShopifyAPI::Webhook.find(:all).each do |wh|
puts "Found it! #{wh.id}" if wh.id == webhook.id
    puts " \n\n #{wh.address} will receive #{wh.format.upcase} requests for #{wh.topic} topic with Webhook ID #{wh.id}. \n\n "
end

Out put :

false

{false=>true}
 
 http://xx.xx.xx.xxx/webhooks/mo.php will receive JSON requests for products/update topic with Webhook ID 8336927. 
 http://xx.xx.xx.xxx/webhooks/mo.php will receive JSON requests for products/create topic with Webhook ID 8337107. 
 http://xx.xx.xx.xxx/webhooks/mo.php will receive JSON requests for products/delete topic with Webhook ID 8337111. 

 And Also a small help needed again

I think now Webhooks are getting created, and I have to execute my scripts (http://xx.xx.xx.xxx/webhooks/mo.php) once 'products/update' is done in my shop. where should i write the function and how to specify the routs for this.

your help is highly appreciated :)

Thanks

0 Likes