Short answer: the sandscale CLI is built to be scripted. sandscale sandbox exec and sandscale sandbox run exit with the remote command's own exit code, so make, set -e and && chains work as if the command ran locally. For everything else, a fixed set of exit codes tells your script what kind of failure it hit, and --json gives machine-readable output. For CI and servers, use a personal API key with the SDK or the API rather than a CLI sign-in; Sandscale's authentication docs recommend that.
All commands and codes here are from the live CLI reference, which is generated from the CLI itself.
The exit codes
From the sandscale sandbox reference:
| Code | Meaning |
|---|---|
| 0 | Done (for exec and run: the command's own code) |
| 1 | Failed |
| 2 | Usage error |
| 3 | No session (run sandscale login) |
| 4 | Refused |
| 5 | Not found |
| 6 | Conflict |
| 7 | Rate limited |
One thing to watch: for exec and run, the remote command's exit code passes through, so a test runner that exits 1 and a CLI failure that exits 1 look the same to your script. When the difference matters, add --json to exec and read the result rather than the exit code alone.
A Makefile target that runs tests in a microVM
test-remote:
sandscale sandbox run --sync . -- make test
run starts a sandbox, sends the folder, runs the command, streams its output, exits with its code and stops the sandbox afterwards. make test-remote fails exactly when make test fails inside the sandbox. How to run your test suite with sandscale sandbox run covers --sync, --keep and network.
Check what would be sent before you send it
sandscale sandbox run --sync . --dry-run -- true
--dry-run lists what would be sent and sends nothing. Per the reference, --sync leaves out what .gitignore leaves out, .git, node_modules and symbolic links, and never sends secret or environment files such as .env*, keys, credentials, cloud and registry tokens and Terraform state; those are listed by rule and path, never shown. A pre-push hook can run the dry run and stop if the list is longer than expected.
Branching on refusals and rate limits
sandscale sandbox start --template python-3.12 --ttl 1h --network none --json > box.json
case $? in
0) echo "started" ;;
3) echo "not signed in: run sandscale login" ; exit 1 ;;
4) echo "refused by plan or policy" ; exit 1 ;;
7) echo "rate limited, try again later" ; exit 1 ;;
*) echo "failed" ; exit 1 ;;
esac
Exit code 4 is the one to surface to a person: the request was understood and refused, for example by your plan's limits or your organization's policy. Retrying will not help. Exit code 7 is worth retrying after a pause. The template name is the reference's own example; use one from sandscale sandbox template list.
Reading JSON output
Most commands take --json, including sandbox list, show, start, exec, logs and metrics. Run a command once with --json and look at the shape before writing a jq filter against it; the reference documents the flags, not every field. sandscale sandbox metrics ID --follow --json prints one JSON object per line, which suits line-by-line processing.
Signing in on a machine without a browser
sandscale login --device
The CLI signs in with the browser and PKCE by default; --device uses the device authorization grant, so you approve the code on another device. The tokens are kept in the operating system's credential store, never in a file, and the CLI never prints them. sandscale whoami tells a script who is signed in.
When to stop using the CLI in scripts
The CLI session is your own sign-in. Sandscale's authentication docs say that to call the API from a script you should create a personal API key rather than reusing the CLI's session. In practice:
- Your laptop, git hooks, Makefiles: the CLI with your sign-in is fine.
- CI and servers: a personal API key with only the scopes the job needs, held in the CI's secret store, used with the TypeScript SDK or curl. Keys expire within 90 days. See scoping API keys for agents and CI and the GitHub Actions example.
Either way, each Sandscale sandbox the script starts is its own Firecracker microVM, inside your plan and your organization's policy.
FAQ
What exit code does sandscale sandbox exec return?
The remote command's own exit code. Other CLI exit codes are 1 failed, 2 usage, 3 no session, 4 refused, 5 not found, 6 conflict and 7 rate limited.
How do I sign in to the Sandscale CLI on a headless machine?
Run sandscale login --device and approve the code on another device. The CLI keeps its tokens in the operating system's credential store.
Should I use the Sandscale CLI in CI?
Sandscale's docs recommend a personal API key for scripts rather than reusing the CLI's session. Use the key with the TypeScript SDK or the HTTP API in CI.
Can I see what sandscale sandbox run will upload?
Yes. Add --dry-run. It lists what would be sent and sends nothing.
Related
- Handling Sandscale API errors in TypeScript
- Monitoring a sandbox's CPU, memory and runs
- Custom sandbox templates pinned by digest
Originally published on the Sandscale blog.
Top comments (0)