> ## Documentation Index
> Fetch the complete documentation index at: https://docs.stackshift.cloud/llms.txt
> Use this file to discover all available pages before exploring further.

# Run code in one call

> Send code and a command, get back the exit code and output. StackShift starts a fresh sandbox, runs it and deletes the sandbox, all in one request.

> [!TIP]
>
> **Live.** This area is documented as current, user-reliable behavior.

## Goal

Run a command, a script or a few files in a clean, isolated machine and get the result back without managing a sandbox yourself.

## Prerequisites

* A StackShift API key, a signed-in CLI, or a connected agent allowed to run code in short-lived sandboxes.
* A sandbox template reference with its digest, such as `TEMPLATE@sha256:DIGEST`. Copy it from Sandbox templates in the dashboard.

## Workflow

**Step 1**

Pick the template that has the language and tools your code needs.

**Step 2**

Give exactly one of `command` (a shell line), `argv` (a program and its arguments, no shell) or `script` (text to pipe into `interpreter`).

**Step 3**

Add any `files` the code needs. They are written into `/workspace` before the command starts.

**Step 4**

Send `POST /api/v1/sandboxes/run`, or use `stackshift sandbox run`, an SDK or the `sandbox_run` tool.

**Step 5**

Read `exit_code`, `stdout` and `stderr` from the answer. The sandbox is already gone.

## Run or keep

A one-shot run is the quickest way to run code: there is nothing to create first and nothing to clean up after. Use it for tests, scripts, code an agent wrote, and checks in a pipeline.

Keep a sandbox when you need files or processes to carry over between calls, and use a Computer when you want a whole machine that stays, pauses and comes back where it was.

## The sandbox is always deleted

Every run creates a new sandbox and deletes it before the answer comes back, whatever happened: the code succeeded, exited with an error, ran out of time, a file could not be written, or the sandbox never became ready. It is also deleted if your connection drops while the run is in progress.

Nothing from one run is visible to the next. Each run starts from the clean template.

## Request

* `template` (required): the template reference with its digest.
* `profile`: the size of the sandbox, such as `ci`, `customer_code` or `plugin`. Defaults to `ci`.
* `command`: a shell line, such as `npm test && npm run lint`.
* `argv`: a program and its arguments, run without a shell, such as `["python3", "main.py"]`.
* `script` and `interpreter`: script text passed on standard input to the interpreter, such as `python3` or `node`. The interpreter defaults to `sh` and is only accepted with `script`.
* `files`: up to 100 files with `path` and text `content`, 8 MB in total. Paths are inside `/workspace`.
* `environment`: variables for the command, as an object of names and values.
* `cwd`: the working directory. Defaults to `/workspace`.
* `timeout_seconds`: how long the command may run, from 1 to 600. Defaults to 60.

```bash Run a Python file theme={null}
curl -X POST https://api.stackshift.cloud/api/v1/sandboxes/run \
  -H "Authorization: Bearer $STACKSHIFT_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "template": "TEMPLATE@sha256:DIGEST",
    "files": [{ "path": "main.py", "content": "print(sum(range(10)))" }],
    "command": "python3 main.py",
    "timeout_seconds": 30
  }'
```

## Answer

* `state`: `succeeded`, `failed`, `timed_out`, `oom_killed` or `cancelled`.
* `exit_code`: the command’s exit code. It is left out when the command was stopped before it could exit, for example when it ran out of time.
* `stdout` and `stderr`: the first 64 KB of each stream. `stdout_truncated` and `stderr_truncated` say whether more was cut off.
* `start_ms`: milliseconds until the sandbox was ready to run code. `duration_ms`: milliseconds for the whole run, including writing files.
* `sandbox_id` and `execution_id`: identifiers for support and your own logs. The sandbox no longer exists.

```json theme={null}
{
  "sandbox_id": "0f6c1d4e-5b2a-4c8e-9f3a-2d7b8e1c4a90",
  "execution_id": "7a2e9c41-3d5f-4b6a-8e1c-9f0d2b3a4c51",
  "state": "succeeded",
  "exit_code": 0,
  "stdout": "45\n",
  "stderr": "",
  "stdout_truncated": false,
  "stderr_truncated": false,
  "start_ms": 640,
  "duration_ms": 1210
}
```

## From the SDKs

The Go, TypeScript and Python sandbox SDKs all have a `run` call that sends the same request and waits for the answer. The Go and Python clients wait up to five minutes longer than your timeout, so a long run is not cut off by the client; the TypeScript client waits until the answer arrives.

```go Go theme={null}
client, err := sandbox.New(sandbox.Options{
	BaseURL: "https://api.stackshift.cloud",
	Token:   os.Getenv("STACKSHIFT_API_KEY"),
})
if err != nil {
	return err
}
result, err := client.Sandboxes.Run(ctx, sandbox.RunInput{
	Template:       "TEMPLATE@sha256:DIGEST",
	Files:          []sandbox.RunFile{{Path: "main.py", Content: "print(sum(range(10)))"}},
	Command:        "python3 main.py",
	TimeoutSeconds: 30,
})
if err != nil {
	return err
}
fmt.Print(result.Stdout)
```

