knot.token
The knot.token library manages the current user’s API tokens: mint keys for machines and pipelines (optionally narrowed by scopes), list existing tokens, and revoke them.
Execution Environment
| Environment | Behaviour |
|---|---|
Embedded (MCP tool execution, event sinks, plugin handlers, remote/space scripts, knot run-script) |
Available; authenticated automatically via the Go-provided knot.apiclient transport. |
| Health check scripts | Only when the space’s environment sets KNOT_API_PORT or KNOT_URL/KNOT_TOKEN: there is no built-in transport, so the Python knot.apiclient configures itself from the agent (as the space owner) or those variables, and calls raise otherwise. See Environment Compatibility. |
| External (standalone scripts) | Python implementation; configure knot.apiclient first (or set the KNOT_* environment variables). |
Functions
| Function | Description |
|---|---|
list() |
List the current user’s API tokens |
create(name, scopes=None) |
Create an API token and return its value |
update(token_id, name=None, scopes=None) |
Rename a token or change its scopes |
delete(token_id) |
Delete a token by id (its value), revoking it immediately |
list()
List the current user’s API tokens.
Returns: list of dicts, each containing:
id(string): The token’s value — the bearer key itselfname(string): The token’s nameexpires_after(string): Expiry timestamp — any use resets the two week clockscopes(list): The token’s scope list, empty for full access
create(name, scopes=None)
Create an API token for the current user.
Parameters:
name(string): Name identifying the tokenscopes(list, optional): Narrows the token to endpoint groups —"methods"(/api/methods*),"mcp"(/mcp) and"tunnels"(/tunnel/*and/api/tunnels*: create, list and delete tunnels only),"files"(/api/files*, and on Knot Pro the S3 endpoint with the token as the secret key) and"backup"(/api/backup*and/api/restore*only, for a token left in a backup job). Every scoped token can also read its own identity at/api/users/whoami. Empty or omitted means full access.
Returns: string - The new token’s value (the bearer key) — pass it to clients, e.g. space.tunnel_start(..., token=...) or a pipeline’s knot tunnel --token.
update(token_id, name=None, scopes=None)
Rename an API token or change its scopes; only the arguments given change.
Parameters:
token_id(string): The token’s value, fromcreateorlistname(string, optional): New name for the tokenscopes(list, optional): New scope list, as forcreate;[]clears the scopes, giving the token full access
Returns: bool - True on success
delete(token_id)
Delete an API token, revoking it immediately.
Parameters:
token_id(string): The token’s value, fromcreateorlist
Returns: bool - True on success
Examples
import knot.token as token
# A tunnels-only key for a machine that should do nothing but expose a port
key = token.create("pipeline-tunnel", scopes=["tunnels"])
print(key) # the bearer key — the only time to copy it is now or via list
# Revoke it when the pipeline retires
token.delete(key)