HOW-TO

How to add a heartbeat ping to an n8n workflow safely

Published · Updated

Code

This script builds a small workflow with two pings, checks the rules above, and prints JSON you can import into n8n.

js
// Build an n8n workflow with heartbeat pings on side branches.
const PING_URL = "https://example.com/ping"; // replace with your monitor URL

const link = (node) => ({ node, type: "main", index: 0 });

function ping(name, position, event, countExpr) {
  return {
    name, position,
    type: "n8n-nodes-base.httpRequest",
    typeVersion: 4.2,                 // always explicit
    executeOnce: true,                // one ping per run, not one per item
    onError: "continueRegularOutput", // a monitor outage cannot fail the run
    parameters: {
      method: "POST",
      url: PING_URL,
      sendBody: true,
      bodyParameters: { parameters: [
        { name: "event", value: event },
        { name: "count", value: countExpr },
      ] },
      options: { timeout: 5000 },
    },
  };
}

function code(name, position, jsCode) {
  return { name, position, type: "n8n-nodes-base.code", typeVersion: 2, parameters: { jsCode } };
}

const workflow = {
  name: "Heartbeat example",
  nodes: [
    { name: "Start", position: [0, 0], type: "n8n-nodes-base.manualTrigger", typeVersion: 1, parameters: {} },
    code("Work", [220, 0], "return [{ json: { n: 1 } }, { json: { n: 2 } }];"),
    code("Use data", [440, 0], "return $input.all();"),
    ping("Ping start", [220, 200], "start", "0"),
    ping("Ping finish", [440, 200], "finish", "={{ $input.all().length }}"),
  ],
  // Each ping is a second target of the same output, with nothing after it.
  connections: {
    Start: { main: [[link("Work"), link("Ping start")]] },
    Work: { main: [[link("Use data"), link("Ping finish")]] },
  },
  settings: {},
};

function checkWorkflow(wf) {
  const problems = [];
  const names = new Set();
  for (const { name } of wf.nodes) names.add(name);
  for (const { name, typeVersion, executeOnce, onError, parameters } of wf.nodes) {
    if (typeof typeVersion !== "number") problems.push(`${name}: missing typeVersion`);
    if (parameters.url !== PING_URL) continue; // rules below are for ping nodes only
    if (wf.connections[name]) problems.push(`${name}: ping must be a dead end`);
    if (executeOnce !== true) problems.push(`${name}: executeOnce must be true`);
    if (onError !== "continueRegularOutput") problems.push(`${name}: onError must continue`);
  }
  for (const [from, { main }] of Object.entries(wf.connections))
    for (const outputs of main)
      for (const { node } of outputs)
        if (!names.has(node)) problems.push(`${from}: unknown target ${node}`);
  return problems;
}

const problems = checkWorkflow(workflow);
if (problems.length) {
  console.error(problems.join("\n"));
  process.exitCode = 1;
} else {
  console.log(JSON.stringify(workflow, null, 2));
}

Why it happens

An HTTP Request node outputs the response it received, not the items that came in. If the ping sits between two nodes, the next node gets the monitor's reply instead of your data. The run can stay green while it processes the wrong thing.

The second trap is in imported JSON. In the source report, nodes without typeVersion were imported as a different version. The Code node showed its default sample code, and the run failed with "Problem in node – Unknown error".

Steps

  1. Replace PING_URL with the address of your monitoring endpoint.
  2. Run the script with Node 22 and save the printed JSON to a file.
  3. In n8n, open a new workflow and choose Import from File.
  4. Check that each ping node hangs off a side branch, with no node after it.
  5. Replace the "Work" and "Use data" nodes with your real nodes, and keep the ping branches as they are.

Pitfalls

  • The versions here (Code node 2, HTTP Request 4.2) come from a report on n8n 2.49. To confirm yours, add the node in the editor, export, and read typeVersion.
  • Without Execute Once, a run with 12 items sends 12 pings.
  • If the monitor needs a token, store it in an n8n credential, not in the URL or the JSON file.
  • The source also reports that retrying a failed execution re-ran the failed write and created a duplicate record. The retry got a new execution id, so do not use that id as an idempotency key.

Seen on: n8n Community ·

Related service: n8n, Make and Zapier workflow automation