feat(api): API v1 writes, token grants, live events, OpenAPI, API keys settings #126

Open
jfro wants to merge 7 commits from feat/123-api-writes into main
Owner

Closes #123. Stacked on #125 (#122 read endpoints): this branch starts from feat/122-api-read, so until #125 merges the diff here includes its commits too. Merge #125 first; this PR's own commits are the five after a93579e.

Built as docs/api.md's decided choices A to H:

  • Grants (Choice A): api_keys.grants, a list, default [] and never null, so every existing key gains nothing. suggestions, quick_notes, todos, sync. A grant that changes things also needs can_write. ApiKeys.allowed?/3 is the one check, shared with MCP; /me reports grants.
  • Writes (Choice B): POST /documents/:id/pages (typed or drawn), PATCH /documents/:id (move), POST /notebooks, /folders, /reports, /pdfs (multipart upload or JSON, Choice F), /reviews. Each needs its MCP write tool. A read-only key gets 403 read_only. Idempotency-Key makes retries safe (24 hours).
  • App features (Choice C): suggestion approve, reject, undo, retry and tag approve or dismiss; quick notes; todo pages and sync now; start a sync. Each item is checked against a folder-limited key's reach (404 outside it).
  • Live (Choice D): GET /events, a server-sent events stream of hints (ready, changes, suggestions, todos, sync), with a keepalive that re-checks the key. There is a new transcribed change kind, and mcp.md says to ignore unknown kinds.
  • OpenAPI (Choice E): hand-written priv/static/openapi.json, served publicly at /api/v1/openapi.json. A test checks that it lists every route and nothing else.
  • Settings (Choice H): the page is now "API keys", with MCP and REST sections, App features checkboxes (all off), and a new key's token shown as a QR code. The QR encoder is hand-written (RmAnalyzer.QRCode), with no new dependency, and checked against segno and zbarimg.
  • Docs: docs/api.md has a quick start with curl, and where the build differs from the design. There's also a README pointer.

Tests: write_test (30), app_test (29), api_keys_grants_test (14), events_test (5), openapi_test (3), qr_code_test (10), changes_transcribed_test (2), settings tests. mix precommit: 1770 passed.

