Skip to content

Commit 769409e

Browse files
committed
README/## Verify webhook events patch
1 parent 17fa221 commit 769409e

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

README.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,21 +1125,34 @@ When you exceed the rate limits for an endpoint, you will receive a `429` status
11251125
| `X-RateLimit-Reset` | The amount of time in milliseconds before you can make another request to this endpoint. Pause/sleep your workflow for this duration. |
11261126
| `X-RateLimit-Interval` | The duration of interval in milliseconds for which this rate limit was exceeded. |
11271127

1128-
## Verify webhooks
1128+
## Verify webhook events
11291129

11301130
ImageKit sends `x-ik-signature` in the webhook request header, which is used to verify the authenticity of the webhook.
11311131

11321132
Verifing webhook signature is easy with imagekit SDK. All you need is `x-ik-signature`, rawRequestBody and secretKey. You can copy webhook secret from imagekit dashboard.
11331133

1134-
Here is an example of how to verify the webhook signature in an express.js server.
1134+
```js
1135+
const {
1136+
timestamp, // Unix timestamp in milliseconds
1137+
event, // Parsed webhook event object
1138+
} = imagekit.verifyWebhookEvent(
1139+
`{"type":"video.transformation.accepted","id":"58e6d24d-6098-4319-be8d-40c3cb0a402d"...`, // Raw request body encoded as Uint8Array or UTF8 string
1140+
't=1655788406333,v1=d30758f47fcb31e1fa0109d3b3e2a6c623e699aaf1461cba6bd462ef58ea4b31' // Webhook secret key
1141+
'whsec_...' // Webhook secret key
1142+
) // Throws an error if signature is invalid
1143+
1144+
// { timestamp: 1655788406333, event: {"type":"video.transformation.accepted","id":"58e6d24d-6098-4319-be8d-40c3cb0a402d"...} }
1145+
```
1146+
1147+
Here is an example of implementing with express.js server.
11351148

11361149
```js
11371150
const express = require('express');
11381151
const Imagekit = require('imagekit');
11391152

11401153
// Webhook configs
11411154
const WEBHOOK_ENDPOINT = '/webhook';
1142-
const WEBHOOK_SECRET = 'whsec_0DrqBcZEGejn4fU0P6+EQNTPAEgUnIQW'; // Copy from Imagekit dashboard
1155+
const WEBHOOK_SECRET = 'whsec_...'; // Copy from Imagekit dashboard
11431156
const WEBHOOK_EXPIRY_DURATION = 60 * 1000; // 60 seconds
11441157

11451158
// Server configs

0 commit comments

Comments
 (0)