Documentation Index
Fetch the complete documentation index at: https://docs.posthook.io/llms.txt
Use this file to discover all available pages before exploring further.
If you use a third-party API that processes data (like video encoding or AI generation) but doesn’t offer webhooks, you usually have to run a cron job to poll for status.
With Posthook, you can create a “self-healing” loop that checks the status and reschedules itself if the job isn’t done yet.
Workflow
- Schedule: Trigger the job and schedule the first check.
- Handle: Check status. If
pending, reschedule. If completed, process the result.
import Posthook from '@posthook/node';
import { Signatures, SignatureVerificationError } from '@posthook/node';
const posthook = new Posthook(process.env.POSTHOOK_API_KEY);
const signatures = new Signatures(process.env.POSTHOOK_SIGNING_KEY);
/* 1. Schedule */
async function startVideoEncoding(videoFile) {
// 1. Start the job on the 3rd party API
const job = await transcoderApi.start(videoFile);
// 2. Schedule the first check for 1 minute later
await posthook.hooks.schedule({
path: '/webhooks/check-status',
postIn: '1m',
data: {
jobId: job.id
}
});
}
/* 2. Handle */
app.post('/webhooks/check-status', async (req, res) => {
try {
const delivery = signatures.parseDelivery(req.body, req.headers);
const { jobId } = delivery.data;
const job = await transcoderApi.get(jobId);
if (job.status === 'completed') {
// DONE: Process the result
await db.saveVideo(job.url);
return res.status(200).send('Video processed');
}
if (job.status === 'failed') {
// FAIL: Handle error (maybe alert team)
return res.status(200).send('Job failed');
}
// PENDING: Reschedule check for another minute
await posthook.hooks.schedule({
path: '/webhooks/check-status',
postIn: '1m',
data: { jobId }
});
res.status(200).send('Still pending, rescheduled');
} catch (err) {
if (err instanceof SignatureVerificationError) {
return res.status(401).json({ error: err.message });
}
throw err;
}
});