How to create a webhook

Magnolia Webhooks is a DX Core feature that is currently available as a developer preview. The Webhooks module is provided only for testing purposes in a preproduction beta version.

Installing with Maven

Maven is the easiest way to install the module. Add the following to your bundle:

<dependency>
  <groupId>info.magnolia.webhooks</groupId>
  <artifactId>magnolia-webhooks-core</artifactId>
  <version>1.0-beta</version>
</dependency>

The webhooks module requires at least the following dependencies, which will be included automatically if you use Maven to add the module to your webapp.

<dependency>
  <groupId>io.vavr</groupId>
  <artifactId>vavr</artifactId>
</dependency>
<dependency>
  <groupId>io.github.resilience4j</groupId>
  <artifactId>resilience4j-core</artifactId>
</dependency>
<dependency>
  <groupId>io.github.resilience4j</groupId>
  <artifactId>resilience4j-retry</artifactId>
</dependency>

The essentials of a webhook

Usually, the main purpose of a webhook is to communicate to an external API that an event has just occurred on the origin. The API then executes a further action accordingly.

At least the following must be configured for a webhook:

  • A webhook REST client, which will communicate with the external API.

  • A webhook event.

Internal or external REST client

In this guide, we configure a new REST client directly in the webhook configuration file but it is also possible to reference an already-existing REST client configuration through the restClientName and restCallName properties.

Location of a webhook configuration

All webhook definition Yaml files should be placed in the webhooks directory of your light module.

An API endpoint

For testing purposes, you may try several publicly available webhook APIs, for example:

As many of these public APIs are provided free-of-charge for testing, some limits may apply as to how many requests you can send per day, for example. Always get familiar with the sites' usage policies before testing.

In the example configuration below, we use the last one: https://httpdump.io. When you access this API page for the first time and press the red Create new Endpoint button there, the site will create a new short URL for you, in the form https://httpdump.io/<id-string>, for example https://httpdump.io/yxlbo.

The address generated will be the value of the url property in the REST client definition associated with your webhook configuration. Let’s see this in a concrete example configuration in the next section.

Example configuration

/example-light-module/webhooks/webhookConfig_1.yaml
name: webhook1
url: https://httpdump.io/yxlbo
method: get
enabled: true

events:
  - name: goLive
    eventType: Published
  - name: outdated
    eventType: Unpublished
    filter: "@path LIKE '%out-of-date%'"

The first half of this configuration defines the webhook name (webhook1) and the REST client properties.

The second half specifies the webhook events. Two webhook events are configured there:

  • goLive - to call the API whenever you publish a page.

  • outdated - to call the API whenever you unpublish a page, with a filter restriction applying though.

When you add this configuration to your light module, Magnolia will create the associated REST client automatically. You can check the full client configuration in the Definitions app.

Registered webhook REST client configuration

The webhook configuration can also be reviewed in the app:

Registered webhook configuration

Triggering the webhooks

Publishing a page

The goLive event will be executed for any page you publish on the author instance. If you create and publish a page called 777, the API’s web interface will display something like the following:

Publishing of a webhook event as seen on the API page

Unpublishing a page

The outdated event is triggered when you unpublish a page. However, due to a filter definition configured for this event, only the pages that contain out-of-date in the title will be considered.

Hence, if you now rename the published 777 page to for example 777-out-of-date and unpublish it on the author instance, the webhook gets triggered and the API notified:

Unpublishing of a webhook event as seen on the API page

Webhook event and REST client configuration properties

The webhook properties allow you to configure even more complex webhook events than the ones showcased above.

See also the possibilities of Magnolia REST client configuration properties, including definition of security schemes, timeouts, or caching for more advanced REST API communication scenarios.

By allowing REST client configuration properties in a webhook configuration file we are not porting REST client configurations to webhook Yaml files.

For complex REST client configurations, always try to configure them in the native /restClients/ folder of your module and only reference a configuration in a webhook definition.

Feedback