HOW-TO

How to alert when an n8n fetch returns 200 but empty data

Published · Updated

Code

js
// n8n Code node, mode "Run Once for All Items".
// Place it right after the fetch node.
// Enable "Always Output Data" in the fetch node settings.
const WINDOW = 7;       // healthy runs to remember
const MIN_HISTORY = 3;  // runs needed before the median is trusted
const DROP_RATIO = 0.5; // alert below half of the median

function median(values) {
  const s = [...values].sort((a, b) => a - b);
  const mid = Math.floor(s.length / 2);
  return s.length % 2 ? s[mid] : (s[mid - 1] + s[mid]) / 2;
}

function checkRowCount(count, history) {
  const base = history.length >= MIN_HISTORY ? median(history) : null;
  if (count === 0) return { alert: true, reason: 'zero rows', median: base };
  if (base !== null && count < base * DROP_RATIO) {
    return { alert: true, reason: 'below half of median', median: base };
  }
  return { alert: false, reason: 'ok', median: base };
}

function run(items, store) {
  // "Always Output Data" sends one empty item when the list is empty.
  const count = items.filter(
    (i) => i.json && Object.keys(i.json).length > 0
  ).length;
  const history = Array.isArray(store.rowCounts) ? store.rowCounts : [];
  const result = checkRowCount(count, history);
  // Only healthy runs enter the history, so bad runs cannot lower the baseline.
  if (!result.alert) store.rowCounts = [...history, count].slice(-WINDOW);
  return [{ json: { count, ...result } }];
}

// Inside n8n the Code node globals exist. Outside n8n this block is skipped.
if (typeof $input !== 'undefined') {
  return run($input.all(), $getWorkflowStaticData('global'));
}

Why it happens

n8n marks a run as successful when no node throws an error. An API that answers 200 with an empty list is not an error, so the run stays green. The same happens when a filter upstream changes, a token loses a scope, or a page moves. Nobody looks at a green run, so the gap can last for days.

Steps

  1. Open the fetch node, go to Settings, and turn on "Always Output Data". Without it, an empty result stops the workflow and the check never runs.
  2. Add a Code node after the fetch node, set the mode to "Run Once for All Items", and paste the code.
  3. The code counts the items that have at least one field.
  4. It reads the last seven healthy counts from workflow static data and computes their median.
  5. It sets alert to true when the count is zero, or below half of the median once three runs are stored.
  6. Add an IF node on {{ $json.alert }}. Send the true branch to Slack, email or Telegram.
  7. Connect the fetch node to your normal processing as a second branch, so the rows still flow on.

Pitfalls

  • Static data is saved only for active workflows started by a trigger. Manual test runs do not keep the history.
  • A zero count always alerts. If an empty result is normal for your source, such as on weekends, skip those days before the check.
  • Alerting runs are not stored. If the lower volume is real and permanent, clear rowCounts once so a new baseline can form.
  • Keep the alert message to counts and the reason. Do not put row contents or keys in it.

Seen on: n8n Community ·

Related service: n8n, Make and Zapier workflow automation