- Handle video webhooks by verifying the signature on the original request bytes, matching the event to an owned job, and storing the receipt durably before acknowledging it.
- Treat completion and failure as different outcomes. Deduplicate a replay at storage time so two workers cannot deliver the same version twice.
- Test a changed body, a worker interruption and an unrelated job before putting the receiver into production.
A create request returns HTTP 202. Your application knows the job exists, but it still cannot tell a customer that their file is ready. Let the job finish asynchronously. Give the receiver a durable record of the original request, the expected account and the destination.
Choose how the event reaches you
For one job, include webhook_url and webhook_secret in the create request. For all matching jobs on one ngram account, POST to /api/v1/webhooks/subscriptions with event_type and target_url. Register separate subscriptions for video.completed and video.failed. Save each subscription’s secret at creation; it is not returned on a later read. Use a public HTTPS receiver for production.
POST /api/v1/webhooks/subscriptions
Authorization: Bearer <server-side API key>
Content-Type: application/json
{
"event_type": "video.completed",
"target_url": "https://events.example.com/ngram/completed"
}
Create the failure subscription with event_type set to video.failed. A separate receiver path for each subscription makes it straightforward to select the matching signing secret before reading the body.
Start from the API getting-started guide to authenticate and check account access. Key creation is self-service. Creating a video with an API key requires Basic or a higher paid plan and sufficient credits.
Work through a duplicate before putting the receiver in production
Take a sample delivery for a video that has already finished. On the first valid receipt, persist the job ID, event type, version when supplied, and the action you intend to perform. Commit the receipt and the work item together if your storage permits it. Return the successful response once the work is durable. An in-memory flag will disappear when the receiver restarts.
Now replay the identical delivery while the first worker is still copying the file. Both HTTP requests may succeed. Only one durable work item should exist for that completion. Enforce that property at storage time with a unique key and a conditional insert. Two workers can both pass a separate “does this exist?” check before either one writes anything. Use an event identifier when the delivery contract provides one. Otherwise choose a key that distinguishes the event type, video and intended version so a real later revision can still be processed.
Test three separate failures. Alter one byte of a signed body and expect signature verification to reject it. Interrupt processing after receipt and confirm the queued work resumes. Send a valid failed event for a different video and confirm it cannot replace the earlier video's file. Each exercise tests a different boundary, so recording only the HTTP status leaves most of the workflow untested.
Which event gets to change the customer record?
| Delivery received | Receiver action | Customer-visible consequence |
|---|---|---|
| First valid completion for the expected job and version | Persist one work item and verify delivery | The file becomes ready only after the delivery step succeeds. |
| Same completion delivered again | Return the existing processing outcome | No second file or message. |
| Valid failure for a different job | Look up that job and record its failure | The earlier completed file remains available. |
| Body changed after signing | Reject it before accepting work | No destination changes. |
Verify the signature before parsing the JSON
Keep the exact bytes delivered to the endpoint. Read X-Ngram-Timestamp and X-Ngram-Signature. Using the secret assigned to this callback or subscription, compute HMAC-SHA256 over the timestamp, a period and the raw bytes. Reject invalid signatures and timestamps more than five minutes from the server clock. The helper below assumes a Node request handler has already captured the raw body as a Buffer.
import { createHmac, timingSafeEqual } from "node:crypto";
function validWebhook(rawBody, headers, secret) {
const timestamp = headers["x-ngram-timestamp"];
const signature = headers["x-ngram-signature"];
if (typeof timestamp !== "string" || !/^\d+$/.test(timestamp)) return false;
if (typeof signature !== "string" || !/^sha256=[0-9a-f]{64}$/.test(signature)) return false;
if (Math.abs(Date.now() / 1000 - Number(timestamp)) > 300) return false;
const expected = createHmac("sha256", secret)
.update(timestamp + ".").update(rawBody).digest();
const received = Buffer.from(signature.slice(7), "hex");
return timingSafeEqual(expected, received);
}
Only after verification should the handler parse event, video_id and any version_id. Resolve the destination from your own job-to-record mapping, not from a free-form URL supplied by a customer. The webhook integration describes callback and subscription scopes.
Route a completed result to the correct record
In the completed example, video_id is vid_release_brief and the receiver gets a version_id, a result URL and duration_ms of 13200. An endpoint returning 204 has acknowledged receipt. Record delivery to the customer only after your application has checked the intended destination.
A preview-only job can complete without result.url. Its job-specific callback receives the event; account-wide subscriptions do not receive that preview completion. Read live_preview on GET /api/v1/videos/{id}, or export the approved version if the recipient needs an MP4.
Send failure to the owner, not the customer’s file slot
The failure example has video_id vid_source_review, error_code generation_failed and the message “Could not finish the video.” Keep the failure and its job ID with the source record. Notify the operator who can inspect the cause. Do not create a file placeholder that looks like a successful video.
Acknowledge durable receipt and make retries safe
Persist the event or enqueue it durably, with a unique processing key, before answering 2xx. Keep that acknowledgement within ten seconds. A timeout or non-2xx response can lead to retries after 1, 2, 5, 15 and 30 minutes, for up to six attempts in total. HTTP 410 tells a persistent subscription that the receiver is gone and removes the subscription.
For a versioned completion, key the delivery on video_id and version_id. The same pair should not notify twice. A new edit can complete under the same video_id with a new version_id; process it as a new delivery when appropriate. Use a separate failure key and read current status when an older completion has no version_id. The public API integration covers the job and export reads you can use to reconcile a result.
Decide between polling and an account subscription
For one small integration that owns one job at a time, looking up the saved video ID may be easier to operate. Set a bounded interval, handle processing and failed states explicitly, and record the existing job before retrying after a timeout. Starting a fresh render each time a status lookup fails turns a temporary network problem into duplicate work.
An account subscription makes more sense when several services need outcomes from many jobs. It also means the receiver can see events for videos created outside its own workflow. Match the event to an owned job before doing destination work. Keep a way to reconcile a known unfinished job through the status API when the receiver was unavailable for part of an incident.
Document who can rotate the signing secret, where the current subscription lives and which worker drains accepted events. Those details belong in the integration runbook. They let the next engineer recover delivery without changing a customer's access or publishing another copy.
Give support a receipt it can actually investigate
For each job retain the relevant identifier, last accepted event, processing outcome and destination update. Store a concise failure reason with the failed job. Keep shared secrets and full customer payloads out of routine logs. When a customer reports a missing file, the operator should be able to distinguish “nothing arrived,” “receipt saved, worker pending,” and “file copied, notification failed.”
A receipt whose signature is valid still has to match the account and job expected by your application. A job title or destination taken from untrusted input should never decide which customer's record gets updated. If the match is missing, retain enough information for investigation and leave the customer portal unchanged.
You just read it. Now watch it.
ngram turns this post into a short explainer video: scenes, voiceover, and motion graphics included.






