# Run sandbox

URL: https://docs.hanzo.ai/docs/openapi/sandbox/run-in-sandbox

> Runs one command inside the caller's sandbox and answers its exit code, stdout and stderr.

`POST /v1/sandbox/run`

> [Sandbox →](https://docs.hanzo.ai/docs/openapi/sandbox) · [All API references →](https://docs.hanzo.ai/docs/openapi) · [Get started →](https://docs.hanzo.ai/docs/guides)

|               |                                        |
| ------------- | -------------------------------------- |
| **Address**   | `https://api.hanzo.ai/v1/sandbox/run`  |
| **Method**    | `POST`                                 |
| **Operation** | `run_in_sandbox`                       |
| **Auth**      | `Authorization: Bearer $HANZO_API_KEY` |

Runs one command inside the caller's sandbox and answers its exit code,
stdout and stderr. A non-zero exit is a successful call carrying a failed
program, so it comes back as data and not as an error.

Name a `session` and the command NARRATES INTO IT: its output is appended to
that session's live log as the program produces it, so anything watching the
session — GET /v1/agent/sessions/stream, scoped to one run with ?root= —
watches the work happen rather than waiting for the verdict. Without it the
call is what it always was: silent until it returns, which for an agentic run
is twenty-five minutes of blank screen.

The session is named; the TENANT is not. It is the org the caller already
proved, so a session belonging to somebody else is absent from the org this
call acts for and the append is refused there.

## Request

9 fields, body `application/json` (required).

| Field        | In   | Type            | Required | Description                                                                                                                                                                                                              |
| ------------ | ---- | --------------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `argv`       | body | string\[]       | —        | Argv is the program and its arguments, already split — the form no shell can misread.                                                                                                                                    |
| `blind`      | body | string\[]       | —        | Blind is the set of secrets this command must never publish.                                                                                                                                                             |
| `command`    | body | string          | —        | Command is a shell line, run by `sh -c`.                                                                                                                                                                                 |
| `dir`        | body | string          | —        | Dir runs the command somewhere other than the sandbox's working directory, which Leased.Workdir names.                                                                                                                   |
| `events`     | body | boolean         | —        | Events says the command writes JSON lines to stdout, one event each, as an agent harness's --json does: each is appended to Session as one `event`, in the shape the command wrote it, and stderr is narrated as output. |
| `id`         | body | string          | —        | ID is the sandbox to run in, from an earlier lease.                                                                                                                                                                      |
| `session`    | body | string          | —        | Session is the live agent session this command narrates into: its output is appended there AS IT IS PRODUCED, so every surface watching that session watches the command work instead of a blank pause.                  |
| `stdin`      | body | string          | —        | Stdin is fed to the program on standard input.                                                                                                                                                                           |
| `timeoutSec` | body | integer (int64) | —        | TimeoutSec bounds this ONE command, so a wedged program holds the caller for its own timeout rather than for the whole lease.                                                                                            |

## Response

| Status    | Body            | Meaning |
| --------- | --------------- | ------- |
| `200`     | sandbox.Ran     | ok      |
| `default` | problem-details | refused |

`200` body — 3 fields.

| Field      | In   | Type            | Always | Description                                                                                                                                                                |
| ---------- | ---- | --------------- | ------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `exitCode` | body | integer (int64) | —      | ExitCode is the PROGRAM's own status — 0 succeeded, anything else is what it returned, and a Command runs under `sh -c` so its shell's conventions apply.                  |
| `stderr`   | body | string          | —      | Stderr is standard error, kept apart from Stdout so a caller reading a program's OUTPUT is not reading its diagnostics as data.                                            |
| `stdout`   | body | string          | —      | Stdout is what the program wrote to standard output, collected whole rather than streamed — to watch it arrive instead, name a RunIn.Session and read that session's feed. |

Failure carries the platform error shape — see [Errors](https://docs.hanzo.ai/docs/errors).

## Examples

**CLI**

```bash
hanzo sandbox run
```

**SDK**

**.ts**

```ts
import { Configuration, SandboxApi } from 'hanzoai';

const api = new SandboxApi(new Configuration({ accessToken: process.env.HANZO_API_KEY }));
const { data } = await api.runInSandbox({ argv: ["<argv>"], blind: ["<blind>"] });
```

**.py**

```python
from hanzoai.cloud import ApiClient, Configuration
from hanzoai.cloud.api import SandboxApi

client = ApiClient(Configuration(access_token=os.environ["HANZO_API_KEY"]))
result = SandboxApi(client).run_in_sandbox(argv=["<argv>"], blind=["<blind>"])
```

**.go**

```go
cfg := hanzoai.NewConfiguration()
cfg.AddDefaultHeader("Authorization", "Bearer "+os.Getenv("HANZO_API_KEY"))
client := hanzoai.NewAPIClient(cfg)

resp, _, err := client.SandboxAPI.RunInSandbox(context.Background()).Execute()
if err != nil {
	return err
}
```

**.rs**

```rust
use hanzo_client::apis::{configuration::Configuration, sandbox_api};

let mut cfg = Configuration::new();
cfg.bearer_access_token = std::env::var("HANZO_API_KEY").ok();

let result = sandbox_api::run_in_sandbox(&cfg, Default::default()).await?;
```

**.java**

```java
import ai.hanzo.cloud.ApiClient;
import ai.hanzo.cloud.api.SandboxApi;

ApiClient client = new ApiClient();
client.setBearerToken(System.getenv("HANZO_API_KEY"));

var result = new SandboxApi(client).runInSandbox();
```

**HTTP**

```bash
curl -X POST https://api.hanzo.ai/v1/sandbox/run \
  -H "Authorization: Bearer $HANZO_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
       "argv": [
         "<argv>"
       ],
       "blind": [
         "<blind>"
       ]
     }'
```

**MCP**

Tool `sandbox`, op `run_in_sandbox` — POST the JSON-RPC envelope to `https://api.hanzo.ai/v1/mcp`.

```bash
curl -X POST https://api.hanzo.ai/v1/mcp \
  -H "Authorization: Bearer $HANZO_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
       "jsonrpc": "2.0",
       "id": 1,
       "method": "tools/call",
       "params": {
         "name": "sandbox",
         "arguments": {
           "op": "run_in_sandbox",
           "input": {
             "argv": [
               "<argv>"
             ],
             "blind": [
               "<blind>"
             ]
           }
         }
       }
     }'
```

---

[Sandbox API](https://docs.hanzo.ai/docs/openapi/sandbox) · [All Hanzo APIs](https://docs.hanzo.ai/docs/openapi) · [Interactive reference](https://docs.hanzo.ai/docs/openapi/reference)
