Skip to content

Commit 17fa221

Browse files
committed
Readme/## Verify webhooks
1 parent db98b80 commit 17fa221

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

README.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,6 +1125,84 @@ 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
1129+
1130+
ImageKit sends `x-ik-signature` in the webhook request header, which is used to verify the authenticity of the webhook.
1131+
1132+
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.
1133+
1134+
Here is an example of how to verify the webhook signature in an express.js server.
1135+
1136+
```js
1137+
const express = require('express');
1138+
const Imagekit = require('imagekit');
1139+
1140+
// Webhook configs
1141+
const WEBHOOK_ENDPOINT = '/webhook';
1142+
const WEBHOOK_SECRET = 'whsec_0DrqBcZEGejn4fU0P6+EQNTPAEgUnIQW'; // Copy from Imagekit dashboard
1143+
const WEBHOOK_EXPIRY_DURATION = 60 * 1000; // 60 seconds
1144+
1145+
// Server configs
1146+
const PORT = 8081;
1147+
1148+
const imagekit = new Imagekit({
1149+
publicKey: 'pub_...',
1150+
urlEndpoint: 'https://ik.imagekit.io/example',
1151+
privateKey: 'pvt_...',
1152+
})
1153+
1154+
const app = express();
1155+
1156+
app.post('/webhook', express.raw({ type: 'application/json' }), (req, res) => {
1157+
// Get `x-ik-signature` from webhook request header & rawRequestBody
1158+
const signature = req.headers["x-ik-signature"]; // eg. 't=1655788406333,v1=d30758f47fcb31e1fa0109d3b3e2a6c623e699aaf1461cba6bd462ef58ea4b31'
1159+
const rawBody = req.rawBody;// Unmodified request body encoded as Uint8Array or UTF8 string
1160+
1161+
// Verify signature & parse event
1162+
let webhookResult;
1163+
try {
1164+
webhookResult = imagekit.verifyWebhookEvent(rawBody, signature, WEBHOOK_SECRET);
1165+
// `verifyWebhookEvent` method will throw an error if signature is invalid
1166+
} catch (e) {
1167+
// Failed to verify webhook
1168+
return res.status(401).send(`Webhook error: ${e.message}`);
1169+
}
1170+
const { timestamp, event } = webhookResult;
1171+
1172+
// Check if webhook has expired
1173+
if (timestamp + WEBHOOK_EXPIRY_DURATION < Date.now()) {
1174+
return res.status(401).send('Webhook signature expired');
1175+
}
1176+
1177+
// Handle webhook
1178+
switch (event.type) {
1179+
case 'video.transformation.accepted':
1180+
// It is triggered when a new video transformation request is accepted for processing. You can use this for debugging purposes.
1181+
break;
1182+
case 'video.transformation.ready':
1183+
// It is triggered when a video encoding is finished and the transformed resource is ready to be served. You should listen to this webhook and update any flag in your database or CMS against that particular asset so your application can start showing it to users.
1184+
break;
1185+
case 'video.transformation.error':
1186+
// It is triggered if an error occurs during encoding. Listen to this webhook to log the reason. You should check your origin and URL-endpoint settings if the reason is related to download failure. If the reason seems like an error on the ImageKit side, then raise a support ticket at [email protected].
1187+
break;
1188+
// ... handle other event types
1189+
default:
1190+
console.log(`Unhandled event type ${event.type}`);
1191+
}
1192+
1193+
// Acknowledge webhook is received and processed successfully
1194+
res.status(200).end();
1195+
});
1196+
1197+
app.listen(PORT, () => {
1198+
console.log(`Server listening on port ${PORT}`);
1199+
console.log(
1200+
`Webhook endpoint: 'http://localhost:${PORT}${WEBHOOK_ENDPOINT}'`,
1201+
'Do replace 'localhost' with public endpoint'
1202+
);
1203+
});
1204+
```
1205+
11281206
## Support
11291207

11301208
For any feedback or to report any issues or general implementation support, please reach out to [[email protected]](mailto:[email protected])

0 commit comments

Comments
 (0)