MongoDB and Code: Dead-Letter Queue for Failed Events Template

This Spojit template captures any event that fails processing into a MongoDB dead-letter collection so you can inspect, fix, and replay it later instead of losing it.

What It Builds

A Webhook trigger receives an incoming event and the workflow attempts its normal processing. If a step fails, a Code connector node runs execute-javascript to wrap the original payload together with the error details and a timestamp into a single record, and a MongoDB connector node uses insert-documents to write that record into a dead_letter collection. The result is a durable failure-capture pattern: nothing silently disappears, and every failed event is parked with enough context to replay it once the underlying issue is resolved.

The Prompt

Paste this into Miraxa, the intelligent layer across your automation, and it builds the workflow, connecting the tools for you:

Build a workflow with a Webhook trigger that processes an incoming event. Wrap the processing steps so that if any step fails, a Code node uses execute-javascript to build a dead-letter record containing the original event payload, the error message, and the current timestamp, then a MongoDB node inserts that record into a collection named dead_letter for later inspection and replay.

Connectors Used

  • Webhook trigger - receives the incoming event as JSON and starts the run.
  • code - execute-javascript assembles the dead-letter record (original payload, error, timestamp) from the failed run.
  • mongodb - insert-documents writes the record into your dead_letter collection for inspection and replay.

Customize It

Change the collection name in the prompt from dead_letter to match your database, and adjust which fields the Code node captures (for example add the event type, a retryCount, or the original Webhook headers). To replay later, build a second workflow on a Schedule trigger that uses find-documents to pull unresolved records, reprocesses them, and uses update-documents to mark them as cleared.

Tips

  • Use the Connector node in Direct mode for both the Code and MongoDB steps so the capture is deterministic and costs no AI credits.
  • Store a clear status field (such as status: "pending") on each record so your replay workflow can find only the events that still need attention.
  • Index the dead_letter collection on the field you replay by (event id or received timestamp) so lookups stay fast as the queue grows.

Common Pitfalls

  • Make sure the MongoDB connection used by insert-documents can write to the target database, not just read from it, or the capture step itself will fail.
  • Keep the dead-letter record small and serializable: pass the parsed event body, not large binary blobs, into execute-javascript so the document inserts cleanly.
  • Avoid re-inserting the same event twice on retries by including the original event id in the record and checking for it before writing.

Related Articles

Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.