# Read a player's own ledger

You are writing a small program that reads a player's own money history from Osogbo Life (https://osogbolife.app), a life-simulation game set in Osogbo, Nigeria. Every amount that reaches or leaves a player's wallet in the game is one row in its ledger, and a player can read their own rows from outside the game. Build it in this folder.

NOT OPEN YET: Osogbo Life has not switched this API on today. Everything below is built; write the program and test it against the stand-in below. When it opens, a player makes a token at https://osogbolife.app/developers/ledger.

Ask me what I want from my history if I have not said; if you cannot ask, build all three of these: a summary of money in and out by kind, month by month (a month in Lagos time, which is UTC+1); a CSV file of every row; a check that my balance equals the newest row's balance_after and equals the sum of every row's delta.

WHAT THE API IS. Read-only. It answers for the player whose token is sent, and for nobody else. Nothing here can move money.
- The player makes a token at https://osogbolife.app/developers/ledger while signed in (5 at most). It starts with il_led_, is shown once, and can be revoked there at any time.
- Send it in a header on every request: Authorization: Bearer <token>. Never put it in the address, where it would be logged.
- GET https://osogbolife.app/api/v1/ledger/me answers { "success": true, "data": { "balance": 15000, "frozen": false } }.
- GET https://osogbolife.app/api/v1/ledger/me/rows?limit=50 answers { "success": true, "data": [ { "id": 9912, "at": "2026-10-12T19:04:11.000Z", "kind": "wage", "delta": 4500, "balance_after": 15000 } ], "meta": { "next": 9840 } }. Rows come newest first, up to 100 a page: send limit=100. For the page after, send before=<the next you were given>; next is null on the last page. A row is those five fields and nothing else: delta is minus for money out, at is a time in UTC, kind is a short word such as wage, food, rent. The first row a player ever has is their starting cash, so a balance is the sum of every delta; balance_after can be null on a very old row.
- GET https://osogbolife.app/api/v1/audit needs no token and answers { "success": true, "data": { "intact": true, "entries": 1234, "checked_at": "..." } }: whether the log of staff actions is unbroken. It never returns an entry.
- A refusal is { "success": false, "error": { "code": "...", "message": "..." } } with HTTP 401 for bad_token (missing, wrong or revoked), 429 for rate_limited and 503 for switched_off. At most 60 requests a minute for the ledger and 30 a minute for the audit state: read the pages one after another, and wait 1 second between pages once you are past page 48. The city can change these numbers: the message of a 429 says the one in force. On a 429, stop, say how many rows you had read, and write nothing half-done.
- It is for a server or a script. A page in a browser on another site cannot call it.
- The money is the game's own, written ₳. Amounts are whole numbers.

THE TOKEN IS A SECRET. Read it from the environment variable LEDGER_TOKEN and nowhere else. Never write it into a file, never print it, never put it in a commit. If LEDGER_TOKEN is missing or empty, stop with one clear line saying how to set it. Read the address from LEDGER_BASE (ignore a slash at its end), and use https://osogbolife.app when it is not set. Send the token only to an https address, or to 127.0.0.1 or localhost for the stand-in: refuse any other LEDGER_BASE before sending anything.

WHAT TO WRITE. Two files, in plain Node (version 18 or later, no packages), unless I ask for Python or another language:
1. ledger.mjs: the program. It reads the balance, then every page of rows until next is null (the page size from LEDGER_PAGE, 100 when not set), then does what I asked, then prints one line with the audit state. It checks success on every answer; on a refusal it prints the HTTP status, the code and the message, and exits with 1. A CSV goes to ledger.csv in this folder, with the header id,at,kind,delta,balance_after, newest first; that file is the player's money history, so say that it should be kept out of anything public. With no rows at all, say the balance was not checked. It ends with a line that starts "ok:" and says how many rows it read; if a check of mine fails, the last line starts "not ok:" and it exits with 1.
2. mock.mjs: exactly the script below, a stand-in for the API on this machine with one token, il_led_test, and seven sample rows. Run it, then run your program against it, and fix your program until it ends with "ok:". Run it once more with LEDGER_PAGE=3, which takes three requests, to see the pages join. Also try it with a wrong token and show me that it stops with the refusal. The stand-in never answers 429 or 503: handle those from the words above.

```js
// mock.mjs: a stand-in for the API on this machine, so the script can be written and tried with no real token.
//   node mock.mjs      then      LEDGER_BASE=http://127.0.0.1:4801 LEDGER_TOKEN=il_led_test node ledger.mjs
import { createServer } from "node:http";
// Seven rows that add up, oldest to newest, across two months; served newest first, as the real API does.
let bal = 0; const rows = [["starting_cash", 5000], ["wage", 4500], ["food", -650], ["wage", 4500], ["rent", -3000], ["food", -700], ["wage", 4500]]
  .map(([kind, delta], i) => ({ id: i + 1, at: new Date(Date.UTC(2026, 8, 28 + i * 2, 18)).toISOString(), kind, delta, balance_after: (bal += delta) })).reverse();
const send = (r, status, body) => { r.writeHead(status, { "content-type": "application/json" }); r.end(JSON.stringify(body)); };
createServer((q, r) => {
  const u = new URL(q.url, "http://x");
  if (u.pathname === "/api/v1/audit") return send(r, 200, { success: true, data: { intact: true, entries: 1234, checked_at: new Date().toISOString() } });
  if (q.headers.authorization !== "Bearer il_led_test") return send(r, 401, { success: false, error: { code: "bad_token", message: "Send your token as: Authorization: Bearer il_led_..." } });
  if (u.pathname === "/api/v1/ledger/me") return send(r, 200, { success: true, data: { balance: rows[0].balance_after, frozen: false } });
  if (u.pathname === "/api/v1/ledger/me/rows") { const n = Math.min(100, Math.max(1, Number(u.searchParams.get("limit")) || 50)), b = Number(u.searchParams.get("before")) || Infinity, left = rows.filter((x) => x.id < b), page = left.slice(0, n);
    return send(r, 200, { success: true, data: page, meta: { next: left.length > n ? page[page.length - 1].id : null } }); }
  send(r, 404, { success: false, error: { code: "not_found", message: "No such address." } });
}).listen(4801, "127.0.0.1", () => console.log("mock ledger API on http://127.0.0.1:4801"));
```

When you finish, tell me: what the program prints against the stand-in, what it does with a wrong token, and how to run it for real (set LEDGER_TOKEN in the shell, then node ledger.mjs).
