Skip to content

Commit cd7e96d

Browse files
committed
readme fix
1 parent 99c9e62 commit cd7e96d

File tree

3 files changed

+51
-79
lines changed

3 files changed

+51
-79
lines changed

README.md

Lines changed: 48 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,99 +1129,70 @@ When you exceed the rate limits for an endpoint, you will receive a `429` status
11291129

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

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.
1132+
Verifying webhook signature is easy with imagekit SDK. All you need is the value of the `x-ik-signature` header, request body, and [webhook secret](https://imagekit.io/dashboard/developer/webhooks) from the ImageKit dashboard.
11331133

1134-
```js
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-
}
1153-
```
1154-
1155-
Here is an example for implementing with express.js server.
1134+
Here is an example using the express.js server.
11561135

11571136
```js
11581137
const express = require('express');
11591138
const Imagekit = require('imagekit');
11601139

11611140
// Webhook configs
1162-
const WEBHOOK_ENDPOINT = '/webhook';
11631141
const WEBHOOK_SECRET = 'whsec_...'; // Copy from Imagekit dashboard
1164-
const WEBHOOK_EXPIRY_DURATION = 60 * 1000; // 60 seconds
1165-
1166-
// Server configs
1167-
const PORT = 8081;
1142+
const WEBHOOK_EXPIRY_DURATION = 300 * 1000; // 300 seconds for example
11681143

11691144
const imagekit = new Imagekit({
1170-
publicKey: 'pub_...',
1171-
urlEndpoint: 'https://ik.imagekit.io/example',
1172-
privateKey: 'pvt_...',
1145+
publicKey: 'public_...',
1146+
urlEndpoint: 'https://ik.imagekit.io/imagekit_id',
1147+
privateKey: 'private_...',
11731148
})
11741149

11751150
const app = express();
11761151

11771152
app.post('/webhook', express.raw({ type: 'application/json' }), (req, res) => {
1178-
// Get `x-ik-signature` from webhook request header & rawRequestBody
1179-
const signature = req.headers["x-ik-signature"]; // eg. 't=1655788406333,v1=d30758f47fcb31e1fa0109d3b3e2a6c623e699aaf1461cba6bd462ef58ea4b31'
1180-
const rawBody = req.rawBody;// Unmodified request body encoded as Uint8Array or UTF8 string
1181-
1182-
// Verify signature & parse event
1183-
let webhookResult;
1184-
try {
1185-
webhookResult = imagekit.verifyWebhookEvent(rawBody, signature, WEBHOOK_SECRET);
1186-
// `verifyWebhookEvent` method will throw an error if signature is invalid
1187-
} catch (e) {
1188-
// Failed to verify webhook
1189-
return res.status(401).send(`Webhook error: ${e.message}`);
1190-
}
1191-
const { timestamp, event } = webhookResult;
1192-
1193-
// Check if webhook has expired
1194-
if (timestamp + WEBHOOK_EXPIRY_DURATION < Date.now()) {
1195-
return res.status(401).send('Webhook signature expired');
1196-
}
1197-
1198-
// Handle webhook
1199-
switch (event.type) {
1200-
case 'video.transformation.accepted':
1201-
// It is triggered when a new video transformation request is accepted for processing. You can use this for debugging purposes.
1202-
break;
1203-
case 'video.transformation.ready':
1204-
// 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.
1205-
break;
1206-
case 'video.transformation.error':
1207-
// 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].
1208-
break;
1209-
// ... handle other event types
1210-
default:
1211-
console.log(`Unhandled event type ${event.type}`);
1212-
}
1213-
1214-
// Acknowledge webhook is received and processed successfully
1215-
res.status(200).end();
1216-
});
1153+
const signature = req.headers["x-ik-signature"];
1154+
const requestBody = req.body;
1155+
let webhookResult;
1156+
try {
1157+
webhookResult = imagekit.verifyWebhookEvent(requestBody, signature, WEBHOOK_SECRET);
1158+
} catch (e) {
1159+
// `verifyWebhookEvent` method will throw an error if signature is invalid
1160+
console.log(e);
1161+
// Return a response to acknowledge receipt of the event so that ImageKit doesn't retry sending this webhook.
1162+
res.send()
1163+
}
12171164

1218-
app.listen(PORT, () => {
1219-
console.log(`Server listening on port ${PORT}`);
1220-
console.log(
1221-
`Webhook endpoint: 'http://localhost:${PORT}${WEBHOOK_ENDPOINT}'`,
1222-
'Do replace 'localhost' with public endpoint'
1223-
);
1224-
});
1165+
const { timestamp, event } = webhookResult;
1166+
1167+
// Check if webhook has expired
1168+
if (timestamp + WEBHOOK_EXPIRY_DURATION < Date.now()) {
1169+
// Return a response to acknowledge receipt of the event so that ImageKit doesn't retry sending this webhook.
1170+
res.send()
1171+
}
1172+
1173+
// Handle webhook
1174+
switch (event.type) {
1175+
case 'video.transformation.accepted':
1176+
// It is triggered when a new video transformation request is accepted for processing. You can use this for debugging purposes.
1177+
break;
1178+
case 'video.transformation.ready':
1179+
// 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.
1180+
break;
1181+
case 'video.transformation.error':
1182+
// 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].
1183+
break;
1184+
default:
1185+
// ... handle other event types
1186+
console.log(`Unhandled event type ${event.type}`);
1187+
}
1188+
1189+
// Return a response to acknowledge receipt of the event
1190+
res.send();
1191+
})
1192+
1193+
app.listen(3000, () => {
1194+
console.log(`Example app listening on port 3000`)
1195+
})
12251196
```
12261197

12271198
## Support

index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,7 @@ class ImageKit {
672672
* @param payload - Raw webhook request body (Encoded as UTF8 string or Buffer)
673673
* @param signature - Webhook signature as UTF8 encoded strings (Stored in `x-ik-signature` header of the request)
674674
* @param secret - Webhook secret as UTF8 encoded string [Copy from ImageKit dashboard](https://imagekit.io/dashboard/developer/webhooks)
675-
* @returns \{ `timstamp`: Verified UNIX epoch timestamp if signature, `event`: Parsed webhook event payload \}
675+
* @returns \{ `timestamp`: Verified UNIX epoch timestamp if signature, `event`: Parsed webhook event payload \}
676676
*/
677677
verifyWebhookEvent = verifyWebhookEvent;
678678
}

libs/interfaces/webhookEvent.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,5 @@ export interface WebhookEventVideoTransformationError extends WebhookEventVideoT
7777
export type WebhookEvent =
7878
| WebhookEventVideoTransformationAccepted
7979
| WebhookEventVideoTransformationReady
80-
| WebhookEventVideoTransformationError;
80+
| WebhookEventVideoTransformationError
81+
| Object;

0 commit comments

Comments
 (0)