# Add Arena54 items to this project

Goal: Give players of this game items they own, granted by our server, and honour what they own in another Arena54 game.

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 server code that decides a player has earned something, and the page code that shows what a player has.
- Find how the project reads configuration and environment variables, and which files git ignores.
- Any backend or script will do. Use the HTTP client and the router the project already has, or the runtime’s own if it has none. If the project has nowhere to run code off the page, stop and tell me.
- If the project has no environment example file, add one, and load the environment the way the runtime provides.
- If the project already has an Arena54 client or key variable, reuse it.
- 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`. Log `meta.request_id` with any failure.

Keys and headers:

- Use the game’s SECRET key (it starts `a54_test_` or `a54_live_`, with no `pub_`). Read it from the environment variable `ARENA54_SECRET_KEY`, on the server only. Never send it to a page and never commit it. A secret key that arrives from a browser is refused.
- Send `Authorization: Bearer <secret key>`. A body is JSON; a `Content-Type` header is optional.
- A test key writes to test data of its own that never touches live data. Build with the test key.
- A 429 carries `Retry-After` in seconds.

- The page uses the game’s PUBLISHABLE key (`a54_pub_…`) and the player’s token as `X-Arena54-Player`, and can only read.

Calls:

- POST /play/test-token: A sample player. Test keys only.
    data: { "token": "a54p_…", "player": { "id": "pl_test_player", "name": "Test Player" } }
- GET /play/me: Who the token is: id, name, when it expires.
    data: { "id": "pl_…", "name": "Ada", "game": { "slug": "…", "name": "…" }, "expires_at": "2026-10-12T21:00:00.000Z" }
- DELETE /play/me: The player erases themselves from this game: saves, scores, achievements, items and the connection.
    data: { "player": "pl_…", "removed": { "saves": 3, "scores": 1, "achievements": 2, "items": 0, "connection": 1, "ledger_accounts_unlinked": 0 } }   (the player's token as X-Arena54-Player; erases them from this game, so sign them out after)
- POST /play/checkout: Start the purchase of an item at its own price. Answers the address of Arena54’s checkout page. Test keys only for now.
    data: { "sale": "uuid", "url": "https://arena54.com/play/checkout/…", "item": "gold-helmet", "price": 500, "currency": "NGN", "test": true }   (send { "item": "gold-helmet", "redirect": "https://your-game/after" })
- GET /play/checkout/{id}: Whether one purchase or tip by this player is open, paid, cancelled or refunded.
    data: { "sale": "uuid", "item": "gold-helmet", "price": 500, "status": "open" | "paid" | "cancelled" | "refunded", "test": true }
- POST /play/tips: Start a tip from the player to your game, of an amount they choose. Answers the address of the checkout page.
    data: { "sale": "uuid", "url": "https://…", "item": "tip", "price": 500, "currency": "NGN", "test": true }   (send { "amount": 500, "redirect": "https://your-game/after" }; confirm with GET /play/checkout/{id})
- GET /play/items: The game’s items, and which the player owns.
    data: [{ "slug": "gold-helmet", "name": "Gold helmet", "description": "…", "owned": true, "granted_at": "…" | null, "look": { "picture": "https://…", "model": "https://…glb" | null, "kind": "vehicle" | null, "colour": "yellow" | null, "rarity": "rare" | null } | null }]
- GET /play/entitlements: ?game={slug}: what this player owns in another game, if they chose to share it.
    data: [{ "game": "other-game-slug", "item": "gold-helmet", "name": "Gold helmet", "description": "…", "granted_at": "…", "look": { "picture": "https://…", "model": "https://…glb" | null, "kind": "vehicle" | null, "colour": "yellow" | null, "rarity": "rare" | null } | null }]   (empty when nothing is shared)
- PUT /play/players/{id}/items/{item}: Grant an item to a player. Twice is the same as once.
    data: { "player": "pl_…", "item": "gold-helmet", "new": true }   (no request body)
- DELETE /play/players/{id}/items/{item}: Take an item back.
    data: { "player": "pl_…", "item": "gold-helmet", "removed": true }
- GET /play/players/{id}/items: What one player owns.
    data: [{ "slug": "gold-helmet", "name": "Gold helmet", "description": "…", "granted_at": "…", "look": { "picture": "https://…", "model": "https://…glb" | null, "kind": "vehicle" | null, "colour": "yellow" | null, "rarity": "rare" | null } | null }]
- POST /play/players/{id}/items/{item}/transfer: Move an item the player owns to another game, along a route both makers agreed. It leaves this game.
    data: { "transfer": "uuid", "player": "pl_…", "item": "yellow-bus", "to": "other-game-slug", "moved": true, "replay": false }   (send { "to": "other-game-slug", "ref": "your-own-reference" }; a test key answers "moved": false, "test": true)

How it works:

- Items are listed for the game in the Arena54 dashboard, one per line: id | name | description. `{item}` in a path is that id.
- `{id}` in a path is the player’s id, as GET /play/me returns it to the page.
- Only the server grants or takes back an item. A page can never write one.
- The server may call GET /play/me too, with the SECRET key and the player’s token as `X-Arena54-Player`. That is how it confirms who a player is. With a TEST secret key, POST /play/test-token returns a sample player’s token to test with.
- Arena54 does not check that a player id exists: a grant to any id succeeds. That is why the server must confirm the player first.
- Taking back an item the player does not own answers 200 with `removed: false`.
- GET /play/entitlements?game={slug} is asked by this game about ANOTHER game. It answers what the same person owns there, only if they chose to share it. An empty list means nothing to honour, for any reason. Do not read more into it.

Refusals:

- 401 missing_key: No Authorization header, or what follows “Bearer” is not shaped like a game’s key.
- 401 invalid_key: The key is shaped like one but is unknown or revoked, or its game is gone.
- 403 origin_not_allowed: The page’s address is not one the game registered.
- 403 module_not_enabled: The module is not ticked for this game in the dashboard.
- 503 module_off: Arena54 has switched the module off for now.
- 401 invalid_player: No player token, an expired or altered one, or one for another game or mode.
- 401 player_revoked: The player disconnected the game. Send them to sign in again.
- 403 secret_key_required: The call needs the game’s secret key, from a server.
- 404 not_found: No such save, board, achievement, item or address.
- 429 rate_limited: Too many calls this minute, for the key or for one player. Retry-After says how long to wait.

## 3. Do this, in order

1. Add ARENA54_SECRET_KEY to the project’s environment example file with an empty value. Read it only in server code.
2. Write a server function `grantItem(playerId, item)` that calls PUT /play/players/{id}/items/{item}, and `revokeItem` that calls DELETE on the same address. Call `grantItem` at the one place the server decides the player earned it.
3. The page must tell the server who the player is. Have the page send its player token to your server, and have the server confirm it by calling GET /play/me with that token before granting anything. Never take a player id from the page on trust.
4. On the page, show what the player owns with GET /play/items (publishable key and player token): each item has `owned`.
5. If I name another game whose items we honour, call GET /play/entitlements?game=<its slug> from the page and unlock our own matching thing for each item returned. What we give for it is our own rule.
6. Each item may carry `look`. It is null unless the item’s maker attached a picture and Arena54 approved it. When it is there, show `look.picture` for the item, and load `look.model` (a .glb file) only if this project already draws 3D. When it is null, draw our own stand-in. Never treat a look as proof of owning anything: owning is what the list itself says.
7. Only if I ask for an item to move to another game: write a server function `transferItem(playerId, item, toGame, ref)` that calls POST /play/players/{id}/items/{item}/transfer with `{ to, ref }`, where `ref` is our own id for this move (an order id), so a retry is the same move. After `moved: true` the player no longer owns the item here: remove whatever we gave them for it. On 409 `cannot_transfer` tell the player to connect the other game and turn on sharing for this one at arena54.com/play/connections. On 409 `no_route` stop and tell me that both makers must agree the route in the Arena54 dashboard.
8. Only if I ask for an item to be sold: give the item a price in the Arena54 dashboard (id | name | description | price). On the page, call POST /play/checkout with `{ item, redirect }` and send the player to the `url` in the answer. When they come back, the address ends with `#a54_sale=<id>&status=paid` or `cancelled`: confirm it with GET /play/checkout/{id} before showing the item, and never trust the hash alone. Never send a price: Arena54 uses its own. With a live key the purchase is real. If a live key answers 503 `payments_not_open`, real payments are closed at the moment: say so to me and keep the test flow working.
9. If the list of items is empty, stop and tell me to add items for the game in the Arena54 dashboard.
10. If the project has no page, skip the two page steps and say so.

