← All writing

· 2 min read

How this site gets written: a small token API

The admin has an HTTP API with scoped bearer tokens, so scripts and AI agents can draft, preview and publish posts without a browser session.

Drafts on this site do not have to come from the admin editor. There is an HTTP API under /api/v1, and a script or an AI agent with a token can do almost everything the admin can. This post describes how it works.

Tokens, not sessions

The admin uses passkeys, which are right for a person and wrong for a script. So scripts get bearer tokens: a header of the form Authorization: Bearer <token>. A token is created in the admin, shown once, and only its hash is stored. If you lose it, you revoke it and make another. Each token has an expiry of 30 days, 90 days, a year, or none.

Twelve scopes

A token carries a list of scopes, and every route checks for the one it needs.

  • posts:write and posts:publish split drafting from publishing.
  • projects:write and projects:publish do the same for project pages.
  • preview renders Markdown, or gives a private link to a saved post that expires after 30 minutes.
  • uploads adds images to the public Blob store.
  • messages:read and messages:write cover the contact inbox.
  • analytics:read and analytics:write cover the stats and the excluded IP list.
  • tokens:write and sessions:write can create tokens and revoke sessions. Both are labelled "grant sparingly" in the admin, and a token can never mint another token with more power than it has.

Because the scopes are separate, an agent that only writes drafts cannot publish them, and a leaked draft-only token cannot read messages.

The publish checklist

Publishing is a separate call, and it fails with a list of reasons unless the post has a title, a real slug, an excerpt of at least 20 characters and a body of at least 50 words. Projects get a stricter version that also checks every link for a 2xx response. A draft can be as rough as it likes. Only the publish step is strict.

Images

An upload takes raw bytes and a content type: PNG, JPEG, WebP, GIF or AVIF, up to 4.5 MB, which is the request limit on the host. The server checks that the bytes match the type you declared, so a PNG sent as JPEG is refused. You get back a public URL to paste into the Markdown.

Where to read more

This post was created through the same API it describes. The full reference lives in the repository as API.md, and GET /api/v1 returns an index of every endpoint and the scope it needs.