Skip to content

Idempotent webhook handling

Verify PubNio HMAC-SHA256 webhook signatures, deduplicate retries, and handle out-of-order delivery events safely.

PubNio emits signed webhooks for every delivery state transition (post.delivery.published, post.delivery.provider_draft, post.delivery.failed, post.delivery.unresolved, post.delivery.cancelled). Because HTTP webhooks are at-least-once and may arrive out of order, design your receiver to be strictly idempotent.

1. Verify the signature against raw bytes

Always read the raw request body before parsing JSON and verify it with the endpoint’s signing secret. @pubnio/sdk implements Standard Webhooks HMAC-SHA256 with a timestamp tolerance and constant-time comparison, then returns the decoded event.

import { verifyWebhookSignature } from "@pubnio/sdk";

const event = await verifyWebhookSignature({
  secret: process.env.PUBNIO_WEBHOOK_SECRET,
  body: rawBody, // the exact string or bytes received
  headers: request.headers,
});

A failed check throws WebhookVerificationError with missing_headers, invalid_secret, invalid_signature, stale_timestamp, or invalid_payload. Respond with 400 and do not process the event.

2. Deduplicate by event ID

Store each processed event.id in a database unique index inside the same transaction that updates your local post status:

  1. Verify signature and timestamp.
  2. Attempt INSERT INTO processed_webhook_events (event_id) VALUES ($1) ON CONFLICT DO NOTHING.
  3. If zero rows were inserted, return 200 OK immediately (duplicate delivery).
  4. Otherwise apply the target outcome keyed by (postId, targetId, attemptId) and return 200 OK.

3. Never regress terminal states

A delayed webhook from an earlier attempt could arrive after a retry has already succeeded. Only apply an event if attemptNumber is greater than or equal to the latest attempt recorded for that target, and prefer polling GET /api/v1/posts/{postId} as your source of truth whenever reconciling unresolved outcomes.

Type to search…

↑↓ navigate↵ selectEsc close