Guides / n8n

Polling APIs safely in n8n: checkpoints, empty runs and the test-run trap

Intermediate, 9 min read,

Triggers are great until the source changes under you, or until you can't set one up at all. A poller works every time: ask the API what's new on a schedule. Done badly, it misses items, repeats them, or quietly eats your data every time you test it. Done properly, it's one small workflow you can copy for every API.

The example polls a public GitHub repository for new issues and posts each run's new ones to chat. Swap the API and the pattern stays the same.

ScheduleEvery 15 min
Data TableGet checkpoint
CodeSet window
HTTP RequestFetch issues
CodeKeep new issues
HTTP RequestPost alert
Data TableSave checkpoint

The three rules

  1. The checkpoint is the time the run started, not the time it finished. Anything created mid-run belongs to the next window, so nothing falls between two runs.
  2. Save the checkpoint last. If posting fails, the checkpoint stays put and the next run retries the same window.
  3. Test runs never save it. Otherwise every test consumes the items your production run was waiting for.

1. Create the checkpoint table

In n8n, create a Data Table called poll_checkpoints with two string columns: name and last_checked. One table serves every poller you build; each one uses its own name.

Why not workflow static data: it isn't saved in test runs, n8n flags it as unreliable at high frequency, and you can't see or edit it. A table row you can read, fix, and set back by hand to replay a window.

2. Build the workflow

Schedule Trigger: Every 15 min

Every 15 minutes is four calls an hour, well under GitHub's 60 unauthenticated calls per hour.

Data Table: Get checkpoint

Operation Get, table poll_checkpoints, one condition: name Equals github-issues. In the node's Settings, turn on Always Output Data. On the very first run there is no row yet, and without this setting the workflow would stop right here.

Code: Set window

Run Once for All Items mode:

const row = $input.first().json;
const runStartedAt = new Date().toISOString();
// First run: look back one hour instead of fetching everything.
const lastChecked = row.last_checked || new Date(Date.now() - 3600 * 1000).toISOString();

return [{ json: { runStartedAt, lastChecked } }];

This node fixes the window once. Every later node reads it from here, so the edges can't drift during the run.

HTTP Request: Fetch issues

Method / URLGET https://api.github.com/repos/OWNER/REPO/issues
Query parametersstate = all, since = {{ $json.lastChecked }}, per_page = 100
HeadersAccept = application/vnd.github+json, User-Agent = n8n-poller (GitHub rejects requests without one)
SettingsAlways Output Data on

Always Output Data matters on quiet days: an empty answer would otherwise produce zero items, and a node with zero items stops the chain before the checkpoint is saved.

Code: Keep new issues

Run Once for All Items again:

const { runStartedAt, lastChecked } = $('Set window').first().json;
const from = Date.parse(lastChecked);
const to = Date.parse(runStartedAt);

const fetched = $input.all().map(item => item.json).filter(i => i.id); // drops the empty item
const issues = fetched
  .filter(i => !i.pull_request) // this endpoint returns pull requests too
  .filter(i => {
    const created = Date.parse(i.created_at);
    return created >= from && created < to; // new in this window, not just updated
  });

const lines = issues.map(i => `#${i.number} ${i.title} ${i.html_url}`);
if (fetched.length === 100) lines.push('A full page came back: some issues may be missing. Add pagination.');

return [{ json: { count: issues.length, text: `New issues:\n${lines.join('\n')}` } }];

It always returns exactly one item, even when nothing is new. That keeps the rest of the workflow simple.

IF: Anything new?

Condition: {{ $json.count }} is greater than 0. Connect the true output to Post alert. Connect the false output straight to the next step, Production only.

HTTP Request: Post alert

Post {{ $json.text }} to your chat webhook, exactly as in the error workflow guide. Connect its output to Production only as well. Both IF branches now meet in the same node, and only one of them runs each time.

Filter: Production only

Condition: {{ $execution.mode }} is equal to production. Manual runs in the editor have the mode test and stop here.

Data Table: Save checkpoint

Operation Upsert, table poll_checkpoints, one condition: name Equals github-issues. Under Values to upsert (Mapping Column Mode Map Each Column Manually), set name to github-issues and last_checked to {{ $('Set window').first().json.runStartedAt }}. The condition only finds the row. On the first run Upsert creates it with just the values set here, so without name the next run can't find its checkpoint.

Set an error workflow in the workflow's settings and publish. A failed run now alerts you, keeps its checkpoint, and catches up on the next run.

3. Test without eating your data

The traps

Why it's built this way