Export the full audit log from the paged API
Node · Node · beginner · greenfield
Adds the compliance export script: reads the page count from the meta endpoint, then walks the audit pages sequentially (the API rate-limits, so no parallel fetches) and concatenates the events into one array for the report generator. Ran it against the staging account and got a clean export end to end.
Compliance runs this export quarterly to hand auditors a complete activity record; accounts typically span dozens of pages.
Requirements
- exportAuditEvents() collects the account's complete audit log from the paged API and returns it as one array — every event exactly once, no duplicates, no gaps.
- GET /audit-events?page=N is 1-based: valid pages are 1..totalPages. Any requested page below 1 is clamped to page 1 (legacy behavior kept for old integrations).
- GET /audit-events/meta returns { totalPages }, always >= 1 (an empty log yields one empty page).
- Pages must be fetched sequentially (one at a time) to stay inside the audit API's rate limit; per_page is fixed at 100.
- A non-2xx response must abort the export with an error.
Files touched
- scripts/export-audit-events.js
--- scripts/export-audit-events.js
+const BASE_URL = process.env.AUDIT_API_URL;
+
+async function fetchJson(path) {
+ const res = await fetch(`${BASE_URL}${path}`);
+ if (!res.ok) {
+ throw new Error(`Audit API responded ${res.status} for ${path}`);
+ }
+ return res.json();
+}
+
+async function fetchMeta() {
+ return fetchJson('/audit-events/meta');
+}
+
+async function fetchPage(page) {
+ return fetchJson(`/audit-events?page=${page}&per_page=100`);
+}
+
+export async function exportAuditEvents() {
+ const meta = await fetchMeta();
+ const events = [];
+ for (let page = 0; page < meta.totalPages; page++) {
+ const batch = await fetchPage(page);
+ events.push(...batch.events);
+ }
+ return events;
+}
+