> **Read-only since 2026-10-26.** This inventory moved into Shop View on 2026-10-26 and is read-only here: nothing can be added, changed, claimed or requested. Agents working with the owner: use Shop View's shop and inventory tools instead. Anything written here would not carry over.

# Shop inventory — agent access guide

Base URL: `https://shop.whydidimakethat.com`  ·  Machine-readable: [`/openapi.json`](https://shop.whydidimakethat.com/openapi.json)  ·  Index: [`/llms.txt`](https://shop.whydidimakethat.com/llms.txt)

This is a small private equipment/inventory service (cameras, microcontrollers, tools, kit parts). Agents can **look up gear** (`read`) and optionally **reserve it** (`claim`). **You do not need any existing token to request access**: the request endpoint is public. A human owner approves each request in the web UI.

**Scopes:** ask for `read` for equipment lookup. Add `claim` **only** if you will reserve/release gear. Add `suggest` (owner approves each change) or `edit` (saved at once, owner can undo) **only** if you will add or correct items. Ask for the minimum.

## TL;DR — 3 HTTP calls from the homepage

```bash
BASE=https://shop.whydidimakethat.com
curl -sS "$BASE/"                 # 1. homepage: "AI agents: request access" panel -> /agents.md
curl -sS "$BASE/agents.md"        # 2. this document
curl -sS -X POST "$BASE/agent/request" \
  -H 'content-type: application/json' \
  -H 'Idempotency-Key: my-agent-2026-10-01-kit-lookup' \
  -d '{"a":"my-agent","p":"equipment lookup","s":["read"]}'   # 3. submit request
# -> {"id":"s_...","st":"pending","pickup":"pk_...","poll":"/agent/pickup/s_..."}
```

Python (stdlib only, no special headers needed):

```python
import json, urllib.request
BASE = "https://shop.whydidimakethat.com"
req = urllib.request.Request(
    BASE + "/agent/request",
    data=json.dumps({"a": "my-agent", "p": "equipment lookup", "s": ["read"]}).encode(),
    headers={"content-type": "application/json", "Idempotency-Key": "my-agent-2026-10-01-kit-lookup"},
    method="POST",
)
r = json.load(urllib.request.urlopen(req))
print(r)   # save r["id"] and r["pickup"] privately
```

## Full flow

1. **Request** — `POST /agent/request` (public, no auth). Response contains `id`, `st:"pending"` and **`pickup`**.
2. **Pending** — the request sits in `pending` until the owner acts. Pending requests do **not** expire on their own and there is no push/callback (`callback` fields are ignored). You poll.
3. **Owner approval** — the owner opens `https://shop.whydidimakethat.com`, signs in with their master token, opens the **Agents** tab and clicks **Approve** (they may change the scopes) or **Deny**. Approve creates a session valid for **365 days by default** (owner may choose 1–3650 days). Tell your user/owner that a request named `<your a>` is waiting; do not ask them to paste any token to you.
4. **Token pickup** — poll `GET /agent/pickup/:id` with your `pickup` secret until `st` is `active`. The first successful poll returns the session `token` **exactly once**.
5. **Inventory reads/claims** — call the compact `/a/*` endpoints with the session token.

### `pickup` is a secret — save it privately, it is shown once

* The `pickup` value is returned **only once**, in the response to the *first* `POST /agent/request`. The server stores only its SHA-256 hash and **cannot show it again**.
* Save it immediately in a private place (env var, secret store, 0600 file). **Do not paste it into chat, logs, tickets or URLs.**
* Anyone holding `pickup` + `id` can claim the session token, so treat it like a password.
* The session `token` (`ag_...`) is **delivered once**: the first `GET /agent/pickup/:id` after approval returns it; every later call returns `{"st":"active","delivered":1}` with no token. Store it privately as soon as you get it. If you lose it, ask the owner to revoke the session and submit a new request.

## 1. Request access

`POST /agent/request`   (no `Authorization` header needed; `Content-Type: application/json`)

| Field | Meaning |
|---|---|
| `a` (or `agent_name`) | **Required.** Your agent name, max 80 chars |
| `p` (or `purpose`) | Short human-readable purpose shown to the owner, max 200 chars |
| `s` (or `scopes`) | Array (or comma string) from `read`, `claim`, `suggest`, `edit`. Default `["read"]`. Unknown scopes are dropped |
| `key` (or header `Idempotency-Key`) | Optional idempotency key, max 200 chars (see below) |

```bash
curl -sS -X POST https://shop.whydidimakethat.com/agent/request \
  -H 'content-type: application/json' \
  -d '{"a":"my-agent","p":"equipment lookup","s":["read"]}'
# {"id":"s_0123456789abcdef01234567","st":"pending","pickup":"pk_...","poll":"/agent/pickup/s_0123456789abcdef01234567"}
```

Save `id` and `pickup`.

### Idempotent retries (no duplicate requests)

Network flaked? Retrying is safe — you will not spam the owner's Agents tab.

* While a matching request is `pending` (or `active` but its token has not yet been picked up) a repeat `POST` returns the **existing** request: `{"id":"s_…","st":"pending","existing":1,"poll":"/agent/pickup/s_…","note":"…"}`.
* "Matching" means: the same `Idempotency-Key` header / `key` field if you send one; otherwise the same agent name + purpose + scopes.
* **`pickup` is NOT repeated on a duplicate** (`existing:1`). It was only in the first response and is stored hashed, so the server cannot re-issue it, and re-issuing it to an unauthenticated caller would let anyone who guesses your name/purpose steal your token. Keep using the `pickup` from your first response with the returned `id`.
* If you lost the first response (and so the pickup): send a **new** `Idempotency-Key` to create a fresh request; ask the owner to deny/ignore the old one.
* Once a request is denied, revoked, or its token has been picked up, the same key/fingerprint creates a new request.
* Recommended: always send a stable `Idempotency-Key` (e.g. `<agent>-<task>-<date>`).

## 2. Check approval / pick up the token

`GET /agent/pickup/:id` with **one of**: `X-Pickup: <pickup>` or `Authorization: Bearer <pickup>`.

```bash
curl -sS https://shop.whydidimakethat.com/agent/pickup/$ID -H "X-Pickup: $PICKUP"
```

| Response | Meaning |
|---|---|
| `{"st":"pending"}` | Owner has not decided. Poll every 30–60 s (back off to a few minutes); may take hours/days |
| `{"st":"active","token":"ag_…","s":["read"]}` | **Approved. Token delivered — shown once, save it now.** `s` = granted scopes |
| `{"st":"active","delivered":1}` | Token was already picked up earlier; not returned again |
| `{"st":"denied"}` | Owner denied. Don't retry without asking |
| `{"st":"revoked"}` | Session revoked or expired (default 365 days). Submit a new request |
| `401 {"err":"invalid_pickup"…}` / `{"err":"missing_pickup"…}` | Wrong or missing pickup secret |
| `404 {"err":"nf"}` | Unknown request id |

## 3. Use the token

Send `Authorization: Bearer <token>` (or `X-Agent-Token: <token>`).

```bash
TOKEN=ag_...
curl -sS "https://shop.whydidimakethat.com/a/items?q=esp32&l=20" -H "Authorization: Bearer $TOKEN"
curl -sS "https://shop.whydidimakethat.com/a/items/ITEM_ID"      -H "X-Agent-Token: $TOKEN"
```

| Endpoint | Scope | Notes |
|---|---|---|
| `GET /a/items?q=&c=&l=` | `read` | Search name/brand/model/notes/id (`q`), category (`c`, exact), limit (`l`, default 50, max 200) → `{"i":[…],"n":count}` |
| `GET /a/items/:id` | `read` | One item + held claims `cl` |
| `GET /a/claims?p=` | `read` or `claim` | Your held claims (optionally for project `p`) |
| `POST /a/claim` `{"id","q","p"}` | `claim` | Reserve `q` units (default 1) for project label `p` (≤80 chars). Does **not** change owned quantity |
| `POST /a/release` `{"cid"}` or `{"id","p"}` | `claim` | Release a claim you hold (by claim id, or your latest held claim for item `id` [+ project `p`]) |

```bash
curl -sS -X POST https://shop.whydidimakethat.com/a/claim -H "Authorization: Bearer $TOKEN" -H 'content-type: application/json' \
  -d '{"id":"ITEM_ID","q":1,"p":"kit-box-v2"}'      # {"ok":1,"cid":"c_…","aq":3}  |  {"err":"no_stock","aq":0}
curl -sS -X POST https://shop.whydidimakethat.com/a/release -H "Authorization: Bearer $TOKEN" -H 'content-type: application/json' \
  -d '{"cid":"c_…"}'                                # {"ok":1,"aq":4}
```

Stock model: `q` = owned quantity, `aq` = **available** = owned − sum of held claims. Claim fails with `409 no_stock` if `q > aq`. Sessions can release only their own claims.

### Compact key map (responses are minified JSON)

| Key | Meaning |
|---|---|
| `id` | item id (also session/request id in `/agent/*`) |
| `n` | name (in lists: count of results at top level) |
| `b` / `m` / `c` | brand / model / category |
| `q` | owned quantity (in claims: claimed qty; in requests to claim: qty wanted) |
| `aq` | available quantity (owned − held) |
| `loc` | location |
| `h` | total held quantity (item detail) |
| `cl` | held claims `[{cid,q,p,a}]` |
| `cid` | claim id |
| `p` | project label (in `/agent/request`: purpose) |
| `a` | agent name |
| `t` | claim created-at timestamp |
| `i` | result array |
| `ok` | `1` on success |
| `err` | short error code |
| `st` | status: `pending` · `active` · `denied` · `revoked` |
| `s` | scopes |
| `pickup` | pickup secret (first request response only) |
| `delivered` | `1` = token already picked up |
| `existing` | `1` = duplicate request; existing one returned |

Optional keys (`b`, `m`, `c`, `loc`) are omitted when empty.

## 4. Add or correct items (`suggest` / `edit`)

With `edit` your change is saved at once; with `suggest` it waits in the owner's **Agents → Agent changes** list until they approve or reject it. Either way every change is logged with before/after values and the owner can undo it. Send `"review": true` to queue a change for approval even when you hold `edit`.

| Endpoint | Scope | Notes |
|---|---|---|
| `POST /a/items` | `edit` or `suggest` | Add an item. `n` required; optional `id` (default: slug of brand + model, else of the name), `b`, `m`, `c`, `q` (default 1), `u` (default `ea`), `loc`, `cond`, `notes`, and `note` (why — shown to the owner). → `201 {"ok":1,"ch":"ch_…","st":"applied","id":…}` or `202 … "st":"pending"` |
| `PATCH /a/items/:id` | `edit` or `suggest` | Change only the fields you send (same keys; long names like `name`, `quantity`, `location` also work). → `{"ok":1,"ch":…,"st":"applied"/"pending"}` or `{"ok":1,"st":"unchanged"}` |
| `GET /a/changes?st=` | `read`, `suggest` or `edit` | Your changes and their status: `pending`, `applied`, `rejected`, `undone` |

```bash
curl -sS -X POST https://shop.whydidimakethat.com/a/items -H "Authorization: Bearer $TOKEN" -H 'content-type: application/json' \
  -H 'Idempotency-Key: my-agent-2026-10-21-amana-46350' \
  -d '{"n":"Amana Tool 46350 Mortise Compression Spiral 1/4 D x 1 CH","b":"Amana Tool","m":"46350","c":"consumable","q":1,"note":"from a shelf photo"}'
curl -sS -X PATCH https://shop.whydidimakethat.com/a/items/ITEM_ID -H "Authorization: Bearer $TOKEN" -H 'content-type: application/json' \
  -d '{"q":3,"loc":"CNC drawer 2"}'
```

**Before adding, search** (`GET /a/items?q=…`) — the item may already be listed, or be part of a kit. The server refuses an add whose brand + model is already listed (`409 {"err":"duplicate","id":…}`): PATCH that item's quantity instead, or resend with `"distinct": true` if it really is a different item. Other refusals: `409 exists` (id taken), `409 pending` (the same add is already waiting for the owner), `409 below_held` (quantity below what claims hold). Agents can't delete items. Use an `Idempotency-Key` header (or `key`) so a retried write is not applied twice.

## Errors

| HTTP | Body | Meaning / fix |
|---|---|---|
| 400 | `{"err":"bad_json"…}` | Body is not a JSON object |
| 400 | `{"err":"a"…}` | Missing agent name (`a`) on `/agent/request` |
| 400 | `{"err":"id"}` / `{"err":"q"}` / `{"err":"cid"}` | Missing/invalid field on claim/release |
| 401 | `{"error":"authentication_required","agent_docs":"/agents.md","request_access":{"method":"POST","path":"/agent/request","authentication_required":false}}` | No/invalid/expired/revoked token on a protected route (admin/human routes). `/a/*` returns the compact form `{"err":"authentication_required","agent_docs":…,"request_access":…}`. Fix: request access (this guide) or re-request if revoked/expired. Response carries `WWW-Authenticate: Bearer` |
| 401 | `{"err":"invalid_pickup"}` / `{"err":"missing_pickup"}` | Wrong/missing pickup secret |
| 403 | `{"err":"insufficient_scope","need":"claim"}` | Valid session but scope not granted. Ask the owner for a new request with that scope |
| 403 | `{"err":"forbidden"}` | Releasing a claim held by another session |
| 404 | `{"err":"nf"}` | Item / claim / request not found |
| 409 | `{"err":"no_stock","aq":N}` | Not enough available stock |
| 409 | `{"err":"already"}` | Claim already released |
| 409 | `{"err":"retry"}` | Rare race on `/agent/request`; just retry |
| 409 | `{"err":"duplicate"}` / `exists` / `pending` / `below_held` | Item add/edit refused; see section 4 |
| 500 | `{"err":"server_error","m":…}` | Server error |

Agent endpoints send `Access-Control-Allow-Origin: *`. The server never rejects a request for its `User-Agent` (missing or unusual UA is fine).

### Cloudflare error 1010

If you receive a plain-text `error code: 1010` (HTTP 403, not JSON) you were stopped by Cloudflare's edge Browser Integrity Check before reaching this service — typically default `Python-urllib/x.y` or similar library UAs. Workaround: send any explicit, descriptive `User-Agent`, e.g. `User-Agent: my-agent/1.0 (+contact)`.

## Admin (owner only — for reference)

Owners approve in the UI (Agents tab). Equivalent API (master token): `GET /admin/sessions?st=pending`, `POST /admin/sessions/:id/approve` `{"s":["read"],"days":365}`, `…/deny`, `…/revoke`, `GET /admin/claims`, `GET /admin/changes?st=pending`, `POST /admin/changes/:id/approve|reject|undo`. Agents never receive or need the master token.
