Attendance into HR and payroll
An HR or payroll system usually wants two things: who worked when (first in, last out, breaks) and how long (hours, overtime). Punchly records both from every door — the web, the phone, the desktop app and kiosks — so the integration doesn't care how people clock in.
There are two ways to bring it across. Most integrations use the first; add the second if the HR system should show who is in right now.
| Nightly | Live | |
|---|---|---|
| What arrives | One row per person per day: first in, last out, worked, break and overtime hours | Every clock-in, break and clock-out, seconds after it happens |
| How | POST /reports/attendance, on a schedule |
Webhooks with the four attendance.* events |
| Needs | A workspace key, Read-only | A webhook, made once with a key that has webhooks:manage |
Before you start
An owner or admin makes a workspace key under Workspace settings → Integrations → API keys, with Read-only access. It reaches only this workspace, reads everyone's attendance, and changes nothing. See Authentication.
Nightly: a CSV of the day
The attendance report has already done the arithmetic: each row is one person on one day, with the first clock-in, the last clock-out, and the hours worked, on break and over capacity. This script asks for a period and writes it as CSV; run it every night for yesterday, or once a pay period for the whole period.
// Attendance for a period, one row per person per day worked, as CSV.
// Node 18 or later; no packages. Usage:
// PUNCHLY_API_KEY=pkw_… node attendance-daily.mjs 2026-09-01 2026-09-30 > attendance.csv
// With no dates it reports yesterday. PUNCHLY_TIME_ZONE sets whose days they are (IANA name).
const API = process.env.PUNCHLY_API_URL ?? 'https://api.punchly.work/v1';
const KEY = process.env.PUNCHLY_API_KEY;
const TZ = process.env.PUNCHLY_TIME_ZONE ?? Intl.DateTimeFormat().resolvedOptions().timeZone;
if (!KEY) throw new Error('Set PUNCHLY_API_KEY');
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
async function punchly(method, path, body) {
for (let attempt = 0; ; attempt++) {
const res = await fetch(`${API}${path}`, {
method,
headers: { 'X-Api-Key': KEY, ...(body && { 'Content-Type': 'application/json' }) },
...(body && { body: JSON.stringify(body) }),
});
if (res.status === 429 && attempt < 5) {
await sleep(Number(res.headers.get('retry-after') ?? 5) * 1000);
continue;
}
const text = await res.text();
if (!res.ok) throw new Error(`${method} ${path}: ${res.status} ${text}`);
return text ? JSON.parse(text) : null;
}
}
async function workspaceId() {
if (process.env.PUNCHLY_WORKSPACE_ID) return process.env.PUNCHLY_WORKSPACE_ID;
const { items } = await punchly('GET', '/me/workspaces');
if (items.length !== 1) throw new Error('Set PUNCHLY_WORKSPACE_ID: the key reaches several');
return items[0].workspaceId;
}
/** `YYYY-MM-DD` of an instant in TZ. */
const dayIn = (date) => date.toLocaleDateString('en-CA', { timeZone: TZ });
/** `HH:MM` of an ISO instant in TZ, or empty. */
const timeIn = (iso) =>
iso ? new Date(iso).toLocaleTimeString('en-GB', { timeZone: TZ, hour: '2-digit', minute: '2-digit' }) : '';
const hours = (seconds) => (seconds / 3600).toFixed(2);
const csv = (values) => values.map((v) => `"${String(v ?? '').replaceAll('"', '""')}"`).join(',');
const yesterday = dayIn(new Date(Date.now() - 86_400_000));
const [from = yesterday, to = from] = process.argv.slice(2);
const wid = await workspaceId();
const report = await punchly('POST', `/workspaces/${wid}/reports/attendance`, {
range: { from, to },
tz: TZ,
});
console.log(csv(['member', 'date', 'first_in', 'last_out', 'worked_h', 'break_h', 'overtime_h', 'still_clocked_in']));
for (const day of report.days) {
if (!day.startAt) continue; // a day with no time tracked
console.log(
csv([
day.memberLabel,
day.date,
timeIn(day.startAt),
// `+1` when the last clock-out was after midnight, on the next day.
day.endAt && day.endDayOffset ? `${timeIn(day.endAt)} (+${day.endDayOffset})` : timeIn(day.endAt),
hours(day.workS),
hours(day.breakS),
hours(day.overtimeS ?? 0),
day.stillClockedIn ? 'yes' : 'no',
]),
);
}PUNCHLY_API_KEY=pkw_… PUNCHLY_TIME_ZONE=Asia/Kolkata node attendance-daily.mjs 2026-09-01 2026-09-30 > attendance.csv"member","date","first_in","last_out","worked_h","break_h","overtime_h","still_clocked_in"
"Amy Patel","2026-09-28","09:02","17:31","7.98","0.50","0.00","no"
"Ben Okafor","2026-09-28","22:00","06:05 (+1)","7.58","0.50","0.00","no"- Whose days.
tzdecides where a day starts and ends. Use the zone the HR system counts in, so a night shift lands on the day payroll expects. A clock-out after midnight shows(+1). - Still clocked in. A day that hasn't ended yet says so; run for yesterday, not today, if payroll should only see finished days.
- Entry by entry instead of per day? Use the time-entry sync; every entry says whether it was a
regularorbreakentry and, from a kiosk, which one.
Live: every clock-in as it happens
Make a webhook with the four attendance events — in Punchly under Profile → Advanced → Webhooks, or through the API — and point it at a small server like this one. It checks the signature, skips repeats, answers at once and hands each event on.
// Receive clock-ins, breaks and clock-outs as they happen, and hand each one to your system.
// Node 18 or later; no packages. Usage:
// PUNCHLY_WEBHOOK_SECRET=whsec_… node attendance-webhook.mjs
// Listens on PORT (3000). Point a webhook with the four attendance.* events at it, over https in
// production (a reverse proxy or a tunnel in front of this server).
import { createHmac, timingSafeEqual } from 'node:crypto';
import { createServer } from 'node:http';
const SECRET = process.env.PUNCHLY_WEBHOOK_SECRET;
const PORT = Number(process.env.PORT ?? 3000);
if (!SECRET) throw new Error('Set PUNCHLY_WEBHOOK_SECRET');
/** Event ids already handled, so a retry isn't recorded twice. Keep these in a database. */
const handled = new Set();
function verified(header, body) {
const parts = Object.fromEntries((header ?? '').split(',').map((kv) => kv.split('=', 2)));
const t = Number(parts.t);
if (!t || Math.abs(Date.now() / 1000 - t) > 300) return false;
const expected = createHmac('sha256', SECRET).update(`${t}.${body}`).digest('hex');
const given = Buffer.from(parts.v1 ?? '', 'utf8');
return given.length === expected.length && timingSafeEqual(given, Buffer.from(expected, 'utf8'));
}
function record(event) {
const d = event.data;
// Replace with a write to your HR system: who, what, when (UTC and local), and where.
console.log(
[event.type, d.memberId, d.at, `${d.localTime} ${d.timeZone}`, d.source, d.kioskId ?? '-', d.proof ?? '-'].join(' | '),
);
}
createServer((req, res) => {
const chunks = [];
req.on('data', (chunk) => chunks.push(chunk));
req.on('end', () => {
const body = Buffer.concat(chunks).toString('utf8');
if (req.method !== 'POST' || !verified(req.headers['punchly-signature'], body)) {
res.writeHead(400).end();
return;
}
const event = JSON.parse(body);
// Answer at once; do the work after. A slow answer is retried.
res.writeHead(204).end();
if (handled.has(event.id)) return;
handled.add(event.id);
if (event.type.startsWith('attendance.')) record(event);
});
}).listen(PORT, () => console.log(`listening on ${PORT}`));What it prints for a morning:
attendance.clocked_in | 01928f3e-… | 2026-09-30T03:30:00.000Z | 2026-09-30T09:00:00 Asia/Kolkata | kiosk | 01929b10-… | code
attendance.break_started | 01928f3e-… | 2026-09-30T07:30:00.000Z | 2026-09-30T13:00:00 Asia/Kolkata | kiosk | 01929b10-… | code- Attendance reads the timer the same way everywhere: starting with nothing running is a clock-in, stopping with nothing following is a clock-out, and moving between work and a break is a break. Switching from one task to another sends nothing.
proofsays how the person proved who they were at a kiosk: their PIN, their QRcode, or amanager_pin. Useful when payroll wants to know which clock-ins a manager keyed in.- Webhooks can arrive twice or out of order (Webhooks): the script skips an event id it has seen, and each event carries its own time, so order doesn't matter.
Both together
Webhooks are fast; a nightly run is complete. Many HR integrations take the live events for "who's in now" and let the nightly CSV be the record payroll runs on — so a webhook missed while the HR server was down never becomes a missing shift.