The API and Tokens
Outcome: you know where the API's own documentation lives (inside the server), how a script authenticates, and how to give a script a key that opens one drawer instead of the house.
One interface, three ways in
Everything the web app shows you, it
fetched from the API at /api/v1/ — the
lab did nothing in this guide that the
screens do not also do. A caller proves
itself one of three ways:
- a login token (the JWT of chapter 5): the user's full rights, a few days' life;
- an API token: a long-lived key, created for a purpose, with chosen rights;
- CalDAV's basic auth (chapter 10) — a different door for calendar clients.
The API documents itself. The binary
serves a documentation page at
/api/v1/docs and its machine-readable
specification at /api/v1/docs.json —
the lab checked both answer (transcript
t14). When this guide and that page
disagree, believe the page: it is
generated by the version you are running.
API tokens
Creating one is a small ceremony, and the server teaches it by refusal. The lab's first attempt — just a title — came back:
{"code":2002,"message":"Invalid Data",
"invalid_fields":["expires_at: non zero value required",
"permissions: non zero value required"]}
Two fields are compulsory because they
are the point: every API token has an
expiry date and a permission set.
Given both, the same call answers with
the token itself (transcript t10):
$ curl -X PUT …/api/v1/tokens -H "Authorization: Bearer $TOKEN" \
-d '{"title":"lab script","expires_at":"2027-10-31T00:00:00Z",
"permissions":{"projects":["read_all"],"tasks":["read_all"]}}'
{"id":1,"title":"lab script","token":"tk_<redacted>",
"permissions":{"projects":["read_all"],"tasks":["read_all"]},…}
The value begins tk_ and is shown
once, at creation — store it where
you store passwords. Rights are granted
per kind of thing (projects, tasks,
labels, …) and per operation
(read_all, read_one, create,
update, delete), so a backup script
can be given read-everything and a phone
widget read-one-project, and neither can
borrow the other's reach.
The lab then used its read-only token twice. Reading the projects worked. Using it to create a task answered:
{"code":11,"message":"missing, malformed, expired or otherwise invalid token provided"}
HTTP 401
Note the shape of that refusal, because you will meet it in the wild: out-of-scope is reported as invalid token — a 401 claiming the key is bad when the key is fine and merely not allowed to do this. When a script's call is refused, check the token's permissions before you check its spelling.
The verbs, as this build speaks them
The lab's transcripts amount to a small phrasebook for v2.7.1, and chapter 16 collects the bruises:
- create is
PUT(projects, tasks, labels, tokens, shares); - read is
GET, single or list; - update is
POSTon the thing itself, whole-object (chapter 6's rule) — top-level routes like/tasks/{id}, where some nested spellings (/projects/{p}/tasks/{t}) answer 404; - delete is
DELETEon the top-level route (/tasks/{id}worked; the nested one 404'd); - a few reads that feel natural are not offered at all (a bucket's tasks are read through its view — chapter 8).
Other versions may sand these edges down; on yours, the error messages and the built-in docs page are the authority, and the habit of reading back what you wrote is the safety net.
Check yourself: create a token that can only read one project, and use it to look at another. The refusal you get is the fence working — and now you know not to read its message literally.