Terndesk, the keyboard-first email client with a CLI. 50% off for the first 100 buyers. Get it →

Webhook Retry Idempotency: Handle Duplicates Safely

Webhook Retry Idempotency: Handle Duplicates Safely

11 min read
Static Forms Team
webhooksidempotencyreliabilitydynamodbnode.js
Share:

A webhook retry means your receiver can see the same form submission more than once. To handle it safely, key every event on the Idempotency-Key header, record that key with a conditional write before doing any work, and make the final side effect an upsert instead of a blind create.

Static Forms sends Idempotency-Key: staticforms:form.submitted:<submissionId> with every webhook delivery, and it reuses the same value on each retry. Webhooks are available on Starter and up, and the pricing page lists the current plans.

This guide builds the receiver in Node.js with DynamoDB and a CRM-style destination. If you are new to the mechanics, what a webhook is and how it works covers the basics first.

What a retry looks like and how to dedupe it

A webhook can succeed and still look like a failure to the sender. Your receiver creates the lead, the connection drops before the response arrives, the sender retries, and the same submission creates another lead.

What the sender saw What your receiver did Result without dedupe Fix
Timeout before your 2xx (Static Forms waits 10 seconds per attempt) Created the lead Second lead on retry Check the key before any side effect
5xx from your receiver Maybe nothing, maybe half the work Partial or duplicate work Write the receipt first, return 5xx if it fails
2xx, then a manual redelivery Created the lead Duplicate lead Same key, so the receipt already exists
Two copies arriving together Both ran Race, two leads Conditional write lets only one win

The finished path is:

Static Forms -> authenticated receiver -> DynamoDB receipt -> DynamoDB Stream -> CRM upsert

Four-step flow: a delivery arrives, the key is checked, a conditional receipt is written, and one lead results

The guarantee you actually want

HTTP defines an operation as idempotent when repeating the same request has the same intended effect as making it once. RFC 9110 applies that property to methods such as PUT and DELETE, but webhook deliveries normally use POST. Your receiver has to add the safety itself.

Be picky with the wording:

  • A conditional database write can record one receipt when two copies arrive together.
  • DynamoDB Streams can deliver a receipt to a worker more than once.
  • A CRM upsert keyed by the submission ID can turn those worker runs into one logical lead.

That is effectively-once business behavior. The code may run twice. The user-facing result should still exist once.

Use the submission, not the timestamp, as identity

Current Static Forms deliveries contain data.submissionId. They also send an Idempotency-Key in this form:

Plain Text
staticforms:form.submitted:<submissionId>

Every retry for the same delivery carries the same value. The timestamp is not a safe substitute because two events can share a timestamp, and a retried payload may reach you much later.

Treat the header and body as a pair. If the header says one submission ID and the body says another, reject the request before writing anything. That catches broken proxies, mapping mistakes, and hand-built test requests that do not match the production contract.

An idempotency key is not authentication. Validate the sender first, then run the receipt logic. The guide to Bearer, Basic and custom header auth for form webhooks covers that step without mixing credentials into this example.

Store the receipt with a conditional write

Create a DynamoDB table whose partition key is pk and enable DynamoDB Streams with new images. The receipt item stores the key, the event, the submission ID, the few fields the worker needs, a status, and a cleanup timestamp. Keep the rest of the payload out of the table: it carries the form's API key, the recipient address, and every form field, and you do not need them to dedupe.

Pick a retention window that covers your real replay and support needs. The sample keeps receipts for 30 days. Enable TTL on the expiresAt attribute, which holds epoch seconds, or nothing removes the receipts. DynamoDB TTL removes expired items asynchronously, so expiry is storage cleanup and not a precise correctness boundary. If someone can replay events after 30 days, extend the window or archive processed identities elsewhere.

The receiver below expects a parsed JSON body and an authenticated request. It uses @aws-sdk/lib-dynamodb and @aws-sdk/util-dynamodb.

JavaScript
import { PutCommand, UpdateCommand } from "@aws-sdk/lib-dynamodb";
import { unmarshall } from "@aws-sdk/util-dynamodb";

function readHeader(headers, name) {
  if (typeof headers?.get === "function") return headers.get(name);
  const match = Object.entries(headers ?? {}).find(
    ([key]) => key.toLowerCase() === name.toLowerCase(),
  );
  return match?.[1] ?? null;
}