## 4. Prove it works

Set `BASE` and `ARENA54_SECRET_KEY` (my TEST secret key) in the shell. If I have not given you a key, ask me for a TEST secret key once, and for nothing else. Use the sample player id `pl_test_player`. `BASE=https://developers.arena54.com/api/v1`.

- Run: `curl -X PUT "$BASE/play/players/pl_test_player/items/<an item id>" -H "Authorization: Bearer $ARENA54_SECRET_KEY"`. Expect HTTP 200 and `"new": true` the first time, `false` after.
- Run: `curl "$BASE/play/players/pl_test_player/items" -H "Authorization: Bearer $ARENA54_SECRET_KEY"`. Expect the item in the list.
- Run the PUT with an item id that does not exist. Expect HTTP 404 `not_found`.
- Get a sample token: `curl -X POST "$BASE/play/test-token" -H "Authorization: Bearer $ARENA54_SECRET_KEY"`. Call the project’s own grant route with that token and with a made-up one. Expect the first to grant and the second to be refused by our server.
- A 503 `module_off` means Arena54 has not opened items yet, and a 403 `module_not_enabled` means I must tick “Items and entitlements” for the game. Tell me which.

Show me the output of each check.

## 5. Rules you must not break

- Only the server grants an item, and only after it has confirmed who the player is. A page’s word is a claim.
- The secret key never reaches a page, a game build or a commit.
- Do not treat an empty entitlements list as meaning anything about the player.
- Do not sell or price an item through this.
- On 429, wait for `Retry-After` seconds. Never retry in a tight loop.

## 6. Done when

- The server grants and revokes an item, and the page shows what the player owns.
- The server confirms the player with /play/me before granting.
- The secret key is read from the environment and is in no client code and no commit.
- The 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 · This prompt: https://developers.arena54.com/prompts/items.server.md
