6.7 KiB · text History 06ebb60

recourse mobile API

The JSON API the Android app (android/, plan in docs/android.md) talks to. It follows the family’s conventions (pin_flip’s docs/api.md, party-slut’s /api/v1), and the server half is rosin_hunter_3000’s shape: a /api/v1 nest with its own bearer-only middleware.

Signing in

Each device gets its own token. Only its SHA-256 is stored; the token itself is shown once.

  1. On the web, open Devices (the account menu) → Pair a device. The page shows a QR of recourse://pair?base=<server>&token=<token>, and the two fields as text. base is PUBLIC_URL when set, otherwise the origin the browser is using (X-Forwarded-Proto/-Host, then Host).
  2. The app scans it (or the phone opens the link, or the fields are typed), calls GET /api/v1/me, and keeps the token only if that answers 200.

Send it as Authorization: Bearer <token>. The session cookie is not accepted here: the API has no CSRF protection of its own.

A token stays valid for 90 days after it was last used; use is recorded at most once a day, and each recording slides the expiry. Tokens minted before expiries existed keep none. A deactivated account’s tokens stop working. DELETE /api/v1/sessions/current unpairs the calling device; the Devices page and DELETE /api/v1/me/devices/{id} revoke any. A revoked token’s next request is a 401.

The older GET /api/profile (cookie or bearer) is unchanged.

Conventions

  • Format: snake_case keys, UUIDv7 ids, ISO-8601 UTC timestamps.
  • Permissions: may_write, may_create_tags, can_reply, can_edit_tags, can_post, can_edit are computed by the server; the app shows or hides controls from them and holds no rules of its own. The server checks every write regardless.
  • Visibility: exactly the web’s. A category is visible when it and every ancestor admit the user’s trust level (or they’re staff). Anything you can’t see is a 404, whether or not it exists — topics, posts, tags used only in hidden categories, and writes aimed at any of them. Lists (search, tags, bookmarks, notifications, unread count) leave hidden things out.
  • Email verification: when the site requires it, an unverified account reads everything it may see, and every write answers 403 unverified. POST /me/verification sends a new link.
  • Reading a topic counts a view (at most once per ten minutes per user) and marks it read up to its last post, as on the web.
  • Toggles take the desired state ({"bookmarked": true}), so a retried request lands on the same value.
  • Images: avatar_url is server-relative (/u/{id}/avatar), public, and is either the upload or a generated SVG.
  • Post bodies: cooked is the server’s sanitized HTML of the markdown — the web’s own rendering.
  • Errors always use this shape: {"error": {"code": "...", "message": "...", "details": {...}|null}}
Status Codes
400 bad_request (unreadable JSON or a malformed id; details.reason)
401 unauthorized
403 unverified, closed (topic closed to replies), not_author (tags)
404 not_found
410 removed (a deleted topic, for non-staff)
422 invalid (the message says what)

Endpoints

All paths start with /api/v1, and all need a token.

Me and devices

Method Path Notes
GET /me {user, verification_required, min_trust_to_create_tag}. user has preferred_view (classic/focus), preferred_sort, may_write, email_verified, trust_level, trust_name, …
PATCH /me {layout?, sort?, reaction_notifications?} → as GET /me. The web’s profile reads the same fields.
PUT /me/avatar multipart, part avatar (PNG/JPEG/GIF/WebP, ≤ 5 MB) → as GET /me
DELETE /me/avatar back to the letter avatar → as GET /me
POST /me/verification {result: sent | recent | verified | failed}; rate-limited as on the web
GET /me/devices {devices: [{id, label, created_at, last_used_at, expires_at, current}]}
DELETE /me/devices/{id} 204
DELETE /sessions/current 204; this device’s token is revoked

Browsing

Method Path Notes
GET /categories {categories: [{id, slug, name, colors, description, parent_id, depth, topic_count, watching}]} in tree order
GET /categories/{slug}?sort= {category, ancestors, subcategories, sort, topics, can_post}. topics covers the category and its visible subcategories, pinned first. A given sort is saved as the account’s, as on the web.
PUT /categories/{slug}/watch {watching} → {watching}
POST /categories/{slug}/topics {title, body, tags?: [..]} → 201, as GET /topics/{id}
GET /topics/{id} {topic, posts, can_reply, can_edit_tags, watching}
POST /topics/{id}/posts {body} (markdown) → 201, a post
PUT /topics/{id}/tags {tags: [..]} → {tags}. Replaces; up to five; new names need may_create_tags and are otherwise dropped.
PUT /posts/{id}/reaction {key} or {key: null} → {reactions}
PUT /posts/{id}/bookmark {bookmarked} → {bookmarked}

A topic summary (in every list) is {id, title, category, author, is_pinned, is_closed, posts_count, replies, views, created_at, bumped_at, tags, posters, reactions, is_new, unread, notes} — what a Focus tile shows. category is {id, slug, name, colors}; colors is the lineage, root first, the web’s category circle.

A post is {id, post_number, author, cooked, created_at, edited_at, deleted, bookmarked, reactions: [{key, emoji, count, reacted}], can_edit}.

Finding

Method Path Notes
GET /tags {tags: [{name, slug, count}]}, most used first
GET /tags/{slug} {tag, topics}
GET /search?q= {q, topics}; titles, post bodies, tag names; two characters minimum, 50 results
GET /bookmarks {bookmarks: [{post_id, topic_id, topic_title, post_number, cooked, deleted, created_at}]}
GET /notifications {unread, notifications: [{id, kind, topic_id, post_id, topic_title, actor, is_read, created_at}]}, the 50 newest
GET /notifications/unread {unread}
POST /notifications/{id}/read 204
POST /notifications/read 204; all of them

Not in the API

Everything under /admin (categories, reaction types), invites, editing and deleting posts and topics, sign-up and password sign-in stay on the web or the manage CLI. There is no push (docs/android.md §2e).

Testing

tests/api_db.rs drives these routes through the real router against Postgres (just test-db; skipped without TEST_DATABASE_URL). just fixtures also writes the responses to android/app/src/test/resources/api/, which the app’s ContractTest decodes.