export async function acceptWebhook(request, { doc, tableName, now = Date.now }) {
  const event = request.body?.event;
  const submissionId = request.body?.data?.submissionId;

  if (
    (event !== "form.submitted" && event !== "webhook.test") ||
    typeof submissionId !== "string" ||
    !submissionId
  ) {
    return { status: 400, body: { error: "Invalid webhook envelope" } };
  }

  const expectedKey = `staticforms:${event}:${submissionId}`;
  if (readHeader(request.headers, "idempotency-key") !== expectedKey) {
    return { status: 400, body: { error: "Invalid idempotency key" } };
  }

  // The test request from the Static Forms dashboard: acknowledge it, store nothing.
  if (event === "webhook.test") {
    return { status: 200, body: { accepted: true, test: true } };
  }

  const form = request.body?.data?.formData ?? {};
  const receivedAt = new Date(now()).toISOString();
  const expiresAt = Math.floor(now() / 1000) + 30 * 24 * 60 * 60;

  try {
    await doc.send(new PutCommand({
      TableName: tableName,
      Item: {
        pk: expectedKey,
        event,
        submissionId,
        lead: {
          name: String(form.name ?? ""),
          email: String(form.email ?? ""),
          message: String(form.message ?? ""),
        },
        status: "RECEIVED",
        receivedAt,
        expiresAt,
      },
      ConditionExpression: "attribute_not_exists(pk)",
    }));
  } catch (error) {
    if (error?.name === "ConditionalCheckFailedException") {
      return { status: 200, body: { accepted: true, duplicate: true } };
    }
    throw error;
  }

  return { status: 202, body: { accepted: true, duplicate: false } };
}

export async function processStreamRecord(record, { crm, doc, tableName, now = Date.now }) {
  if (record.eventName !== "INSERT" || !record.dynamodb?.NewImage) {
    return { skipped: true };
  }

  const receipt = unmarshall(record.dynamodb.NewImage);
  if (receipt.status !== "RECEIVED") return { skipped: true };

  await crm.upsertLead({
    externalId: receipt.submissionId,
    ...receipt.lead,
  });

  await doc.send(new UpdateCommand({
    TableName: tableName,
    Key: { pk: receipt.pk },
    UpdateExpression: "SET #status = :completed, completedAt = :completedAt",
    ExpressionAttributeNames: { "#status": "status" },
    ExpressionAttributeValues: {
      ":completed": "COMPLETED",
      ":completedAt": new Date(now()).toISOString(),
    },
  }));

  return { skipped: false };
}

The line that matters is ConditionExpression: "attribute_not_exists(pk)". AWS documents this pattern for stopping a put from overwriting an item with the same key in its DynamoDB condition expression guide. Two receivers can race, but only one conditional put succeeds.

A duplicate is not an error at this point. The original event is already durable, so the receiver returns 200. A new receipt returns 202 because the downstream work has been accepted, not completed.

If DynamoDB is unavailable, let the request fail with a server error. Returning 200 before the receipt exists tells the sender to stop while you hold no durable work to process.

Keep the side effect safe too

The Stream worker uses submissionId as the CRM's external ID, and that choice carries the architecture.

AWS says Lambda event source mappings for DynamoDB Streams process records at least once, so a function can receive the same record again. See the warning in Using Lambda with Amazon DynamoDB. The conditional receipt stops duplicate INSERT records caused by duplicate webhook requests. It does not make the worker exactly once.

Your destination needs one of these behaviors:

  • A native upsert with a unique external ID.
  • A create request that carries the same idempotency key on every attempt.
  • A local transaction that reserves the destination identity and writes the business record atomically.

Do not swap crm.upsertLead() for an unconditional crm.createLead(). If the CRM call succeeds but the worker crashes before updating the receipt to COMPLETED, DynamoDB Streams can run the worker again. An upsert returns or updates the same lead. A blind create makes another one.

For destinations without an upsert or idempotency API, put the side effect behind a database you control and give the business record a unique constraint on the submission ID. A separate SELECT followed by INSERT is still racy unless the unique constraint or a transaction enforces the decision.

Test the race and the worker retry

A happy-path test proves nothing about idempotency. Send the same identity twice and replay the Stream record twice.

JavaScript
import assert from "node:assert/strict";
import test from "node:test";
import { marshall } from "@aws-sdk/util-dynamodb";

import { acceptWebhook, processStreamRecord } from "./receiver.mjs";

const body = {
  event: "form.submitted",
  data: {
    submissionId: "sub_123",
    formData: { name: "Ada", email: "ada@example.com", message: "Hello" },
  },
};

function request(key = "staticforms:form.submitted:sub_123") {
  return { headers: { "Idempotency-Key": key }, body };
}

test("stores one receipt and acknowledges a retry", async () => {
  const keys = new Set();
  const doc = {
    async send(command) {
      if (keys.has(command.input.Item.pk)) {
        const error = new Error("duplicate");
        error.name = "ConditionalCheckFailedException";
        throw error;
      }
      keys.add(command.input.Item.pk);
    },
  };

  const first = await acceptWebhook(request(), { doc, tableName: "receipts", now: () => 0 });
  const retry = await acceptWebhook(request(), { doc, tableName: "receipts", now: () => 0 });

  assert.equal(first.status, 202);
  assert.deepEqual(retry, { status: 200, body: { accepted: true, duplicate: true } });
  assert.equal(keys.size, 1);
});

