# Add Arena54 rankings to this project

Goal: Show the Arena54 rankings (games or creators, this week, this month or all time) in this project.

You are a coding agent working inside my project. Do the work. Do not ask me anything you can find out by reading the project. Do not invent an endpoint, a field or an error code that is not written below.

## 1. Look at the project first

- Find the framework and language, how the project is run and tested, and the page or component that should show a ranked list, and the server code that can fetch for it.
- Find how the project reads configuration and environment variables, and which files git ignores.
- Any backend will do. Use the HTTP client the project already has. If the project has no server code at all, stop and tell me: this key must not reach a browser.
- Tell me in two or three lines what you found and what you will change, then carry on.

## 2. The contract

Base URL: `https://developers.arena54.com/api/v1` (called $BASE below).

Every answer is JSON in one envelope:

```json
{ "success": true, "data": …, "meta": { "request_id": "req_…", "mode": "test" } }
{ "success": false, "error": { "code": "…", "message": "…" }, "meta": { "request_id": "req_…", "mode": "test" } }
```

Branch on `error.code`. Show `error.message` to a person; it is written for them. Log `meta.request_id` with any failure.

A list also carries `meta.total` and `meta.next_cursor`: send that back as `cursor` for the next page; null means the end. `limit` is 1 to 100, 20 by default; a value outside that is ignored. A filter value the directory does not know returns an empty list with 200, and a cursor it cannot read returns the first page. When a key is not accepted, `meta.mode` is null.

Calls:

- GET /rankings/{kind}/{board}: One of the six boards from the head-to-head votes.
    - kind: games or creators.
    - board: week, month or all.
    - limit: Rows per page, 1 to 100. Default 20.
    - cursor: The next_cursor from the page before.

One row of `data` from /rankings/games/week, exactly as a test key returns it:

```json
{
  "rank": 1,
  "previous_rank": 2,
  "wins": 12,
  "name": "Sample Danfo Dash",
  "tagline": "Sample data: not a real game",
  "tags": [
    "racing",
    "arcade"
  ],
  "country_code": "NG",
  "url": "https://arena54.com/g/sample-danfo-dash"
}
```

A row from /rankings/creators/… has the same fields; its `tags` are labels such as “Studio”, not genre keys.

Keys:

- A test key starts `a54_test_`, works at once and returns sample rows. Build with this one.
- A live key starts `a54_live_` and returns the real directory. It needs approval; swapping it in changes nothing else.
- Send it as `Authorization: Bearer <key>`. Read it from the environment variable `ARENA54_API_KEY`, on the server only. Never put it in client code, in a game build, or in a commit.

Limits: an accepted call answers with `X-RateLimit-Limit`, `X-RateLimit-Remaining`, `X-RateLimit-Reset` (seconds) and `X-Arena54-Plan`. A refusal may carry none of them; a 429 carries `Retry-After` in seconds. Live keys only: `X-Quota-Limit` and `X-Quota-Remaining`.

Refusals:

- 401 missing_key: There is no Authorization header, or what follows “Bearer” is not shaped like an Arena54 key.
- 401 invalid_key: The key is shaped like one but is unknown, revoked, or past its grace period after a rotation.
- 404 not_found: No endpoint, game, creator or country lives at that address.
- 403 plan_required: The key’s plan does not include this endpoint. The message names the plan that does.
- 429 rate_limited: The key has made too many calls this minute. Retry-After says how long to wait.
- 429 quota_exceeded: The key’s plan has used its calls for the day or the month. Retry-After says how long until it resets.
- 503 api_unavailable: The API is switched off for a while. Try again later.

## 3. Do this, in order

1. If the project already has an Arena54 client or key variable, reuse it. Otherwise add ARENA54_API_KEY to the project’s environment example file with an empty value, and read it only in server code, loaded the way the project already loads its environment.
2. Write a server-side function `rankings(kind, board)` where kind is `games` or `creators` and board is `week`, `month` or `all`. It sends the key and returns `data` and `meta`.
3. Render the list in order with each row’s rank, name and link. Show the board’s name, a control to switch between games and creators, and one to switch between week, month and all time.
4. Cache each board for at least five minutes on the server. Rankings move slowly.

## 4. Prove it works

Set `BASE=https://developers.arena54.com/api/v1` and `ARENA54_API_KEY` to my test key. If I have not given you a key, ask me for a TEST key once, and for nothing else.

- Run: `curl "$BASE/rankings/games/week" -H "Authorization: Bearer $ARENA54_API_KEY"`. Expect HTTP 200, `"success": true` and ranked rows of sample games.
- Run: `curl "$BASE/rankings/games/yesterday" -H "Authorization: Bearer $ARENA54_API_KEY"`. Expect HTTP 404 and `"code": "not_found"`.
- Open the page. It shows the board, and switching changes the heading and the call made. With a test key the three periods return the same sample rows; they differ with a live key.

Show me the output of each check.

## 5. Rules you must not break

- The key never reaches a browser, a game build or a commit.
- On 401, show that the key is wrong or missing. Do not retry automatically.
- On 403 `plan_required`, show the message; it names the plan that includes the call. Do not retry.
- On 429, wait for `Retry-After` seconds before the next call, and never retry in a tight loop.
- On 429 or 503, show a “try again later” line and keep showing the last good answer, even past its cache time.
- Show only what the API returned. Do not make up games, people or ranks.

## 6. Done when

- The key is read from the environment on the server and appears in no client bundle and no commit.
- All six boards can be shown.
- A refusal shows a plain message.
- The two curl checks give the answers above.

Then give me a short list of the files you changed and anything I must do by hand.

For a person, not needed for this task: https://developers.arena54.com/docs/reference · This prompt: https://developers.arena54.com/prompts/rankings.server.md
