Skip to content

Webhooks

Igor Balos edited this page Nov 7, 2019 · 9 revisions

For these API requests you will need to use a server API token. Once you obtain it, you will need to use server API client.

server_token = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
client = Postmark::ApiClient.new(server_token)

Get list of available webhooks

client.get_webhooks()

# => [{:id=>12345,:url=>"http://example.com",:message_stream=>"outbound",:triggers=>{"Open"=>{"Enabled"=>false, "PostFirstOpenOnly"=>false}, "Click"=>{"Enabled"=>false}, "Delivery"=>{"Enabled"=>false}, "Bounce"=>{"Enabled"=>false, "IncludeContent"=>false}, "SpamComplaint"=>{"Enabled"=>false, "IncludeContent"=>false}}},{:id=>23456,:url=>"http://example.com",:message_stream=>"outbound",:triggers=>{"Open"=>{"Enabled"=>false, "PostFirstOpenOnly"=>false}, "Click"=>{"Enabled"=>false}, #"Delivery"=>{"Enabled"=>false}, "Bounce"=>{"Enabled"=>false, "IncludeContent"=>false}, "SpamComplaint"=>{"Enabled"=>false, "IncludeContent"=>false}}}]

Get webhook by ID

You can get the full webhook information by using it's ID.

# get webhook by ID
client.get_webhook(12345)

# => {:id=>12345,:url=>"http://example.com", :message_stream=>"outbound",:triggers=>{"Open"=>{"Enabled"=>false, "PostFirstOpenOnly"=>false}, "Click"=>{"Enabled"=>false}, "Delivery"=>{"Enabled"=>false}, "Bounce"=>{"Enabled"=>false, "IncludeContent"=>false}, "SpamComplaint"=>{"Enabled"=>false, "IncludeContent"=>false}}}

Create a new webhook

You can easily create a new webhook with create_webhook method:

client.create_webhook(
    {:url=>"http://example.com",
     :message_stream=>"outbound",
     triggers: {
         Open: { Enabled: true, PostFirstOpenOnly: true },
         Click: { Enabled: true },
         Delivery: { Enabled: true},
         Bounce: { Enabled: true, IncludeContent: false },
         SpamComplaint: { Enabled: true, IncludeContent: false }
     }
    })

# => {:id=>12345,:url=>"http://example.com", :message_stream=>"outbound",:triggers=>{"Open"=>{"Enabled"=>false, "PostFirstOpenOnly"=>false}, "Click"=>{"Enabled"=>false}, "Delivery"=>{"Enabled"=>false}, "Bounce"=>{"Enabled"=>false, "IncludeContent"=>false}, "SpamComplaint"=>{"Enabled"=>false, "IncludeContent"=>false}}}

Update webhook by ID

You can update webhook by identifying it by ID and providing data you wish to update.

client.update_webhook(12345, {:url=>"http://test_updated.com"})

# => {:id=>12345,:url=>"http://example.com", :message_stream=>"outbound",:triggers=>{"Open"=>{"Enabled"=>false, "PostFirstOpenOnly"=>false}, "Click"=>{"Enabled"=>false}, "Delivery"=>{"Enabled"=>false}, "Bounce"=>{"Enabled"=>false, "IncludeContent"=>false}, "SpamComplaint"=>{"Enabled"=>false, "IncludeContent"=>false}}}

Delete webhook by ID

client.delete_webhook(12345)

# => {:error_code=>0, :message=>"Webhook 12345 removed."}