Closes #123. Stacked on #125 (#122 read endpoints): this branch starts from `feat/122-api-read`, so until #125 merges the diff here includes its commits too. Merge #125 first; this PR's own commits are the five after a93579e. Built as docs/api.md's decided choices A to H: - **Grants (Choice A):** `api_keys.grants`, a list, default `[]` and never null, so every existing key gains nothing. `suggestions`, `quick_notes`, `todos`, `sync`. A grant that changes things also needs `can_write`. `ApiKeys.allowed?/3` is the one check, shared with MCP; `/me` reports grants. - **Writes (Choice B):** `POST /documents/:id/pages` (typed or drawn), `PATCH /documents/:id` (move), `POST /notebooks`, `/folders`, `/reports`, `/pdfs` (multipart upload or JSON, Choice F), `/reviews`. Each needs its MCP write tool. A read-only key gets `403 read_only`. `Idempotency-Key` makes retries safe (24 hours). - **App features (Choice C):** suggestion approve, reject, undo, retry and tag approve or dismiss; quick notes; todo pages and sync now; start a sync. Each item is checked against a folder-limited key's reach (404 outside it). - **Live (Choice D):** `GET /events`, a server-sent events stream of hints (`ready`, `changes`, `suggestions`, `todos`, `sync`), with a keepalive that re-checks the key. There is a new `transcribed` change kind, and mcp.md says to ignore unknown kinds. - **OpenAPI (Choice E):** hand-written `priv/static/openapi.json`, served publicly at `/api/v1/openapi.json`. A test checks that it lists every route and nothing else. - **Settings (Choice H):** the page is now "API keys", with MCP and REST sections, App features checkboxes (all off), and a new key's token shown as a QR code. The QR encoder is hand-written (`RmAnalyzer.QRCode`), with no new dependency, and checked against segno and zbarimg. - **Docs:** docs/api.md has a quick start with curl, and where the build differs from the design. There's also a README pointer. Tests: write_test (30), app_test (29), api_keys_grants_test (14), events_test (5), openapi_test (3), qr_code_test (10), changes_transcribed_test (2), settings tests. `mix precommit`: 1770 passed.
jfro added 11 commits 2026-12-03 05:25:34 +00:00
feat(api): API v1 foundation and read endpoints
All checks were successful
CI / test (pull_request) Successful in 1m57s
fc828f82db
JSON API at /api/v1 for native clients, on the same rma_ API keys and
scope as MCP (#122). ApiKeys.allowed?/2 is now the one permission check
MCP and the API share; every library endpoint needs the MCP tool it is.

Adds the Bearer auth plug (API keys only, not the chat's answer tokens),
the shared error body, cursor paging, ETags, and read endpoints: /me,
/sync, /documents, /documents/:id, its pages and page images, /folders,
/tags, /search, /stats, /suggestions and /changes. Notes gains structured
search and read, sharing ranking with the text tools; AgentTools.reviewable/1
filters suggestions for both MCP and the API.

Sorting, Tagging, Remarkable's tags and the change log don't honour a
key's folder limit, so the API filters per item; tests cover a
folder-limited key (404 or filtered lists) and a tool-limited key (403)
on every endpoint.

docs/api.md is Nye's design draft (#121), noting where #122 differs.

Refs #121
Closes #122
resources/list, read, subscribe and the GET event stream now go through
ApiKeys.allowed?/2, so a key without list_changes can't read the change
feed another way (finding F1 on #122).
test(api): attack checklist for the read endpoints
All checks were successful
CI / test (pull_request) Successful in 1m58s
f1a70cff22
No existence oracle, no 304 past a folder limit, replayed cursors stay
limited, bad limits and page numbers are 4xx, no CORS or cookies, no
writes on read paths, access changes count from the next request.
Every API query parameter is a single string; a plug refuses lists and
maps (?q[]=a) up front with invalid_params (finding F2 on #122).
fix(api): accept the Bearer scheme in any case
All checks were successful
CI / test (pull_request) Successful in 2m2s
e42d1bda8a
RFC 9110 makes the auth scheme case-insensitive. Notes in docs/api.md on
ancestor folder names, /me's name fallback and list-shaped params.
docs(api): record choices A-H as decided (Dec 3)
All checks were successful
CI / test (pull_request) Successful in 2m15s
a93579eff7
Clients following the change feed (REST /changes, MCP list_changes,
webhooks) now learn when a page's text changes. docs/mcp.md tells clients
to ignore change kinds they don't know.

Refs #123
Hand-written so showing an API key as a QR code needs no new dependency.

Refs #123
- grants on API keys (suggestions, quick_notes, todos, sync), empty on
  every existing key; a grant that changes things also needs can_write
- write endpoints for the MCP write tools, PDF uploads as multipart or
  JSON, and Idempotency-Key so retries don't write twice
- suggestions, quick notes, todo pages and sync behind their grants,
  each item checked against a folder-limited key's reach
- GET /events, a server-sent events stream of hints
- a hand-written OpenAPI description at /api/v1/openapi.json, with a test
  that it lists every route

Closes #123
Refs #123
docs(api): quick start, what the build does, README pointer
All checks were successful
CI / test (pull_request) Successful in 2m8s
3a41b2374e
Refs #123
Author
Owner

Posted by Haystack for Nye (its token can't comment).

Nye's review of PR #126 (#123 writes, grants, app features, events) at 3a41b23

Verdict: one small fix (F3, low), then ready. Everything on the focus list holds.

Finding

F3 (low): POST /api/v1/notebooks with a non-string folder_id (5, ["work"]) answers 201 and puts the notebook in My files
instead of 400 (Notebooks.make's string/1 turns it into ""). Folder-limited keys are still refused, so no reach bypass.
Fix: in WriteController.create_notebook, 400 invalid_params when folder_id is present and not a string (same for name).
Test: out/write_attack_nye_test.exs, cases 2 and 3 (mix test <file> --only case:2 --only case:3).

Held (C16)

  • Grants: migration default [] not null; allowed?/3 needs grant name AND key grant; :write also needs can_write.
  • can_write=false: 403 read_only on all 7 writes, drawn pages, and suggestion/quick-note/todo writes.
  • Folder-limited: path docs 404, target folders 422, malformed folder_id can't reach My files; approve_move checks Quick sheets + target and a target override.
  • Wrong-type JSON bodies on every write route: 15 of 17 get a 4xx (the 2 others are F3).
  • Idempotency: per API key, mismatch 422, in-progress 409, 5xx released.
  • Events: list_changes gate, change hints filtered by scope, key/tools/folders re-checked at keepalive.
  • openapi.json static, no data. QR SVG never holds the token text; hostile aria label escaped.
  • Full suite with my tests: 1786/1789, the only failures are F3's two cases.

Nits (no change needed to ship)

  1. Idempotent runs before permit/validation, so a 400/403 is kept and replayed for 24h even after the key is fixed. Consider not keeping 400/401/403/404.
  2. Plug.Parsers multipart length is 52 MB for every route, before auth (was 8 MB). Consider a separate parser for /api/v1/pdfs.
  3. approve_move with target_id "" is 409 conflict; 400 reads better.
  4. A replay returns the stored answer even if the key lost can_write since.
_Posted by Haystack for Nye (its token can't comment)._ # Nye's review of PR #126 (#123 writes, grants, app features, events) at 3a41b23 Verdict: one small fix (F3, low), then ready. Everything on the focus list holds. ## Finding F3 (low): POST /api/v1/notebooks with a non-string folder_id (5, ["work"]) answers 201 and puts the notebook in My files instead of 400 (Notebooks.make's string/1 turns it into ""). Folder-limited keys are still refused, so no reach bypass. Fix: in WriteController.create_notebook, 400 invalid_params when folder_id is present and not a string (same for name). Test: out/write_attack_nye_test.exs, cases 2 and 3 (`mix test <file> --only case:2 --only case:3`). ## Held (C16) - Grants: migration default [] not null; allowed?/3 needs grant name AND key grant; :write also needs can_write. - can_write=false: 403 read_only on all 7 writes, drawn pages, and suggestion/quick-note/todo writes. - Folder-limited: path docs 404, target folders 422, malformed folder_id can't reach My files; approve_move checks Quick sheets + target and a target override. - Wrong-type JSON bodies on every write route: 15 of 17 get a 4xx (the 2 others are F3). - Idempotency: per API key, mismatch 422, in-progress 409, 5xx released. - Events: list_changes gate, change hints filtered by scope, key/tools/folders re-checked at keepalive. - openapi.json static, no data. QR SVG never holds the token text; hostile aria label escaped. - Full suite with my tests: 1786/1789, the only failures are F3's two cases. ## Nits (no change needed to ship) 1. Idempotent runs before permit/validation, so a 400/403 is kept and replayed for 24h even after the key is fixed. Consider not keeping 400/401/403/404. 2. Plug.Parsers multipart length is 52 MB for every route, before auth (was 8 MB). Consider a separate parser for /api/v1/pdfs. 3. approve_move with target_id "" is 409 conflict; 400 reads better. 4. A replay returns the stored answer even if the key lost can_write since.
fix(api): 400 for a non-string folder_id on POST /notebooks
All checks were successful
CI / test (pull_request) Successful in 2m6s
d737848ab0
fix(api): 400 for a non-string name on POST /notebooks
All checks were successful
CI / test (pull_request) Successful in 2m10s
ec859da3a4
Author
Owner

Posted by Haystack for Nye (its token can't comment).

Nye's re-review at d737848: Re-checked at d737848. F3 is fixed: every non-string folder_id (false, true, 0, 1.5, [], lists, maps) gets a 400 naming folder_id; null or missing still files in My files for full-access keys; folder-limited keys are still refused outside their folders; read-only keys get 403 and no key gets 401 before the type check. All 25 of my attack tests pass. Full precommit passes on the head (1796 tests) and on main merged with it (1797). CI green. Approve from my side; the ship report is with Jeremy.

Haystack's note: the head has since moved one commit to ec859da (fix(api): 400 for a non-string name on POST /notebooks): a two-line change in write_controller.ex that runs the same optional_string check on name, plus one test. I read the diff; CI is green on ec859da and the PR is mergeable. Nye has the re-check of that commit from Upper Wolfjaw.

_Posted by Haystack for Nye (its token can't comment)._ **Nye's re-review at d737848:** Re-checked at d737848. F3 is fixed: every non-string folder_id (false, true, 0, 1.5, [], lists, maps) gets a 400 naming folder_id; null or missing still files in My files for full-access keys; folder-limited keys are still refused outside their folders; read-only keys get 403 and no key gets 401 before the type check. All 25 of my attack tests pass. Full precommit passes on the head (1796 tests) and on main merged with it (1797). CI green. Approve from my side; the ship report is with Jeremy. _Haystack's note:_ the head has since moved one commit to ec859da (`fix(api): 400 for a non-string name on POST /notebooks`): a two-line change in `write_controller.ex` that runs the same `optional_string` check on `name`, plus one test. I read the diff; CI is green on ec859da and the PR is mergeable. Nye has the re-check of that commit from Upper Wolfjaw.
Author
Owner

Posted by Haystack for Nye (its token can't comment).

Nye's re-check at ec859da: clean.

The one new commit makes a name that isn't a string get a 400 instead of a 500. Checked:

  • F3 fix still holds (non-string folder_id → 400; null still works; folder-limited keys still refused).
  • All 25 of my attack tests and the API suite pass (127 tests).
  • mix precommit passes on main merged with this PR (1798 tests, no conflicts). CI green at ec859da.

No open findings. Ready to merge on Jeremy's ship it.

_Posted by Haystack for Nye (its token can't comment)._ **Nye's re-check at ec859da: clean.** The one new commit makes a `name` that isn't a string get a 400 instead of a 500. Checked: - F3 fix still holds (non-string `folder_id` → 400; `null` still works; folder-limited keys still refused). - All 25 of my attack tests and the API suite pass (127 tests). - `mix precommit` passes on main merged with this PR (1798 tests, no conflicts). CI green at ec859da. No open findings. Ready to merge on Jeremy's ship it.
All checks were successful
CI / test (pull_request) Successful in 2m10s
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'feat/123-api-writes:feat/123-api-writes'
git switch 'feat/123-api-writes'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'feat/123-api-writes'
git switch 'feat/123-api-writes'
git rebase 'main'
git switch 'main'
git merge --ff-only 'feat/123-api-writes'
git switch 'feat/123-api-writes'
git rebase 'main'
git switch 'main'
git merge --no-ff 'feat/123-api-writes'
git switch 'main'
git merge --squash 'feat/123-api-writes'
git switch 'main'
git merge --ff-only 'feat/123-api-writes'
git switch 'main'
git merge 'feat/123-api-writes'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
jfro/rm-analyzer!126
No description provided.