Skip to content

The API in Brief

BookStack has a proper REST API: the library can be read and written by scripts, in JSON, over the same address the humans use. This chapter is a brief on purpose — enough to authenticate, list, create and search, all of which the lab did from the command line — with the full reference living in the instance itself at /api/docs.

Tokens: one per user, shown once

API access is personal. A user whose role includes the Access API permission opens My Account → API Tokens and creates a token: a name, an expiry date. BookStack generates two values:

  • a Token ID, the public half, and
  • a Token Secret, shown once, at creation. It cannot be read back later; lose it and you delete the token and mint another.

Requests authenticate with one header:

Authorization: Token <token-id>:<token-secret>

No token, no entry: the lab's first call without the header came back 401, and the same call with it came back with the library. A token acts as its user — same permissions, same fences. An admin's token is an admin, so store secrets like passwords, scope them to the least user that does the job, and let them expire.

The shape of the thing

Endpoints mirror the hierarchy you know: /api/shelves, /api/books, /api/chapters, /api/pages, plus /api/search, /api/users, /api/attachments and more. Lists come back as JSON with a data array and a total. Creating is a POST of JSON; reading is a GET; the responses are the records themselves, ids and all.

Worked example: the lab's three calls

List the books:

curl -s -H "Authorization: Token $TID:$SECRET" \
  http://your-server/api/books

Create a page inside an existing chapter (chapter 3, in the lab's library):

curl -s -H "Authorization: Token $TID:$SECRET" \
  -H "Content-Type: application/json" \
  -d '{"chapter_id": 3,
       "name": "Sowing Leeks in Modules",
       "html": "<p>Leeks sown in modules...</p>"}' \
  http://your-server/api/pages

The response is the new page as JSON — id, slug, the chapter it landed in, and the stored HTML, which gained BookStack's own paragraph anchors on the way in. Send "markdown" instead of "html" if you'd rather write that way; the API accepts both, matching the two editors of chapter 6.

Search, the same search chapter 7 proved in the browser:

curl -s -H "Authorization: Token $TID:$SECRET" \
  "http://your-server/api/search?query=pluviometer"

One result, the pumpkins page — the API and the search box agree, because they are the same search.

Check: repeat the book list with a wrong secret. A 401 is the correct answer; anything else means the token isn't the thing protecting you.

What the API is for, and not for

For: imports and exports of whole books, generated pages (the club's monthly results page, written by a script), bulk tidying, and backing up content in a readable form alongside chapter 10's real backup. Not for: bypassing the permission model — there is no API back door, and a token sees exactly what its user sees.