Reference

API

Hubstruct's HTTP API. Where its reference is, signing in with keys, how routes are laid out, and the live stream.

Everything the app does goes through Hubstruct's API: the app has no other way in. So anything you can do in the app, a script can do too.

The reference

The API describes itself with OpenAPI:

  • In the app: API docs, in the side rail, is the full reference, with every route, its parameters and its answers, and lets you try them signed in as you.
  • Self-hosted: the API serves an interactive reference at http://<host>:3000/reference, and the raw OpenAPI document at /openapi.json, for generating clients.

Reaching it

From outside, call the API through the app's address, under /api:

text
https://photos.example.com/api/libraries

Self-hosted, it's also published directly on port 3000 (http://<host>:3000/libraries), without the /api prefix.

Signing in

Every route needs either a signed-in session (the app's cookie) or an API key, except health and the sign-in routes themselves. Use a key as a bearer token:

Terminal
curl -H "Authorization: Bearer $HUBSTRUCT_API_KEY" \
  https://photos.example.com/api/libraries

Make keys on your profile. See API keys.

How routes are laid out

Routes that belong to a library sit under it:

text
/libraries/{libraryId}/media
/libraries/{libraryId}/media/{id}
/libraries/{libraryId}/media/upload
/libraries/{libraryId}/albums
/libraries/{libraryId}/tags
/libraries/{libraryId}/people
/libraries/{libraryId}/groups
/libraries/{libraryId}/stacks
/libraries/{libraryId}/duplicates
/libraries/{libraryId}/trash

An id from another library answers 404, as if it didn't exist.

Routes that don't belong to a library are at the top: /libraries itself, /auth/…, /users, /settings, /health/… and /events.

Common tasks

List a library's newest media:

Terminal
curl -H "Authorization: Bearer $KEY" \
  "https://photos.example.com/api/libraries/$LIB/media?sort=created_at&dir=desc&limit=50"

Search by meaning:

Terminal
curl -H "Authorization: Bearer $KEY" \
  "https://photos.example.com/api/libraries/$LIB/media/search?q=dogs+in+snow&limit=50"

Upload a file to a library:

Terminal
curl -X POST -H "Authorization: Bearer $KEY" -H "Content-Type: image/jpeg" \
  --data-binary @photo.jpg \
  "https://photos.example.com/api/libraries/$LIB/media/upload?filename=photo.jpg"

Lists page with a cursor: each page's answer includes the cursor for the next, until there is none. Search results page with limit and offset, and take match=look to search by look alone, like=<media id> to search by a media, and order=farthest to turn the ranking around.

The live stream

GET /events is a Server-Sent Events stream of changes as they happen. It's what keeps the app's pages live:

EventData
helloOn connecting: { boot, versions }
change{ boot, versions, topics, mediaAdded, mediaRemoved }, at most about twice a second while things change
poke{ library, kinds }: a library's rows changed (library is * when any may have). ?library=<id> sends only that library's
pingEvery so often, so proxies keep the stream open

Each topic's version goes up with every change. To tell what you missed while disconnected, compare versions from before with the next hello. boot changes when the API restarts, and then you've missed everything. The stream ends when its session or key does.

Sync

The app keeps a copy of each library in the browser with Replicache, and these are its endpoints, under the library:

  • POST /libraries/{id}/sync/pull: Replicache's pull. Without a cookie, every row of the library (media as lists show them, tags, albums and their members, people, groups); with the cookie the last pull returned, only the rows changed since, and the keys of those gone.
  • POST /libraries/{id}/sync/push: Replicache's push: changes made in the app (favorites, NSFW answers, tags), each applied once, in order, as the route it is.

A poke on the live stream says when there's something to pull.

Health

Without signing in:

  • GET /health/live: the API process is up.
  • GET /health/ready: ok, degraded (an optional service is down) or down (HTTP 503, the database is down).