Skip to content

Server API

createAnnotationsHandler({ storage, auth, prefix, onChange, pageUrl })

Section titled “createAnnotationsHandler({ storage, auth, prefix, onChange, pageUrl })”

From starlight-annotations/server. Returns (request: Request, context?) => Promise<Response | null>. context is the platform’s (ctx on Cloudflare Workers): with waitUntil, notifications run after the response.

Route
GET {prefix}/api?page=<page> The notes of a page and the signed-in user.
GET {prefix}/api/all The notes of every page, for the overview page. 501 if the storage has no list().
POST {prefix}/api { page, change }: applies one change and returns the notes. Same-origin only.
GET {prefix}/login, /callback, /logout Handled by the auth, when it has sign-in pages.

onChange (a function or a list) is called after every saved change; pageUrl(page, request) overrides the link in its event. See Notifications, with slackWebhook(url), teamsWebhook(url) and describeChange(event).

prefix defaults to /_annotations. If your site has a base, include it: prefix: '/docs/_annotations'.

A change is one of create (with a target and a body), reply (with replyTo, optional inReplyTo), resolve, delete. Only the author may delete.

Returns { storage, auth }: githubStorage(options) and githubAuth(options) with the same options.

Option Default
repo owner/name that stores the notes.
token Contents read/write on repo. Or use app.
app A GitHub App instead of token: { appId, privateKey, installationId? }. See Use a GitHub App.
branch 'annotations' Created from the default branch on the first save.
dir 'annotations' Folder in the branch.
commitMessage (message, page) => string, to follow your commit convention.
issues true to let readers turn a thread into a GitHub issue in repo, or { repo: 'owner/name' } for another repository. See Open an issue from a note.
clientId, clientSecret The OAuth app, or the GitHub App’s client ID and secret.
sessionSecret Signs the session cookie.
writers 'collaborators' 'collaborators' (write, maintain or admin access to repo), 'anyone', a list of logins, or (profile) => boolean.
emails false Read the reader’s email at sign-in and store it, encrypted, on their notes, for notifying the author of a note.
recheck 60 Seconds between checks that a signed-in reader may still write. false checks only at sign-in. See below.
cookieSecure true Set false only to test over plain http.

writers is checked at sign-in and again while the session lasts: when a reader saves a note or loads a page, the answer is reused for recheck seconds and then asked again. If their access was removed, the save is refused and they are signed out, so taking someone off the repository takes effect within a minute instead of when their 30-day session ends. If GitHub does not answer, pages still load and saves fail until it does. The answers are kept in memory, per server instance; with 'collaborators', each check is one API call made with token.

{ storage, auth } for GitLab. Like github, with:

Option Default
host 'https://gitlab.com' Your GitLab.
project Path ('group/docs') or numeric id.
token Scope api, Developer role or higher.

writers: 'collaborators' means members with the Developer role or higher. See the GitLab guide.

{ storage, auth } for Gitea, Forgejo and Codeberg. Like github, with:

Option Default
host Your server, e.g. 'https://codeberg.org'. Required.
repo owner/name.
token repository: Read and write.

writers: 'collaborators' means write, admin or owner permission. See the Gitea guide.

githubAppToken({ appId, privateKey, repo, installationId? })

Section titled “githubAppToken({ appId, privateKey, repo, installationId? })”

Returns () => Promise<string>: an installation token of a GitHub App, limited to repo, renewed before it expires. github({ app }) uses it; call it yourself to reach the GitHub API with the same app elsewhere.

Everybody is user. For development, or behind your own login (Cloudflare Access, a VPN) where you know who the reader is. Write your own auth to read the identity from a header instead: an object with user(request), and optionally allowed(user), which the handler calls before every save and on page loads to refuse and sign out someone who may no longer write.

From starlight-annotations/server/node:

  • fsStorage({ dir }): notes as files under dir.
  • toNodeMiddleware(handler): (req, res, next) middleware for node:http, Connect, Express.