-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Closes #20003: Introduce mechanism to register callbacks for webhook context #20025
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b24d302
Closes #20003: Introduce mechanism to register callbacks for webhook …
jeremystretch 55f5b15
Swap ContentType with ObjectType
jeremystretch 86ad4e6
Add plugin dev documentation for webhook callbacks
jeremystretch 28a7101
Merge branch 'feature' into 20003-webhook-context
jeremystretch fb9f053
Fix tests
jeremystretch c9067ec
Add note about namespacing webhook data
jeremystretch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# Webhooks | ||
|
||
NetBox supports the configuration of outbound [webhooks](../../integrations/webhooks.md) which can be triggered by custom [event rules](../../features/event-rules.md). By default, a webhook's payload will contain a serialized representation of the object, before & after snapshots (if applicable), and some metadata. | ||
|
||
## Callback Registration | ||
|
||
Plugins can register callback functions to supplement a webhook's payload with their own data. For example, it might be desirable for a plugin to attach information about the status of some objects at the time a change was made. | ||
|
||
This can be accomplished by defining a function which accepts a defined set of keyword arguments and registering it as a webhook callback. Whenever a new webhook is generated, the function will be called, and any data it returns will be attached to the webhook's payload under the `context` key. | ||
|
||
### Example | ||
|
||
```python | ||
from extras.webhooks import register_webhook_callback | ||
from my_plugin.utilities import get_foo_status | ||
|
||
@register_webhook_callback | ||
def set_foo_status(object_type, event_type, data, request): | ||
if status := get_foo_status(): | ||
return { | ||
'foo': status | ||
} | ||
``` | ||
|
||
The resulting webhook payload will look like the following: | ||
|
||
```json | ||
{ | ||
"event": "updated", | ||
"timestamp": "2025-08-07T14:24:30.627321+00:00", | ||
"object_type": "dcim.site", | ||
"username": "admin", | ||
"request_id": "49e3e39e-7333-4b9c-a9af-19f0dc1e7dc9", | ||
"data": { | ||
"id": 2, | ||
"url": "/api/dcim/sites/2/", | ||
... | ||
}, | ||
"snapshots": {...}, | ||
"context": { | ||
"foo": 123 | ||
} | ||
} | ||
``` | ||
|
||
!!! note "Consider namespacing webhook data" | ||
The data returned from all webhook callbacks will be compiled into a single `context` dictionary. Any existing keys within this dictionary will be overwritten by subsequent callbacks which include those keys. To avoid collisions with webhook data provided by other plugins, consider namespacing your plugin's data within a nested dictionary as such: | ||
|
||
```python | ||
return { | ||
'my_plugin': { | ||
'foo': 123, | ||
'bar': 456, | ||
} | ||
} | ||
``` | ||
|
||
### Callback Function Arguments | ||
|
||
| Name | Type | Description | | ||
|---------------|-------------------|-------------------------------------------------------------------| | ||
| `object_type` | ObjectType | The ObjectType which represents the triggering object | | ||
| `event_type` | String | The type of event which triggered the webhook (see `core.events`) | | ||
| `data` | Dictionary | The serialized representation of the object | | ||
| `request` | NetBoxFakeRequest | A copy of the request (if any) which resulted in the change | | ||
|
||
## Where to Define Callbacks | ||
|
||
Webhook callbacks can be defined anywhere within a plugin, but must be imported during plugin initialization. If you wish to keep them in a separate module, you can import that module under the PluginConfig's `ready()` method: | ||
|
||
```python | ||
def ready(self): | ||
super().ready() | ||
from my_plugin import webhook_callbacks | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from extras.webhooks import register_webhook_callback | ||
|
||
|
||
@register_webhook_callback | ||
def set_context(object_type, event_type, data, request): | ||
return { | ||
'foo': 123, | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.