JavaScript
Use this example on a trusted backend with Fetch API support. Keep ECHORA_API_KEY in a server environment variable, not a public frontend bundle. The async generator follows subsequent pages with cursor only.
const BASE_URL = process.env.ECHORA_BASE_URL?.replace(/\/$/, "");const API_KEY = process.env.ECHORA_API_KEY;if (!BASE_URL || !API_KEY) throw new Error("Missing Echora config");
async function apiGet(path, params = {}, parseJSON = JSON.parse) { const url = new URL(BASE_URL + path); for (const [key, value] of Object.entries(params)) { url.searchParams.set(key, String(value)); } const response = await fetch(url, { headers: { "X-API-Key": API_KEY, Accept: "application/json" }, signal: AbortSignal.timeout(90000), }); const text = await response.text(); if (!response.ok) { let code = "http_error"; try { code = JSON.parse(text).error?.code || code; } catch {} throw new Error(`Echora HTTP ${response.status}: ${code}`); } return parseJSON(text);}
async function* getAllData(source, keywordPath, filters, parseJSON = JSON.parse) { const path = `/v1/data/${encodeURIComponent(source)}/` + encodeURIComponent(keywordPath); let params = { ...filters }; while (true) { const page = await apiGet(path, params, parseJSON); for (const item of page.data) yield item; if (!page.pagination.has_more) return; const cursor = page.pagination.next_cursor; if (!cursor) throw new Error("Missing next_cursor"); params = { cursor }; }}
async function main() { const source = "news"; const keywords = await apiGet("/v1/keywords"); const slot = keywords.data[source].find(item => item.is_active && item.within_quota && item.path); if (!slot) throw new Error("No eligible keyword");
for await (const item of getAllData(source, slot.path, { start: "2026-11-12", end: "2026-11-18", limit: 100, })) { // Upsert using the unique key (item.source_key, item.id). // Replace this line with your application storage. console.log(item.source_key, item.id); }}
main().catch(error => { console.error(error.message); process.exitCode = 1;});Integration notes
Section titled “Integration notes”JSON.parse uses JavaScript Number. If exact large-count precision is required, pass a lossless JSON parser through parseJSON before the first numeric conversion. Content IDs remain strings. Use source_key + id for upserts. The example performs no automatic retries.
After a keyword change, start a fresh query with its new path. Recheck keyword state before retrying timed-out writes. See pagination and errors.