# Can a plugin add a webhook?

**URL:** <https://meta.discourse.org/t/can-a-plugin-add-a-webhook/81361>\
**Category:** Development\
**Created:** [February 23, 2018, 10:10pm UTC](https://meta.discourse.org/t/can-a-plugin-add-a-webhook/81361 "2018-02-23T22:10:51Z")\
**Posts on this page:** 1\
**Showing post:** 8

<div class="post-metadata">

**Author:** ![jack2](https://avatars.discourse-cdn.com/v4/letter/j/ac91a4/32.png) [@jack2](https://meta.discourse.org/u/jack2)\
**Post date:** [February 27, 2018, 11:27am UTC](https://meta.discourse.org/t/can-a-plugin-add-a-webhook/81361/8 "2018-02-27T11:27:33Z")

</div>

Here is what I did eventually:

Since I need standard notifications for topic creation and deletion, I figured out Discourse already enqueues those, as part of the standard webhook feature. So there’s no need to do it myself.

What remains to be done, as @pfaffman suggested, is to add the webhook to the database. However, the proposed code fails silently, probably because of missing and/or incorrect parameters.

Here is my full working code:

```ruby
#customization:plugin.rb

# Add our webhook automatically, so that admin doesn't have to do it manually
after_initialize do
	# Delete our previously installed webhook (reserved id = 1000)
	WebHook.where(id: 1000).destroy_all

	# Create the webhook (reserved id = 1000)
	hook = WebHook.new(
		id: 1000, # Our reserved id						
		payload_url: 'https://hfzeoihifzh/',
		content_type: 1, # 1 = 'application/json'
		secret: "min12characters",
		active: true,
		verify_certificate: true,
		web_hook_event_type_ids: [1] # 1 = topic events
	)
	hook.save	
end

```

---

_[View the full topic](https://meta.discourse.org/t/can-a-plugin-add-a-webhook/81361)._