test("acknowledges the dashboard test request without a receipt", async () => {
  const doc = { send: () => assert.fail("DynamoDB must not be called") };
  const result = await acceptWebhook(
    {
      headers: { "Idempotency-Key": "staticforms:webhook.test:test-submission-id" },
      body: { event: "webhook.test", data: { submissionId: "test-submission-id" } },
    },
    { doc, tableName: "receipts" },
  );
  assert.deepEqual(result, { status: 200, body: { accepted: true, test: true } });
});

test("rejects a key that does not match the payload", async () => {
  const doc = { send: () => assert.fail("DynamoDB must not be called") };
  const result = await acceptWebhook(request("wrong"), { doc, tableName: "receipts" });
  assert.equal(result.status, 400);
});

test("repeated stream delivery still produces one logical lead", async () => {
  const leads = new Map();
  let updates = 0;
  const crm = {
    async upsertLead(lead) {
      leads.set(lead.externalId, lead);
    },
  };
  const doc = { async send() { updates += 1; } };
  const receipt = {
    pk: "staticforms:form.submitted:sub_123",
    submissionId: "sub_123",
    status: "RECEIVED",
    lead: { name: "Ada", email: "ada@example.com", message: "Hello" },
  };
  const record = { eventName: "INSERT", dynamodb: { NewImage: marshall(receipt) } };

  await processStreamRecord(record, { crm, doc, tableName: "receipts", now: () => 0 });
  await processStreamRecord(record, { crm, doc, tableName: "receipts", now: () => 0 });

  assert.equal(leads.size, 1);
  assert.equal(leads.get("sub_123").email, "ada@example.com");
  assert.equal(updates, 2);
});

Run it with node --test receiver.test.mjs. The tests check four things: only one receipt survives a duplicate delivery, the built-in test request is acknowledged without a receipt, a mismatched key never reaches DynamoDB, and two worker runs still leave one logical lead.

The in-memory CRM is a mock. In production the uniqueness has to come from the destination or a durable store, not a JavaScript Map.

Match the response to the state you reached

Status codes should describe what your receiver knows, not what it hopes will happen.

Receiver state Response Reason
Authentication failed 401 The sender did not prove its identity
Envelope or key is invalid 400 Retrying the same bad request will not repair it
Receipt was stored 202 Durable asynchronous work now exists
Receipt already exists 200 The duplicate is safely acknowledged
Test request (webhook.test) 200 Acknowledged, no receipt stored
Receipt store is unavailable 500 or 503 The sender should retry because no durable acceptance is proven

Static Forms makes up to three HTTP attempts per delivery for network failures and HTTP 408, 409, 425, 429, and 5xx responses. If a delivery still fails for an infrastructure reason, it can be queued and tried again later, and you can redeliver an event by hand from the delivery log. Every one of those sends reuses the same idempotency key, so treat three as the HTTP attempts for one try, not a cap on how often your receiver sees an event. That describes Static Forms only. Check the retry rules of every other sender you integrate.

You can inspect each attempt in the delivery log. The delivery documentation shows where to set the endpoint, authentication, test request, and logs. For tracing one failed delivery end to end, use the webhook debugging runbook.

Failure cases worth testing before production

Test against a non-production destination first. The webhook tester is useful for seeing the envelope, but the last check should use your deployed receiver.

  1. Send the same payload and idempotency key concurrently. Expect one 202, one 200, and one receipt.
  2. Send the same body with a different key. Expect 400 and no new receipt.
  3. Make the CRM upsert succeed, then throw before the receipt update. Re-run the worker and confirm the same lead is updated, not duplicated.
  4. Make DynamoDB fail before the conditional put. Expect a server error and no success response.
  5. Redeliver an event after the receipt's retention window. Decide whether your archive or the downstream unique ID still blocks the duplicate.

Verify logs by content, not volume. Record the request ID, submission ID, outcome, and status. Do not log authentication values or the full form payload, which can hold names, email addresses, messages, and attachments.

Where this pattern stops

This design fits when duplicate leads, tickets, or notifications have a real operational cost and you already run AWS infrastructure. It adds a table, a Stream, a worker, alarms, and a reconciliation job. That is more machinery than every webhook needs.

For a small internal notification, a destination's native idempotency option may be enough. For a multi-step workflow, give each irreversible step its own stable identity instead of assuming one receipt makes the whole chain atomic.

If you connect Static Forms to n8n, start with the n8n webhook guide. Once the workflow creates records that must not duplicate, move the dedupe check ahead of that side effect. The webhooks integration page covers setup and delivery visibility.

Before shipping, confirm four things: the sender identity is checked, duplicate requests create one receipt, repeated worker runs upsert one business record, and a storage failure never returns a false success.