ClipSDK Webhooks in Python
Verify Clipsdk-Signature HMAC-SHA256 headers in Python 3 using raw request bytes, hmac.compare_digest constant-time checks, 300-second replay windows, and idempotent delivery handling.
Webhook Headers & Delivery Contract
ClipSDK POSTs job.succeeded and job.failed JSON events to your HTTPS webhook_url with Clipsdk-Signature, Clipsdk-Job-Id, Clipsdk-Attempt, and Idempotency-Key headers.
| Header | Format / Example | Purpose |
|---|---|---|
Clipsdk-Signature | t=1796566019,v1=8a3f... | Unix timestamp plus hex HMAC-SHA256 of "<t>.<rawBody>" keyed with your webhook secret |
Clipsdk-Job-Id | job_3k9x0c2m1q8w7e6r5t4y | Identifier of the finished clipping job |
Clipsdk-Attempt | 1 to 5 | 1-based delivery attempt counter |
Idempotency-Key | <job_id>:<status> | Stable deduplication key identical across retries for the same job outcome |
User-Agent | clipsdk-webhooks/1 | Fixed ClipSDK webhook sender identifier |
Your webhook_url must use https:// on the default port (443) with no embedded user:pass@ credentials. Any 2xx HTTP response marks the event delivered. HTTP 3xx redirects are treated as failures and trigger a retry.
Webhook Event Payload Schema
Every webhook request body matches { event, job }, delivering the complete finished Job object so your consumer can ingest clips without an extra GET call.
{
"event": "job.succeeded",
"job": {
"id": "job_3k9x0c2m1q8w7e6r5t4y",
"op": "clips",
"status": "succeeded",
"stage": null,
"created_at": "2026-12-06T14:01:02Z",
"finished_at": "2026-12-06T14:04:19Z",
"usage": { "source_minutes": 38.4, "clips": 5, "output_seconds": 196.2 },
"pricing": { "basis": "source_minutes", "reason": null, "quality_multiplier": 1 },
"result": {
"source": { "duration_s": 2304, "language": "en", "speakers": null },
"clips": [
{
"rank": 1,
"score": 91,
"title": "Why 90-second intros kill viewer retention",
"video": "https://api.clipsdk.com/f/clip_rank1.mp4",
"poster": "https://api.clipsdk.com/f/clip_rank1.jpg",
"srt": "https://api.clipsdk.com/f/clip_rank1.srt",
"vtt": "https://api.clipsdk.com/f/clip_rank1.vtt"
}
],
"locked": null
},
"error": null,
"poll_url": "https://api.clipsdk.com/v1/jobs/job_3k9x0c2m1q8w7e6r5t4y"
}
}When a job fails, event is "job.failed", job.status is "failed", job.result is null, job.error contains { code, message }, and billing is $0. See all 12 job error codes in Pricing & Limits.
Verify Clipsdk-Signature in Python
Compute HMAC-SHA256 over the exact string "<t>.<rawBody>" using your webhook secret, verify with constant-time equality, and reject timestamps older than 300 seconds.
Never parse and re-stringify JSON before verifying Clipsdk-Signature. Whitespace or key-order changes invalidate the HMAC digest.
import hashlib
import hmac
import json
import os
import time
from http.server import BaseHTTPRequestHandler, HTTPServer
WEBHOOK_SECRET = os.environ["CLIPSDK_WEBHOOK_SECRET"]
SEEN_KEYS: set[str] = set()
def verify_clipsdk_signature(
raw_body: bytes, header_value: str, secret: str, tolerance_sec: int = 300
) -> bool:
if not header_value:
return False
parts = {}
for item in header_value.split(","):
if "=" in item:
k, v = item.strip().split("=", 1)
parts[k] = v
if "t" not in parts or "v1" not in parts:
return False
try:
timestamp = int(parts["t"])
except ValueError:
return False
if abs(int(time.time()) - timestamp) > tolerance_sec:
return False
signed_payload = f"{parts['t']}.".encode("utf-8") + raw_body
expected_hex = hmac.new(
secret.encode("utf-8"), signed_payload, hashlib.sha256
).hexdigest()
return hmac.compare_digest(parts["v1"], expected_hex)
class WebhookHandler(BaseHTTPRequestHandler):
def do_POST(self):
if self.path != "/webhooks/clipsdk":
self.send_response(404)
self.end_headers()
return
length = int(self.headers.get("Content-Length", "0"))
raw_body = self.rfile.read(length)
sig = self.headers.get("Clipsdk-Signature", "")
if not verify_clipsdk_signature(raw_body, sig, WEBHOOK_SECRET):
self.send_response(401)
self.end_headers()
return
idem_key = self.headers.get("Idempotency-Key", "")
if idem_key and idem_key in SEEN_KEYS:
self.send_response(200)
self.end_headers()
return
if idem_key:
SEEN_KEYS.add(idem_key)
payload = json.loads(raw_body.decode("utf-8"))
# Persist or enqueue background asset copy, then return 2xx within 15s
print(payload["event"], payload["job"]["id"])
self.send_response(200)
self.end_headers()
if __name__ == "__main__":
HTTPServer(("0.0.0.0", 8787), WebhookHandler).serve_forever()Retry Schedule & Timeout Rules
ClipSDK retries failed or timed-out webhook deliveries up to 5 total attempts (0s, +10s, +1m, +5m, +30m) using a 15-second timeout and identical request body bytes.
| Attempt | Delay After Previous Failure | Timeout | Success Criterion |
|---|---|---|---|
| Attempt 1 | Immediate (0s) | 15 seconds | Any 2xx status code (no redirects) |
| Attempt 2 | +10 seconds | 15 seconds | Same payload body and Idempotency-Key |
| Attempt 3 | +1 minute | 15 seconds | Same payload body and Idempotency-Key |
| Attempt 4 | +5 minutes | 15 seconds | Same payload body and Idempotency-Key |
| Attempt 5 | +30 minutes | 15 seconds | Final attempt; poll GET /v1/jobs/{id} to reconcile |
Production Consumer Checklist
Return HTTP 200 inside 15 seconds, deduplicate on Idempotency-Key in durable storage, and mirror 7-day /f/{key} files into your own bucket asynchronously.
- Decouple Heavy Downloads: Never download multi-megabyte MP4 files synchronously inside the webhook HTTP handler. Store the verified event in your database or queue, reply
200, and copyclip.video,clip.poster,clip.srt, andclip.vttfrom a background worker. - Persist
Idempotency-Key: Store<job_id>:<status>with a unique constraint in Postgres or Redis so network timeouts after commit never duplicate user notifications or social posts. - Reconcile Missed Deliveries: Run a lightweight periodic sweeper calling
GET /v1/jobs/{id}on any job still marked running after 30 minutes to recover cleanly from extended endpoint outages. - Zero-Dependency Reference Scripts: Download the runnable verifier and
--self-testharness from bitgate/clipsdk-api-skills.