Skip to content

Commit 9dab91e

Browse files
committed
README - imagekit.verifyWebhookEvent usage code fixed
1 parent dd4da9d commit 9dab91e

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

README.md

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,16 +1132,24 @@ ImageKit sends `x-ik-signature` in the webhook request header, which can be used
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

11341134
```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"...} }
1135+
try {
1136+
const {
1137+
timestamp, // Unix timestamp in milliseconds
1138+
event, // Parsed webhook event object
1139+
} = imagekit.verifyWebhookEvent(
1140+
'{"type":"video.transformation.accepted","id":"58e6d24d-6098-4319-be8d-40c3cb0a402d"...', // Raw request body encoded as Uint8Array or UTF8 string
1141+
't=1655788406333,v1=d30758f47fcb31e1fa0109d3b3e2a6c623e699aaf1461cba6bd462ef58ea4b31', // Request header `x-ik-signature`
1142+
'whsec_...' // Webhook secret
1143+
)
1144+
// { timestamp: 1655788406333, event: {"type":"video.transformation.accepted","id":"58e6d24d-6098-4319-be8d-40c3cb0a402d"...} }
1145+
} catch (err) {
1146+
// `verifyWebhookEvent` will throw an error if the signature is invalid
1147+
// And the webhook event must be ignored and not processed
1148+
console.log(err);
1149+
// Under normal circumstances you may catch following errors:
1150+
// - `Error: Incorrect signature` - you may check the webhook secret & the request body being used.
1151+
// - `Error: Signature missing` or `Error: Timestamp missing` or `Error: Invalid timestamp` - you may check `x-ik-signature` header used.
1152+
}
11451153
```
11461154

11471155
Here is an example for implementing with express.js server.

0 commit comments

Comments
 (0)