API and SDK
Everything the app can do, the API can do too (SI-558). The web app is one client of it, next to the CLI, the TUI, cron jobs and Pi agents. They all use the typed client in @hotaru/sdk. The CLI is in cli.md, local agents over MCP in agents.md, the browser extension in extension.md. Why it’s built this way: ADR-0006.
Reference
GET /api/v1/openapi.json is the whole API as an OpenAPI 3.1 document, with each operation’s scope (x-scope) and the SDK method that calls it. Open it with any OpenAPI viewer, or load it into a tool that reads OpenAPI. It needs a login or a token, like everything under /api.
curl -H "Authorization: Bearer hotaru_…" https://hotaru.example.com/api/v1/openapi.jsonThe SDK
import { createClient } from '@hotaru/sdk';
const hotaru = createClient({ baseUrl: 'https://hotaru.example.com', token: 'hotaru_…' });const { items, nextCursor } = await hotaru.stream({ unread: true });await hotaru.markRead(items.map((item) => item.itemId));- Every response is validated against its zod schema. A response that doesn’t match throws.
- Errors throw
HotaruApiError, with the HTTPstatusand the server’sdetailfor people. On429it carriesretryAfterSeconds(rate limits). - Wire types come with it:
StreamItem,Subscription,ChatResponseand the rest, inferred from the schemas inpackages/sdk/src/schemas.ts. - The browser creates its client with
baseUrl: ''and no token: the session cookie goes along.
Live updates
Two server-sent event streams, followed over fetch so a token can open them (the browser’s EventSource can’t send one). Each runs until its signal aborts and reconnects on its own after the server’s retry delay.
const stop = new AbortController();void hotaru.events({ on: { items: ({ count }) => console.log(`${count} new`) }, // Every (re)connect: events may have been missed, so ask newItems() for the total. onOpen: () => void hotaru.newItems(loadedAt), signal: stop.signal});void hotaru.chatEvents({ on: { chat: (chat) => render(chat) }, signal: stop.signal });| Stream | Event | Data |
|---|---|---|
GET /api/v1/events |
items |
{ count }: new items arrived in your stream |
GET /api/v1/chat/events |
chat |
the whole chat, after each change (chat.md) |
Reducers
Pure functions every client runs its own state through, so the CLI, the TUI and the web app change state the same way:
| Function | What it does |
|---|---|
mergeStreamPage(items, page) |
Appends the next page without items already loaded |
applyItemPatch(item, patch) |
The item as it will be once the server takes the patch, to show at once |
toggleLabel(labels, label) |
Turns a Signal, Facts or Hype label on or off, in the server’s order |
reduceItemsEvent(count, event) |
Adds up arrivals from items events |
reduceChatEvent(chat, event) |
The chat after a chat event (it replaces what was there) |
Scopes
| Scope | May | Made by |
|---|---|---|
read |
Read everything; preview filters and rules; resolve views | A token |
cron |
Also start work: fetch a source, rerun an item’s pipeline, group topics, run a taste review | A token |
subscribe |
Read, find feeds and follow them (discover, previewSource, subscribe); nothing else. For the browser extension |
A token |
write |
Everything a token may do, cron and subscribe included |
A token |
session |
Manage API tokens and the starred feed’s address; export or delete your account | A logged-in browser only |
The admin screen’s requests (/api/v1/admin/…, admin: true in the capability list, x-admin in the OpenAPI document) are session too, and only for admins: anyone else gets 403 (accounts.md).
A token without the scope gets 403 with a message saying which token it needs. The scope of each request is in packages/sdk/src/capabilities.ts, and in the OpenAPI document. Tokens: auth.md.
Rate limits
Every API request counts against two per-user limits (SI-563), whether it comes from a browser or a token:
| Limit | Counts | Allows |
|---|---|---|
| Requests | Every request under /api/ |
600 a minute |
| Expensive | Requests that fetch other sites, call a model or start work | 120 in 10 minutes |
The expensive ones are marked expensive: true in packages/sdk/src/capabilities.ts and x-expensive in the OpenAPI document: following, discovering and previewing feeds, recipes, refreshing a source, adding a link, loading a full article, claim checks, rerunning the pipeline, grouping topics, taste reviews, chat messages, imports, check previews, saving to Raindrop, Readwise and MyMind and connecting them, test notifications (notifications.md), your data export, and deleting your account (it checks a password). An expensive request counts against both limits.
Over a limit, the answer is 429 with error: "rate_limited", a message for people and a Retry-After header in seconds. The counters live in memory, so they start again when the app restarts. Pages and /health aren’t limited. Some features have a daily limit of their own on top, such as 30 recipes or 20 claim checks a day. The code: apps/web/src/lib/server/rate-limits.ts.
A request over one of the account’s quotas answers 403 with the reason: following a source beyond its limit, adding a link with its storage full, or a budget above its limit (accounts.md).
Adding an endpoint
- Write the route under
apps/web/src/routes/api/v1/, validating with a schema frompackages/sdk/src/schemas.ts(readBody()). - Add a capability to
packages/sdk/src/capabilities.ts: the method name, the path with{params}, the scope, the schemas and a summary. - Add the client method of that name to
packages/sdk/src/client.ts. - Use it from the page through
api, never a form action or a directfetch. - Give it a case in the isolation suite,
apps/web/src/lib/server/isolation.spec.ts: whether another account’s IDs must be refused, or what the request may do with the caller’s own data (accounts.md). Mark itexpensivewhen it fetches, calls a model or starts work.
pnpm test fails until all five agree: packages/sdk/src/capabilities.spec.ts checks the list against the client, apps/web/src/lib/server/capabilities.spec.ts checks it against the routes, and that the browser uses only the SDK. The exceptions are the login pages and the server’s load functions, which only read (ADR-0006).