Explore new webhook event options, detailed setup, specific content triggers, and improved logging and retry mechanisms
With Umbraco, you can create your own webhook events.
This documentation guides you through the process of implementing your webhook events using the WebhookEventBase<TNotification>
base class.
The WebhookEventBase<TNotification>
class serves as the foundation for creating custom webhook events. Here's a brief overview of its key components:
Alias: The property that must be overridden to provide a unique identifier for your webhook event.
EventName: A property that represents the name of the event. It is automatically set based on the provided alias unless explicitly specified.
EventType: A property that categorizes the event type. It defaults to "Others" but can be customized using the WebhookEventAttribute
.
WebhookSettings: The property containing the current webhook settings.
ProcessWebhooks: The method responsible for processing webhooks for a given notification.
ShouldFireWebhookForNotification: The method determining whether webhooks should be fired for a specific notification.
ConvertNotificationToRequestPayload: An optional method allowing customization of the notification payload before sending it to webhooks.
To create a custom webhook event, follow these steps:
Derive from WebhookEventBase<TNotification>
:
Override the Alias Property:
Provide a unique identifier for your event using the Alias
property:
Apply WebhookEventAttribute
(Optional):
You can use the WebhookEventAttribute
to specify the event name and type. Apply this attribute to your custom event class:
Umbraco already has some types as constants, which you can find at Constants.WebhookEvents.Types
. If you do not specify this attribute, the event name will default to your alias, and the type will default to Other
.
Implement Notification Handling:
If needed, customize the handling of the notification in the HandleAsync
method.
Register Your Webhook Event:
Ensure that Umbraco is aware of your custom event by registering it in a composer:
Implement Optional Overrides: Depending on your requirements, you can override methods such as ConvertNotificationToRequestPayload
and ShouldFireWebhookForNotification
to customize the behavior of your webhook event.
Here's a basic example of a custom webhook event:
For scenarios where your webhook event is content-specific, Umbraco provides another base class: WebhookEventContentBase<TNotification, TEntity>
. This class is an extension of the generic WebhookEventBase<TNotification>
and introduces content-related functionalities.
The WebhookEventContentBase<TNotification, TEntity>
class is designed for content-specific webhook events, where TEntity
is expected to be a type that implements the IContentBase
interface.
To leverage the WebhookEventContentBase<TNotification, TEntity>
class, follow these steps:
Derive from WebhookEventContentBase<TNotification, TEntity>
:
Override the Required Methods:
GetEntitiesFromNotification: Implement this method to extract content entities from the notification.
ConvertEntityToRequestPayload: Implement this method to customize the content entity payload before sending it to webhooks.
If we take a look at the ContentPublishedWebhookEvent
, we can see how these methods are overriden.
ProcessWebhooks Implementation:
The ProcessWebhooks
method in this class has been enhanced to iterate through content entities obtained from the notification. It checks the content type of each entity against the specified webhook's content type keys, firing webhooks only for matching entities.
Umbraco webhooks enable seamless integration and real-time updates by notifying external services about content changes and events within the Umbraco CMS
Webhooks provide real-time, event-driven communication within Umbraco. Seamlessly integrated, these lightweight, HTTP-based notifications empower you to trigger instant actions and synchronize data. With its different extension points, you can tailor these webhooks to fit a broad range of requirements.
To work with Webhooks, go to Webhooks in the Settings section.
To create a webhook, click the Create
button. It will take you to the Create webhook screen.
The Url
should be the endpoint you want the webhook to send a request to, whenever a given Event
is fired.
Events are when a given action happens. By default, there are 5 events you can choose from.
Content Published - This event happens whenever some content gets published.
Content Unpublished - This event happens whenever some content gets unpublished
Content Deleted - This event happens whenever some content gets deleted.
Media Deleted - This event happens whenever a media item is deleted.
Media Saved - This event happens whenever a media item is saved.
For Content or Media events, you can specify if your webhook should trigger only for specific Document or Media types.
For example, if you have selected the Content Published
event, you can set your webhook to trigger only for specific content types.
You can specify custom headers that will be sent with your request.
For example, you can specify Accept: application/json
, security headers, and so on.
Umbraco webhooks are configured with some defaults, such as default headers or some events that send a payload. In this section, we will take a look at those.
For example, the Content Published
event sends the specific content that triggered the event. The JSON format is identical to the Content Delivery API
. Here’s an example of a JSON object:
However, the Content deleted
does not send the entire content as JSON, instead, it sends the Id
of the content:
By default, webhook requests includes 3 headers:
user-agent: Umbraco-Cms/{version}
- where version is the current version of Umbraco.
umb-webhook-retrycount: {number of retries}
- where number of retries is the current retry count for a given webhook request.
umb-webhook-event: {Umbraco.event}
- where event is the event that triggered the request. For example, for Content published: umb-webhook-event: Umbraco.ContentUnpublish
To add more than the default events to Umbraco, you can leverage the provided IUmbracoBuilder
and IComposer
interfaces. Below is an example of how you can extend the list of available webhook events using a custom WebhookComposer
:
This is a list of all the current events that are available through Umbraco. If you want them all enabled, use the following:
Sometimes it is desirable to modify one of the standard Umbraco webhooks, for example, to change the Payload. This can be done by adding a custom implementation, as shown in the code example below:
Add the following line in a Composer to replace the standard Umbraco implementation with your custom implementation:
Configure Webhook settings in your appsettings.*.json
and is in the Umbraco::CMS
section:
Enabled: Specifies whether webhooks are enabled.
MaximumRetries: Defines the number of retries for a webhook request.
Period: The interval between checks for any webhook requests that need to be fired.
EnableLoggingCleanup: Indicates whether webhook log cleanup is enabled.
KeepLogsForDays: Determines the number of days to retain webhook logs.