The API and Tokens
By the end of this chapter a script with no login at all can read your base, using a token you made for it — and you know how to revoke that power, and where the API's own manual lives.
Why an API chapter in a grid's guide
Everything the web app does, it does through the REST API — the lab is the proof, having built this whole guide's example without opening the app. That means anything a person can do in the grid, a program can do on a schedule: the nightly stock report, the import from the till, the little script that flags rows older than a year. The grid is for people; the API is how the base joins the rest of your machinery.
Two kinds of pass
The API accepts two credentials, and the difference matters:
- The login token (
xc-authheader) is a person's pass, issued at sign-in, carrying that person's roles — the admin token this lab used for most calls. It expires; chapter 10 is about the roles inside it. - An API token (
xc-tokenheader) is a thing's pass: created by a user from the tokens page (or the API), named, optionally scoped and expiring, and revocable without changing anyone's password. This is the one scripts should hold.
The lab created one and used it for a read, then checked the refusal with a nonsense token (transcript t08):
curl -X POST http://localhost:18104/api/v1/tokens \
-H "xc-auth: <login token>" \
-d '{"description":"Lab read token (fixture)"}'
curl .../api/v2/tables/<items-id>/records?fields=Name \
-H "xc-token: <api token>"
# -> ["Cordless drill", "Box of screws (assorted)", ...]
curl ... (same) -H "xc-token: deadbeef-not-a-token"
# -> HTTP 401 {"error":"ERR_AUTHENTICATION_REQUIRED", ...}
Treat an API token like a password for exactly what it can reach: give each script its own, write down where each is used, and delete tokens whose jobs have ended. The tokens table the lab saw inside the database (chapter 13) is the server keeping that same list.
The manual the server writes for you
There is a second document endpoint, and it is this chapter's favourite thing. For each base, the server generates an OpenAPI description — the contract of the data API for your tables, field names and all:
curl http://localhost:18104/api/v2/meta/bases/<base-id>/swagger.json \
-H "xc-auth: <token>"
The lab's copy is 26 KB describing 8 paths: for each table, list and create records, count them, fetch one, and the links endpoint chapter 7 used. Feed that file to an OpenAPI viewer or a client generator and your base has documentation that cannot drift from the truth, because the server writes it from the same definitions it serves.
The API's two generations, honestly
This lab worked against the v2
addresses (/api/v2/...) for data and
most metadata, with v1 still answering
for sign-in, tokens and the info
endpoint. Two v2 behaviours, met in
earlier chapters, are worth expecting
rather than discovering at midnight:
some request properties belong at the
top level of the body in this build
(the link and rollup calls of chapter 7
were refused until they were), and view
creation lives at per-kind addresses
(/kanbans, /galleries) rather than
one /views route. The errors in each
case named the missing thing plainly,
and the transcripts keep them verbatim
— when this API refuses you, read the
message twice before reaching for the
documentation; it is usually the
documentation.