# Scripting the Sandscale CLI: exit codes, JSON output and when to switch to an API key

> Use the sandscale CLI in shell scripts, Makefiles and git hooks: what each exit code means, how sandbox exec and run pass through the command's code, JSON output, dry runs, and why CI should use an API key instead of the CLI session.

Use the sandscale CLI in shell scripts, Makefiles and git hooks: what each exit code means, how sandbox exec and run pass through the command's code, JSON output, dry runs, and why CI should use an API key instead of the CLI session.

November 27, 2026 · 5 min read · The Sandscale team

**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](https://sandscale.dev/docs/reference/cli), which is generated from the CLI itself.

## The exit codes

From the [`sandscale sandbox` reference](https://sandscale.dev/docs/reference/cli/sandbox):

| 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

```make
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](https://sandscale.dev/blog/how-to-run-tests-in-a-sandbox-with-the-sandscale-cli) covers `--sync`, `--keep` and network.

## Check what would be sent before you send it

```sh
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

```sh
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

```sh
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](https://sandscale.dev/docs/api/authentication) 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](https://sandscale.dev/docs/sdks/typescript) or curl. Keys expire within 90 days. See [scoping API keys for agents and CI](https://sandscale.dev/blog/scoping-api-keys-for-agents-and-ci) and the [GitHub Actions example](https://sandscale.dev/blog/github-actions-run-agent-pr-tests-in-sandbox).

Either way, each [Sandscale sandbox](https://sandscale.dev/sandboxes) 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](https://sandscale.dev/blog/handling-sandscale-api-errors-typescript)
- [Monitoring a sandbox’s CPU, memory and runs](https://sandscale.dev/blog/monitoring-sandbox-cpu-memory-and-runs)
- [Custom sandbox templates pinned by digest](https://sandscale.dev/blog/custom-sandbox-templates-pinned-by-digest)

**On this page**

1. The exit codes
2. A Makefile target that runs tests in a microVM
3. Check what would be sent before you send it
4. Branching on refusals and rate limits
5. Reading JSON output
6. Signing in on a machine without a browser
7. When to stop using the CLI in scripts
8. FAQ
9. Related

**Run your agents in a box of their own**

Each sandbox is a Firecracker microVM. Free includes 100 awake vCPU-hours a month.

[Start free](https://cloud.sandscale.dev/auth/signup)

## Keep reading

- [November 27, 2026 · **A LlamaIndex.TS data agent that runs its Python in a Sandscale sandbox** · Use LlamaIndex.TS's agent workflow with tools that write and run Python in a Firecracker sandbox, built per sandbox so a run can only reach its own box. Upload data files, answer with the numbers, clean up. Type-checked.](https://sandscale.dev/blog/llamaindex-ts-agent-with-sandscale)
- [November 27, 2026 · **A sandbox for Claude Code: running it headless in a cloud workspace** · Run Claude Code with claude -p inside a Sandscale workspace, a persistent Firecracker microVM, so its Bash tool works on a cloud disk instead of your laptop. Setup, permission flags and what stays where.](https://sandscale.dev/blog/claude-code-headless-in-a-cloud-workspace)

---
Source: https://sandscale.dev/blog/scripting-the-sandscale-cli-exit-codes-and-json
