Access and credentials

The admin API writes content and settings, so it asks who is calling. Two kinds of caller exist: a person with an email and a password, and a machine with a personal access token. Both end up as the same thing — an administrator — but they reach the API differently.

Create the first administrator

There is no configuration file holding an administrator: the first one is written into the site's database. The admin API ships a provisioning script that does exactly that. (On a deployment run by someone else, the site is provisioned with its administrator already — see Sites and instances.)

export DATABASE_URL='<database-url>'
export SNOLLA_ADMIN_EMAIL='you@example.com'
read -rs SNOLLA_ADMIN_PASSWORD && export SNOLLA_ADMIN_PASSWORD
npm run admin:provision
Variable Meaning
DATABASE_URL The site's database
SNOLLA_ADMIN_EMAIL The administrator's email — the key of the record
SNOLLA_ADMIN_PASSWORD At least 8 characters. read -rs reads it without echo and without passing it as a command-line argument

The script is keyed by email: running it again for the same email sets a new password for that administrator rather than creating a second one. That is also the recovery path when a password is lost — there is nothing to guess, only a record to reset.

Sign in

ADMIN_API=http://localhost:4731

curl -X POST "$ADMIN_API/api/auth/login" \
  -H 'content-type: application/json' \
  -d '{"email":"<email>","password":"<password>"}' \
  -c cookies.txt

The answer names the administrator, and the session itself travels in the admin_token cookie — HttpOnly, SameSite=Lax, valid for seven days, Secure when NODE_ENV=production. Send that cookie back on every call — curl does it from a jar:

curl "$ADMIN_API/api/auth/me" -b cookies.txt
curl -X POST "$ADMIN_API/api/auth/logout" -b cookies.txt

A wrong password and an unknown email answer the same way — Invalid email or password — so the endpoint cannot be used to find out which addresses exist.

Issue a token for a script

A cookie belongs to a browser. A server, a build or an agent needs a token:

curl -X POST "$ADMIN_API/api/keys/" \
  -H 'content-type: application/json' \
  -b cookies.txt \
  -d '{"name":"release script","expiresAt":"<iso-timestamp>"}'   # expiresAt is optional
{
  "success": true,
  "data": {
    "apiKeyId": "…",
    "name": "release script",
    "token": "sk_1f0c1…"
  }
}
Field Meaning
name What the key is for. It is what you will see in the list
expiresAt Optional ISO timestamp when the key stops working. An expired key is refused like a revoked one

The token is sk_ followed by 48 hexadecimal characters, and this response is the only time it is shown: the database keeps a SHA-256 hash, and the list returns only the first eight characters of the token — the sk_ prefix and five more — so you can tell keys apart. A token that was not saved has to be replaced, not recovered.

curl -H "Authorization: Bearer <token>" "$ADMIN_API/api/administrators/"   # proves the credential is accepted

A key belongs to the administrator who issued it and is bound to a site: the same key presented to another site answers this key belongs to another site. A revoked or expired key answers Invalid API key. Both are refusals, not empty results.

Call What it does
POST /api/keys/ Issue a key — the only time the token is shown
GET /api/keys/ List keys: name, prefix, expiry, revocation, creation date
DELETE /api/keys/{id} Revoke one key

People

Call What it does
GET /api/administrators/ The administrators of the site
GET /api/administrators/{id} One administrator
POST /api/administrators/save Create or update an administrator

A password is never read back: the API writes and compares hashes only.

Development mode

Run the admin API with NODE_ENV=production on every deployment, a staging one included. Without it the API does not ask for a credential: a request is served as an existing administrator. That is convenient on a laptop and wrong anywhere a port can be reached — it makes the whole content database writable without a password.

Related