Skip to content
AllEventsAllEventsDevelopers

Keep a local copy in sync with the batch feed

Events in Batch returns every upcoming event in your coverage, up to 1,000 per call, with a cursor that remembers where you stopped. This guide keeps a local copy of those events up to date with a script you run on a schedule.

You need an API key and Node.js 18 or later. The script runs on your server or as a scheduled job, never in a browser.

How the feed works

  1. The first call has no next_cursor.
  2. Each response has a next_cursor: send it on the next call.
  3. When message is END_OF_RESULTS (has_more is false), you have everything for now. That answer's next_cursor is empty: keep the cursor you sent.
  4. Next time, start from the saved cursor to get events published since.

next_cursor is an opaque string that changes on every response: store it and send it back untouched. A call can return fewer events than count, or none, while the cursor still moves on (with narrow coverage, message can be NO_COVERED_EVENTS_YET). Keep going until END_OF_RESULTS. Calls that return events use credits (your cost per call is in the portal, and each answer's credits_used says what it cost); END_OF_RESULTS and NO_COVERED_EVENTS_YET answers cost 0.

The sync script

The script keeps its cursor and events in a JSON file so it runs as is. In production, write them to your database instead.

JavaScript
// sync.js: run it every hour or so, for example from cron.import { readFile, writeFile } from "node:fs/promises";const BASE = process.env.AE_BASE_URL; // https://developer.allevents.in/v1const KEY = process.env.AE_API_KEY;const STORE = "./ae-events.json";const sleep = (s) => new Promise((r) => setTimeout(r, s * 1000));async function load() {  try {    return JSON.parse(await readFile(STORE, "utf8"));  } catch {    return { cursor: "", events: {} };  }}/** One batch call, with retries on 429 (rate limit) and 503 (temporary failure). */async function fetchBatch(cursor) {  for (let attempt = 1; ; attempt++) {    const params = new URLSearchParams({ count: "1000" });    if (cursor) params.set("next_cursor", cursor);    const response = await fetch(`${BASE}/events/batch?${params}`, {      headers: { "X-API-Key": KEY },      signal: AbortSignal.timeout(45_000),    });    if ((response.status === 429 || response.status === 503) && attempt < 5) {      await sleep(Number(response.headers.get("retry-after")) || 5 * attempt);      continue;    }    const body = await response.json();    if (response.status === 400 && cursor) return { invalidCursor: true };    if (!response.ok) throw new Error(`events/batch ${response.status}: ${body.message}`);    return body;  }}export async function sync() {  const store = await load();  let cursor = store.cursor;  for (;;) {    const body = await fetchBatch(cursor);    if (body.invalidCursor) {      cursor = ""; // the cursor is no longer valid (or an old c<number> one): start a full sync again      continue;    }    for (const event of body.data ?? []) {      store.events[event.event_id] = {        name: event.eventname,        start_at: event.start_at, // venue time, ISO 8601 with offset: show as written        end_ts: event.end_ts, // real UTC, Unix seconds        when: event.display.short,        city: event.venue.city,        url: event.event_url,        synced_at: Date.now(),      };    }    if (body.next_cursor) cursor = body.next_cursor; // empty at END_OF_RESULTS: keep the last one    if (body.message === "END_OF_RESULTS") break;  }  // Drop events that have ended: end_ts is real UTC.  const now = Date.now() / 1000;  for (const [id, e] of Object.entries(store.events)) {    if (e.end_ts < now) delete store.events[id];  }  store.cursor = cursor;  await writeFile(STORE, JSON.stringify(store));  console.log(`Synced: ${Object.keys(store.events).length} events`);}await sync();

Run it with node sync.js. The first run pages through everything in your coverage; later runs only fetch what's new.

Refresh what changed

New events arrive through the cursor. To pick up changes to events you already have, send updated_after (a Unix timestamp; compare each event's updated_at) with a fresh sync from an empty cursor, for example once a day:

curl "$AE_BASE_URL/events/batch?updated_after=1795000000&count=1000" \  -H "X-API-Key: $AE_API_KEY"

Before you show one event's details, you can re-read it with Event Details. Add cached=false only if your copy must be the latest; a cached answer costs 0 credits (credits_used: 0).

Keep the copy honest

  • Delete events once they have ended: the API never returns them again. Refresh or delete data that is out of date (see the API Terms of Use in the developer portal).
  • Store event_id as a string: it's a string on every API, and can be longer than JavaScript's safe integers.
  • Every API returns the same Event fields; Events in Batch also sends description, schedule.dates, video_url and updated_at. See Field types by API.
  • If you show the events publicly, credit "Data from AllEvents" with a link to allevents.in.