Webhooks

Overview

Set up webhooks for real-time notifications on jobs and uploads. Learn signature verification, event handling, and local testing.

Webhooks and notifications are a powerful tool that enables event-driven behavior. Rather than looping to retrieve an object state your webhook can listen to notifications and trigger a workflow accordingly.

For example, instead of periodically checking the status of your jobs to know if they are completed, you can set up a webhook to receive notifications for the job.completed event and do your own process from there.

import Chunkify from '@chunkify/chunkify';

const client = new Chunkify({
    projectAccessToken: 'My Project Access Token',
});

// Create a webhook
const webhook = await client.webhooks.create({
    url: 'https://example.com/webhook',
    events: ['job.completed'],
})

If you don't specify the events field when creating a webhook, it will receive all notifications for the project.

Events

Chunkify notifications system supports various events types. Depending on the event type, the notification will contain different information.

Events List

Events name Description
job.completed The transcoding job has been successfully completed
job.failed The transcoding job has encountered an error
job.cancelled The transcoding job has been cancelled
upload.completed The upload of a video has been successfully completed
upload.failed The upload of a video has encountered an error
upload.expired The upload window has expired

Configuring Webhooks for Specific Events

By default, when creating a webhook all events are enabled. If you want your webhook to only receive specific events, you can pass the events parameter to the webhook creation. Here an example for a webhook that will receive notifications when a job is completed or failed:

import Chunkify from '@chunkify/chunkify';

const client = new Chunkify({
    projectAccessToken: 'My Project Access Token',
});

// Create a webhook
const webhook = await client.webhooks.create({
    url: 'https://example.com/webhook',
    events: ['job.completed','job.failed'],
});

Webhook Signature and Notification Parsing

For security purposes, Chunkify follows the Standard Webhooks specification to sign webhooks. You will need to retrieve the following headers: webhook-id, webhook-timestamp, and webhook-signature, and use your secret key to verify that the notifications are coming from Chunkify.

You can find this key in the Chunkify App in your project settings under the webhooks section.

Here is a simple example of how your webhook server can handle the notifications:

// This example uses Express for simplicity
import express from 'express';
import type { RequestHandler } from 'express';
import Chunkify from '@chunkify/chunkify';


const app = express();
const PORT = 8787;

// Use raw body for signature verification
app.use(express.raw({ type: 'application/json' }));

const webhookHandler: RequestHandler = (req, res) => {
const headers: Record<string, string> = {
    'webhook-signature': req.header('webhook-signature') || '',
    'webhook-id': req.header('webhook-id') || '',
    'webhook-timestamp': req.header('webhook-timestamp') || '',
};

const client = new Chunkify({
    projectAccessToken: 'My Project Access Token',
    webhookKey: 'your secret key',
});

// Unwrap the webhook will also verify the signature using the secret key
let notification: Chunkify.UnwrapWebhookEvent;
try {
   notification = client.webhooks.unwrap(req.body.toString(), { headers });
} catch (error) {
    console.error('Error unwrapping webhook:', error);
    res.status(400).send('Error unwrapping webhook');
    return;
}

const event = notification.event; // e.g., "upload.completed", "job.completed", "job.failed"
console.log('Webhook event received:', event);
switch (event) {
    case 'job.completed':
        const payload = notification.data as Chunkify.UnwrapWebhookEvent.NotificationPayloadJobCompleted;
        console.log(`Job ${payload.job.id} completed`);
        break;
        // Complete with other cases for other event type as needed.
    default:
        console.log(`Unexpected event type: ${event}`);
}
res.status(200).send('OK');
};
    
app.post('/webhook', webhookHandler);
    
app.listen(PORT, () => {
console.log(`Test server listening on http://localhost:${PORT}`);
});

Testing your webhook integration locally

To test your webhook integration safely the Chunkify CLI provides a way to proxy the notifications to your local server. This way you can test your webhook behavior locally before deploying it to production. You can see how to do that in the CLI Guide.