```go Go theme={null}
client, err := sandbox.New(sandbox.Options{
	BaseURL: "https://api.stackshift.cloud",
	Token:   os.Getenv("STACKSHIFT_API_KEY"),
})
if err != nil {
	return err
}
result, err := client.Sandboxes.Run(ctx, sandbox.RunInput{
	Template:       "TEMPLATE@sha256:DIGEST",
	Files:          []sandbox.RunFile{{Path: "main.py", Content: "print(sum(range(10)))"}},
	Command:        "python3 main.py",
	TimeoutSeconds: 30,
})
if err != nil {
	return err
}
fmt.Print(result.Stdout)
```

```typescript TypeScript theme={null}
import { StackshiftSandboxClient } from '@stackshift-cloud/sandbox'

const client = new StackshiftSandboxClient({
  baseUrl: 'https://api.stackshift.cloud',
  token: process.env.STACKSHIFT_API_KEY,
})

const result = await client.sandboxes.run({
  template: 'TEMPLATE@sha256:DIGEST',
  script: 'console.log(21 * 2)',
  interpreter: 'node',
  timeout_seconds: 30,
})
console.log(result.exit_code, result.stdout)
```

```python Python theme={null}
import os
from stackshift_sandbox import StackshiftSandboxClient

client = StackshiftSandboxClient("https://api.stackshift.cloud", token=os.environ["STACKSHIFT_API_KEY"])

result = client.sandboxes.run({
    "template": "TEMPLATE@sha256:DIGEST",
    "files": [{"path": "main.py", "content": "print(sum(range(10)))"}],
    "argv": ["python3", "main.py"],
    "timeout_seconds": 30,
})
print(result["exit_code"], result["stdout"])
```

## From the CLI

`stackshift sandbox run` copies local files in, prints the command’s output to your terminal and exits with the command’s exit code. See the CLI reference for every flag.

```bash theme={null}
stackshift sandbox run --template TEMPLATE@sha256:DIGEST --file main.py=./main.py -- python3 main.py
stackshift sandbox run --template TEMPLATE@sha256:DIGEST --script ./check.sh
```

## From a connected agent

Agents connected over MCP use the `sandbox_run` tool. It takes the same fields as the request above and runs at once, without asking you first, so it needs its own permission: tick “Run code in a short-lived sandbox” when you connect the agent or under Change access.

Without that permission the tool answers `insufficient_scope` for `sandbox.run`, and the agent can ask you to add it.

## Start times

The Sandboxes and Computers pages show how long machines take to be ready, split into warm starts, which use a machine we already prepared, and cold starts, which boot a new one. Each shows the typical time and the time 95 out of 100 starts finish within.

`GET /api/v1/sandboxes/start-stats` returns the same numbers. Each item has `class` (`sandbox` or `computer`), `start` (`warm` or `cold`), `nodes`, `samples`, `p50_ms`, `p95_ms` and `max_ms`.

## What you pay for

A run is billed like any sandbox of its profile, for the seconds it exists, from start until it is deleted. There is no charge after the answer comes back.

## Expected result

> [!TIP]
>
> One answer with the exit code, up to 64 KB of standard output and 64 KB of standard error, how long the sandbox took to be ready and how long the whole run took.

## Common failures

> [!WARNING]
>
> * `400 invalid_request` with “give exactly one of command, argv or script”. Send only one of the three.
> * `400 invalid_request` with “timeout\_seconds must be between 1 and 600”. Ask for 600 seconds or less, or use a sandbox you keep for longer work.
> * `400 invalid_request` with “files exceed 100 files or 8388608 bytes”. Send fewer or smaller files, or download larger inputs from inside the command.
> * `400 invalid_request` with “unknown sandbox profile”. Use a profile from `GET /api/v1/sandboxes/catalog`, such as `ci`.
> * `502 run_failed` with “the sandbox did not start”. Nothing ran and the sandbox was deleted. Try again; if it keeps failing, check the template on the Sandbox templates page.
> * `stdout_truncated` or `stderr_truncated` is `true`. Only the first 64 KB of that stream came back. Write large output to a file and print a summary instead.

## Related guides

> [card] [Computers: a cloud computer for any agent](https://docs.stackshift.cloud/computers/overview.md)
>
> A Computer is a full Linux machine, with or without a desktop, that an agent or a person can create, drive, pause, fork and delete from anywhere.

> [card] [Computers CLI reference](https://docs.stackshift.cloud/computers/cli.md)
>
> Every `stackshift computers` command: open a shell, create, list, run commands, move files, publish ports, fork, pause, snapshot and manage keys.

> [card] [stackshift sandbox run](https://docs.stackshift.cloud/cli/sandbox-run.md)
>
> Run a command or script in a fresh sandbox, print its output and delete the sandbox, in one command.

> [card] [Connection access and approvals](https://docs.stackshift.cloud/ai-features/agent-connection-access.md)
>
> Understand selected resources, permitted actions, owner decisions and revocation.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.