Skip to content

Commit b28d7b3

Browse files
committed
feat: add webhook verification section to README with example code
1 parent 4bad591 commit b28d7b3

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

README.md

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ const permanentSignedUrl = client.helper.buildSrc({
272272
// Result: URL with signature parameter (?ik-s=signature)
273273
```
274274

275-
### Authentication parameters for client-side uploads
275+
## Authentication parameters for client-side uploads
276276

277277
Generate authentication parameters for secure client-side file uploads:
278278

@@ -290,6 +290,41 @@ console.log(customAuthParams);
290290

291291
These authentication parameters can be used in client-side upload forms to securely upload files without exposing your private API key.
292292

293+
## Webhook verification
294+
295+
The ImageKit SDK provides utilities to verify webhook signatures for secure event handling. This ensures that webhook requests are actually coming from ImageKit and haven't been tampered with.
296+
297+
### Verifying webhook signatures
298+
299+
```ts
300+
import ImageKit from '@imagekit/nodejs';
301+
302+
const client = new ImageKit({
303+
privateKey: process.env['IMAGEKIT_PRIVATE_KEY'],
304+
webhookSecret: process.env['IMAGEKIT_WEBHOOK_SECRET'], // Required for webhook verification
305+
});
306+
307+
try {
308+
// Verify and unwrap webhook payload
309+
const event = client.webhooks.unwrap(
310+
webhookBody, // Raw webhook payload (string)
311+
{
312+
headers: webhookHeaders, // Request headers containing signature
313+
}
314+
);
315+
316+
console.log('Webhook signature is valid');
317+
console.log('Event type:', event.type);
318+
console.log('Event data:', event.data);
319+
// Process the webhook event
320+
} catch (error) {
321+
console.log('Invalid webhook signature or malformed payload');
322+
// Reject the request
323+
}
324+
```
325+
326+
For detailed information about webhook setup, signature verification, and handling different webhook events, refer to the [ImageKit webhook documentation](https://imagekit.io/docs/webhooks#verify-webhook-signature).
327+
293328
## Handling errors
294329

295330
When the library is unable to connect to the API,

0 commit comments

Comments
 (0)