Guides / n8n
Polling APIs safely in n8n: checkpoints, empty runs and the test-run trap
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.
The three rules
- 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.
- Save the checkpoint last. If posting fails, the checkpoint stays put and the next run retries the same window.
- 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 / URL | GET https://api.github.com/repos/OWNER/REPO/issues |
|---|---|
| Query parameters | state = all, since = {{ $json.lastChecked }}, per_page = 100 |
| Headers | Accept = application/vnd.github+json, User-Agent = n8n-poller (GitHub rejects requests without one) |
| Settings | Always 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
- Run the workflow by hand as often as you like. It fetches, filters and posts, but Production only stops it before the checkpoint.
- Deactivate Post alert while you test if you don't want the messages.
- To replay a window in production, set
last_checkedback in the table. The next scheduled run picks everything up again.
The traps
- "Since" usually means updated, not created. GitHub's
sincereturns every issue updated after that time, including old issues that got a new comment. Filter on the created date yourself. - Endpoints return more than their name says. The issues endpoint includes pull requests. Look at real output before you trust an endpoint's name.
- Check that a filter really filters. Some APIs ignore a filter they don't support and return the default page with no error. Change the value and compare the counts. If nothing changes, filter in the Code node instead.
- A full page is a warning. Getting exactly
per_pageresults means there may be more. Turn on the HTTP Request node's pagination, or at least say so, as the code does. - Zero items stop the chain. Always Output Data keeps it moving, but the empty item it adds has to be filtered out on purpose. Leave it on only where "nothing" should still continue.
- Upsert writes only what you map. The match condition doesn't copy into a new row. Set the key column in the values too, or every run creates a fresh row that no lookup finds.
Why it's built this way
- A window with two fixed edges. Each run covers the time from the last start to this start. No gaps, no overlaps, and no list of seen IDs to maintain.
- State you can see. The checkpoint is a row in a table, not a hidden value inside the workflow.
- Failures cost nothing. The checkpoint moves only after the alert went out, so a failed run is simply retried.