Webhooks
If you are using Legacy Webhook we recommend you update it to the latest to take advantage of all the latest event triggers.
What are Webhooks?
A webhook is a way for one application to send other applications immediate information. If you would like to react to events within Storyblok in another service, you need a way to inform these services. Storyblok offers webhooks to call to the outside world. This mechanism is very useful when you want to clear a cache or start a build process, for example whenever new content is published or updated.
In Storyblok creating Webhook is pretty straightforward. You can follow the steps in the above image to create your first Webook.
In order to create a Webhook you have to fill 3 required fields.
Once added, a POST request will be sent with a JSON payload to the specified URL. It expects to receive a 2xx
status code and Content-Type: application/json
as a response.
Webhooks will not be retried if the service fails to process the event. Publish and/or save are single events and retries could have unwanted side effects.
If you plan to execute a long-running task, it is recommended that a response is sent immediately (e.g., 202 - Accepted
) instead of waiting until the task is finished. The webhook will time out and trigger an error message after 120 seconds.
Story
- Story published
- Story unpublished
- Story deleted
- Story moved
published
: triggered when a story is published
unpublished
: triggered, when a story is unpublished
deleted
: triggered, when a story is deleted (dropdown in the content overview)
moved
: triggered when a story is moved from a folder or to a folder
Pipeline
To use Pipeline webhooks the Pipelines App needs to be installed first.
- Pipeline deployed
deployed
: triggered, when a pipeline stage is deployed
branch_deployed
: triggered, when a pipeline stage is deployed(legacy webhook)
Releases
To use Release webhooks, the Releases App needs to be installed first.
- Release merged
merged
: triggered when a release is merged into the currently released content
release_merged
: triggered, when a release is merged into the currently released content(legacy webhook)
Tasks
To use Task webhooks, the Tasks App needs to be installed.
The Task App is used to create automation tasks which then execute a webhook. Tasks are triggered from the user interface by clicking the “Execute” button. To configure a webhook using the Task-Manager App, you need to create a new task {1} .
task_execution
: trigger a request from Storyblok
task_execution
with user dialog: the payload contains additional data in dialog_values. When setting up a task, it is possible to add a dialog. The provided user input will be sent to the endpoint as dialog_values.
Securing a Webhook
In order to set the webhook secret you require an Entry plan or above.
To protect your services, it is always a good idea to make sure that only requests from expected sources are processed. To protect your webhook endpoints, it is necessary to verify if the request is really coming from Storyblok. It is possible to verify the sender of the webhook by verifying the signature that is sent along with the payload and generated with a shared secret key (webhook secret).
We recommend using a random string with at least 20 characters (e.g. output of this command openssl rand -hex 20
). Once the secret is inserted, press the Save button or hit enter to permanently store the data.
Storyblok will add a signature to every webhook request once the webhook secret {1} is configured. The signature is sent in the webhook-signature
header of the request. If the webhook secret is not set, the header remains empty.
In your webhook endpoint, you can now check if the webhook-signature
was generated with the webhook secret. To use this example, you need to define the environment variable STORYBLOK_WEBHOOK_SECRET
with the value used as webhook secret.
Most JavaScript frameworks parse incoming data into JSON
automatically, Storyblok's payload is meant to be used as raw text without parsing.
To ensure seamless integration, handle the data without JSON
parsing. Check your framework's documentation or explore methods to directly use the raw text data provided by Storyblok.
Processing Webhooks
There are multiple ways to use the Storyblok webhooks. For instance, they can be sent to specific services to trigger build scripts (e.g. Netlify, Vercel), they can send a notification (e.g. Slack, Discord), or you can even use your own handler to process a webhook.
Depending on the tasks you would like to execute, there are different options for handling an incoming webhook. In this case, it heavily depends on what you would like to trigger with the webhook. There are a couple of ways to execute your logic:
- By hosting a full-blown server. This depends heavily on what technology you are using.
- By using serverless functions. Since webhooks logic usually triggers a concise program, it's a perfect case for serverless functions. Some of the players are AWS Lambda, Google Cloud, Azure Functions, Webtask.io, Vercel, and Netlify Functions.
- By using low- or no-code environments. Low- and no-code environments already provide predefined building blocks that can be used to create your own flows. Example providers are pipedream, n8n, IFTTT, and Zapier.
Webhooks can, for example, be used to trigger build processes on Netlify or Vercel. Start by reading about how Netlify - Build Hooks or Vercel - Deploy Hooks work.
Distribute Events
Sometimes it is not enough to trigger only one endpoint. To send a single event to multiple environments, a service that distributes the event is required. An example of how such a service could look can be found in this GitHub project: https://github.com/DominikAngerer/webhook-router.
Debugging Webhooks
One thing that makes development much easier, is a sensible way of finding out what is happening when something doesn’t work as expected. To allow you to get this information quickly, we can offer two strategies to debug webhooks:
Webhook logs
Logs show you which requests have been sent with which payload.
To see the JSON payload you can click on a log item.
Debug Endpoint
Sometimes it is unclear if the correct payload arrives at the endpoint or if the event is triggered as expected. In this case, you can use webhook.site or RequestBin to test the request information you get from webhooks. The tutorial “How to use RequestBin.com to work with Webhooks” covers this topic.