The API and automation
By the end you can
- Create an API token and call the API with it
- Read the live OpenAPI description and the built-in API page
- Handle the error statuses correctly in a script
- Create a user through the API and from the host command line
Everything goes through the API
Everything the console does goes through its REST API under /api, so anything you can do in the console you can script. Terraform, Ansible and CI jobs use the same endpoints as the console, with the same role permissions.
Tokens
- Scripts: create an API token under Settings, API tokens (or
POST /api/tokens) and send it asAuthorization: Bearer ra_…. A token has the role it was created with: an admin token may do everything an administrator may, a viewer token may only read. There are no finer scopes, so give automation the narrowest role that works. - Browsers: the
ra_sidsession cookie fromPOST /api/auth/login.
The plaintext token is shown once at creation and Authcha stores only a hash, so a leaked token can be revoked but not recovered. A request with a Bearer header that is not valid, unexpired and unrevoked is refused with 401, even if a cookie is also sent. Changes made with a token appear in the audit log like any other. The API tokens page now warns when a token has 14 days or fewer left, because automation using it stops when it expires.
The OpenAPI description
| Where | What |
|---|---|
GET /api/openapi.json | The live description for your version. Needs a session or token |
/api/docs | A built-in page listing every operation, who may call it and what it does. Works offline, with no external scripts or fonts. Sign in to the console in the same browser first |
docs/product/openapi.json | A copy for tools that read a file: code generators, Postman, linters |
Each operation lists the roles allowed through (x-roles) and the error model. A test fails whenever a route is added without being described, so the description cannot fall behind. GET /api/guides lists the product guides, and GET /api/guides/<slug> returns one as Markdown, for scripts and for anyone without a browser.
Errors
A failed request answers with a status and, mostly, { "error": "message" }.
| Status | Meaning |
|---|---|
| 400 | Invalid input or malformed JSON. The message names the problem |
| 401 | Not signed in, session expired, or the token is invalid, revoked or expired |
| 403 | The role, the administrator's provisioning scope, the license, the edition or the public demo refuses it. The message says which |
| 404 | No such object |
| 409 | Already exists, or in the wrong state |
| 415 | The body was not sent as Content-Type: application/json |
| 422 | The JSON parsed but a field is missing or the wrong type. The answer is plain text, not the JSON shape |
| 429 | Rate limited. Read the Retry-After header |
| 502, 503 | A service the request depends on refused or is unavailable |
| 500 | Internal error. Details only in the server log |
A good script retries 429 and 503 with a delay, and fails loudly on everything else.
Create a user in one call
curl -sS -X POST https://authcha.example.internal/api/users \
-H "Authorization: Bearer $AUTHCHA_TOKEN" \
-H "Content-Type: application/json" \
--data @- <<'JSON'
{
"username": "alice",
"password": "a-long-passphrase",
"user_type": "human-read",
"mobile_number": "",
"email": "alice@example.com",
"display_name": "Alice Example"
}
JSONusername is 1 to 64 characters from A-Z a-z 0-9 @ . _ -. The password is stored as a bcrypt hash and never returned. user_type must be an enabled entry of GET /api/user-types that your administrator account may provision. mobile_number is required, so send "" if there is none. A name that already exists, even one in the trash, answers 409. Change a password later with PATCH /api/users/{username}.
Create a user from the host
Without the console or a token, on the Authcha host:
sudo authcha user-create --username alice --user-type human-read \
--mobile "+15550100" --password-file ./alice.pw
printf '%s\n' "$PW" | sudo authcha user-set-password --username alice --password-stdinThe password is never an argument, because it would show in the process list. Give it on standard input, in a file only its owner can read (mode 0600), in AUTHCHA_USER_PASSWORD, or at a prompt. --update changes a user if it exists and creates it if not. Exit codes: 0 done, 1 unexpected, 2 usage, 3 already exists (or does not exist), 4 refused (invalid name, unknown or disabled type, or the license). Each success writes an audit entry shown as cli.
Voucher redemption is the one public call
POST /api/voucher/redeem is public, for captive portals, with body {"code", "mac_address"}. Every failure has the same body and a randomised delay, and the rate limit answers 429.
LabHands-on
On your install (the demo's API is read-only).
- Create a viewer token under Settings, API tokens. Save it once.
- Call
curl -s -H "Authorization: Bearer $TOKEN" https://<host>/api/openapi.json | headand then try a write call with it. Read the 403. - Open
/api/docsin your signed-in browser and find the operation that creates a user and itsx-roles. - Create a user with the API using an admin token. Run it twice and read the 409.
- Send the call without the
Content-Typeheader and read the 415. - Create a user from the host with
authcha user-create, using a password file. Check the audit entry sayscli. - Revoke the tokens you made.
Check yourselfQuiz
Cheat sheetKeep this
- Token:
Authorization: Bearer ra_…. Roles admin or viewer only. Shown once. Warns at 14 days left. - Description:
/api/openapi.json,/api/docs. Guides:/api/guides. - Errors: 400 input, 401 auth, 403 role/license/edition/demo, 409 exists, 415 wrong content type, 422 wrong field, 429 rate limit (
Retry-After). - Create a user:
POST /api/users, orsudo authcha user-create(no password as an argument). - Public:
POST /api/voucher/redeemonly. - Read more: API reference.