Bulk and spreadsheets › A month of results into CSV
A month of results into CSV
Every settled race in Great Britain last month with its winner, page by page, into one CSV file you can open in any spreadsheet.
// A month of results into CSV: page by page with the cursor
const KEY = localStorage.getItem("ahr_key");
const now = new Date();
const start = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth() - 1, 1)).toISOString().slice(0, 10);
const end = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), 0)).toISOString().slice(0, 10);
const lines = ["date,time,course,name,winner,sp,trainer"];
const cell = (v) => `"${String(v ?? "").replace(/"/g, '""')}"`;
let cursor = "";
while (true) {
const q = new URLSearchParams({ region: "GB", date_from: start, date_to: end, result_available: "1", include: "winner", limit: "200", cursor });
const body = await (await fetch("https://api.apihorseracing.com/v1/races/search?" + q, { headers: { "X-API-Key": KEY } })).json();
for (const r of body.data) lines.push([r.date, r.time, r.course, r.name, r.winner?.horse, r.winner?.sp, r.winner?.trainer].map(cell).join(","));
if (!body.meta.has_more) break;
cursor = body.meta.next_cursor;
}
const a = document.createElement("a");
a.href = URL.createObjectURL(new Blob([lines.join("\n")], { type: "text/csv" }));
a.download = "results.csv";
a.click();
What the reply holds
- data[]
- One row per race.
- winner
- The winner, its SP and trainer.
- meta.has_more
- Whether there is another page.
- meta.next_cursor
- Pass it as cursor for the next page.
When it says no
401no_key The key is missing or mistyped. Every sample reads it from AHR_KEY.403plan_needed Your plan does not reach this. The reply names the plan that does.429rate_limited Too fast. Wait the seconds in Retry-After, then try again.