Scripting and automation
space is built to be called from scripts and CI: data goes to stdout, messages to stderr,
--json gives machine-readable output, and the exit code
says what went wrong.
Authenticate in CI
Use environment variables, so no config file is needed:
export AGENTSPACES_API_KEY=<key> # from your CI's secret store
export AGENTSPACES_ORG=acmeAdd AGENTSPACES_URL only if you use another portal than https://app.agent-spaces.eu.
Create a dedicated key for each pipeline in the portal. Make it read only unless the job must change machines. See Authenticate.
JSON output
space machines show dev --json--json prints the API’s object (here the machine) on stdout. Fields are those of the portal
API, so they are stable for scripts. For example, with jq:
space machines list --json | jq -r '.[] | select(.state == "sleeping") | .name'Errors under --json go to stderr as:
{"error": {"code": "not_found", "message": "machine not found", "details": {}}}-q / --quiet prints only names (machines, checkpoints, organizations, profiles) or ids (SSH
keys) for lists, the state for space machines show, the new id for space ssh-keys add, and
nothing for other commands:
for m in $(space machines list -q); do space machines sleep "$m"; done
space machines show dev -q # prints e.g. runningWait for a state
Machine commands are asynchronous: without --wait they return as soon as the portal accepts
them. Add --wait to block until the machine reaches its target state:
space machines create ci-runner --from base --wait --timeout 300| Command | Waits until |
|---|---|
machines create, machines wake, machines restore | running |
machines sleep | sleeping |
machines delete | deleted |
checkpoints create | committed |
If the machine ends up failed, the command exits with code 5 and prints the reason; if the
timeout (default 600 seconds) passes, it exits with 6. -w is short for --wait;
--timeout without --wait is a usage error. With --wait and --json, the command
prints the machine as it was when the wait ended (null after a delete).
Reads (including the polls) are retried up to 3 times on a rate limit (429) or an
unavailable region (502/503). Commands that change something (create, suspend, delete, …) are
never retried, so a network error leaves you to check with space machines show before you retry
one yourself.
Non-interactive deletes
space machines delete and space checkpoints delete ask for confirmation on a terminal. Without a
terminal they refuse unless you pass --yes:
space machines delete ci-runner --yes --waitExample: a throwaway machine per CI job
set -eu
name="ci-$CI_JOB_ID"
space machines create "$name" --from base --wait
trap 'space machines delete "$name" --yes' EXIT
space ssh "$name" ./run-tests.shspace ssh exits with the remote command’s exit status, so the job fails when the tests do.
This job needs a read-write key, and an SSH private key in the job whose public key is
registered on the same account, because SSH keys belong to the user the API key acts as.
Register that public key once, in the portal (Account → SSH keys): an API key cannot
add SSH keys.