Skip to content

Commit 6612f2d

Browse files
authored
Merge pull request #53 from 21TORR/storyblok
New Storyblok webhook docs
2 parents 1541840 + 953a0f4 commit 6612f2d

5 files changed

Lines changed: 154 additions & 3 deletions

File tree

babel.config.js

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
title: Storyblok Bundle Webhooks
3+
authors: [jannik]
4+
tags: [storyblok, symfony]
5+
---
6+
7+
The installation guide for the [Storyblok Bundle] was updated. The new bundle version also includes the [new webhook integration].
8+
9+
10+
[Storyblok Bundle]: /docs/php/symfony/storyblok
11+
[new webhook integration]: /docs/php/symfony/storyblok/webhook

docs/php/symfony/storyblok/index.mdx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ If you are using Symfony Flex you are all set.
4040
4141
# your content token – with `preview` scope
4242
STORYBLOK_CONTENT_TOKEN=
43+
44+
# the secret to validate webhooks
45+
STORYBLOK_WEBHOOK_SECRET=
4346
```
4447

4548
Then add the base config:
@@ -49,5 +52,15 @@ If you are using Symfony Flex you are all set.
4952
space_id: "%env(int:STORYBLOK_SPACE_ID)%"
5053
management_token: "%env(STORYBLOK_MANAGEMENT_TOKEN)%"
5154
content_token: "%env(STORYBLOK_CONTENT_TOKEN)%"
55+
webhook:
56+
secret: "%env(STORYBLOK_WEBHOOK_SECRET)%"
57+
```
58+
59+
and load the routing:
60+
61+
```yaml title="config/routes/storyblok.yaml"
62+
storyblok:
63+
prefix: /storyblok/
64+
resource: '@TorrStoryblokBundle/config/routes.yaml'
5265
```
5366
</details>
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# Webhooks
2+
3+
The Storyblok bundle automatically integrates in to all webhooks that are sent from Storyblok. It parses the requests, validates it and passes properly typed events with payloads to your code, to integrate into your app.
4+
5+
## Installation
6+
7+
After setting up the bundle routes[^1], you need to configure the webhook URL in Storyblok:
8+
9+
```txt
10+
https://.../storyblok/webhook
11+
```
12+
13+
14+
## Webhook Secrets
15+
16+
You should always use webhook secrets to secure your webhook endpoints. The webhooks are a public endpoint, so anyone can send arbitrary requests to it.
17+
18+
### For Paid Plans
19+
20+
On paid plans, you can use the native **Webhooks Secret** feature. Configure the webhook secret in Storyblok and in your bundle:
21+
22+
```yaml title="config/packages/storyblok.yaml"
23+
storyblok:
24+
webhook:
25+
secret: "%env(STORYBLOK_WEBHOOK_SECRET)%"
26+
```
27+
28+
### For Unpaid Plans
29+
30+
Free plans (that are typically used during development) don't support proper webhook secrets, but this you can add the webhook secret to the URL.
31+
32+
For that enable this feature:
33+
34+
```yaml title="config/packages/storyblok.yaml"
35+
storyblok:
36+
webhook:
37+
secret: "my-secret"
38+
allow_url_secret: true
39+
```
40+
41+
and append the secret to your webhook endpoint URL in the config:
42+
43+
```txt
44+
https://.../storyblok/webhook/my-secret
45+
```
46+
47+
:::danger
48+
Don't forget to rotate the secret when switching from an unpaid to a paid plan. You should always use proper webhook secrets if available.
49+
:::
50+
51+
52+
## Usage of Webhook Events
53+
54+
For every webhook request a `StoryblokWebhookEvent` is dispatched.
55+
56+
It provides the webhook payload and gives you the opportunity to pass arbitrary data to the webhook endpoint response.
57+
58+
### Webhook Payloads
59+
60+
The webhook payload defines the type of webhook that was dispatched. Right now the following webhook payloads are implemented:
61+
62+
| Webhook Type | Payload |
63+
|-----------------|---------------------------------|
64+
| Story | `StoryWebhookPayload` |
65+
| Datasource | `DatasourceEntryWebhookPayload` |
66+
| Asset | `AssetWebhookPayload` |
67+
| User management | `UserWebhookPayload` |
68+
| Discussion | *(not yet implemented)* |
69+
| Workflow | `WorkflowStageWebhookPayload` |
70+
| Pipeline | `PipelineWebhookPayload` |
71+
| Release | `ReleaseWebhookPayload` |
72+
73+
74+
A typical implementation registers to the event and matches the payload type:
75+
76+
```php
77+
use Torr\Storyblok\Event\StoryblokWebhookEvent;
78+
79+
public function onStoryblokWebhook (StoryblokWebhookEvent $event) : void
80+
{
81+
switch (true)
82+
{
83+
case $event->payload instanceof StoryWebhookPayload:
84+
// ...
85+
break;
86+
87+
case $event->payload instanceof AssetWebhookPayload:
88+
// ...
89+
break;
90+
}
91+
}
92+
```
93+
94+
:::caution
95+
The webhook event is dispatched synchronously in the webhook event handler. Make sure to always return a fast response, like just dispatching a message to the message bus.
96+
:::
97+
98+
99+
## Endpoint Response
100+
101+
You can pass arbitrary response data to the `StoryblokWebhookEvent` to include in the webhook endpoint response:
102+
103+
```php
104+
use Torr\Storyblok\Event\StoryblokWebhookEvent;
105+
106+
public function onStoryblokWebhook (StoryblokWebhookEvent $event) : void
107+
{
108+
$event->addResponseData("some-data", "test");
109+
}
110+
```
111+
112+
Will include
113+
114+
```json
115+
{
116+
"some-data": "test"
117+
}
118+
```
119+
120+
in the response.
121+
122+
:::tip
123+
Make sure to include useful information in the response, as you can then debug your webhooks (and their responses) in the Storyblok UI.
124+
:::
125+
126+
127+
128+
129+
[^1]: Follow the [installation guide](./#installation). You can see the details about setting up the routing if you open the "manual installation" section.

docusaurus.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ const config = {
4949
keywords: ['best-practice'],
5050
extendDefaults: true,
5151
},
52+
onUntruncatedBlogPosts: 'ignore',
5253
},
5354
theme: {
5455
customCss: require.resolve('./assets/scss/custom.scss'),

0 commit comments

Comments
 (0)