# Sandboxes
URL: https://docs.munito.ai/sandboxes

For CUDA workloads, see the [GPU sandboxes](https://docs.munito.ai/sandboxes/gpu).

<Callout type="idea">
Sandboxes are in public preview. Create a key in the [console](https://console.munito.ai/keys) and start. The API, the
limits and the [prices](https://docs.munito.ai/sandboxes/pricing) can change before general availability.
</Callout>

A CPU sandbox is a Linux microVM. It has its own kernel, disk and network. Code in one sandbox cannot reach another sandbox, unless both are in a [network](https://docs.munito.ai/sandboxes/networks) that you make.

Use a sandbox for:

- Code that you do not trust, such as code that an AI agent writes.
- A test or a build that must start clean.
- An agent that needs its own computer. See [Cloud Agents](https://docs.munito.ai/sandboxes/cloud-agents).

| Property     | Value                                                                                                                  |
| ------------ | ---------------------------------------------------------------------------------------------------------------------- |
| Base URL     | `https://api.munito.ai/sandboxes/v1`                                                                                   |
| Key variable | `MUNITO_API_KEY`                                                                                                       |
| Templates    | `base` (Debian), `ubuntu` (Ubuntu with Docker), `agent` and `desktop`. See [Templates](https://docs.munito.ai/sandboxes/templates) |
| Work folder  | `/workspace`. Each command starts there, unless you set `cwd`                                                          |

## Without code [#without-code]

The [Sandboxes console](https://console.munito.ai/console/sandboxes) creates and terminates sandboxes, opens ports in a browser, and shows SSH commands and desktop screens.
It also pauses and resumes persistent sandboxes.
Use the API for other operations.

## Install the CLI [#install-the-cli]

```bash
npm install -g @munitoai/cli
munito login
```

`munito login` asks for a key with the **Inference** permission, from
[the console](https://console.munito.ai/keys). See [the command line tool](https://docs.munito.ai/cli).
The steps below include cURL examples for use without the CLI.

## Quick start [#quick-start]

### 1. Create a sandbox [#1-create-a-sandbox]

```bash
munito create --size small --timeout 600
```

The sandbox uses the `base` template. Internet access and other optional features default to off.
Pass `--internet` for outbound connections, and `--size tiny` for one vCPU and 128 MiB, restored from a
prepared snapshot.

Keep the id it prints for the next steps:

```bash
export SANDBOX="sbx_01k8f6x7g3d2b9v5n4m1q0w8e6"
```

<Accordions>
<Accordion title="The same call with curl">
```bash
export MUNITO_API_KEY="munitoai_..."

curl -sS --fail-with-body https://api.munito.ai/sandboxes/v1/sandboxes \
  -H "Authorization: Bearer $MUNITO_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"size": "small", "timeout": 600}'
```
</Accordion>
</Accordions>

### 2. Run a command [#2-run-a-command]

```bash
munito exec $SANDBOX "python3 -c 'print(6 * 7)'"
```

`munito run "<command>"` does the same in a throwaway sandbox: it creates one, runs the command, prints the
output and deletes the machine.

<Accordions>
<Accordion title="The same call with curl">
```bash
curl -sS --fail-with-body "https://api.munito.ai/sandboxes/v1/sandboxes/$SANDBOX/exec" \
  -H "Authorization: Bearer $MUNITO_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"command": "python3 -c \"print(6 * 7)\""}'
```

```json
{ "exit_code": 0, "stdout": "42\n", "stderr": "", "duration_ms": 35, "timed_out": false, "truncated": false }
```
</Accordion>
</Accordions>

- A string runs in `bash -lc`. A list, such as `["python3", "-V"]`, is the argument vector.
- To get events while the command runs, set `"stream": true`. The API sends NDJSON: `stdout` and `stderr` events, then one `exit` event.
- To create a sandbox and run a command in one request, set `command` in the create. The answer gives the result in `command`. This command runs for 60 seconds or less.

### 3. Write, read and list files [#3-write-read-and-list-files]

```bash
echo 'print("Hello from the sandbox")' | munito files write $SANDBOX /workspace/hello.py
munito files read $SANDBOX /workspace/hello.py
munito files ls $SANDBOX /workspace
```

<Accordions>
<Accordion title="The same calls with curl">
```bash
curl -sS --fail-with-body -X PUT \
  "https://api.munito.ai/sandboxes/v1/sandboxes/$SANDBOX/files/content?path=/workspace/hello.py" \
  -H "Authorization: Bearer $MUNITO_API_KEY" \
  -H "Content-Type: application/octet-stream" \
  --data-binary 'print("Hello from the sandbox")'

curl -sS --fail-with-body \
  "https://api.munito.ai/sandboxes/v1/sandboxes/$SANDBOX/files/content?path=/workspace/hello.py" \
  -H "Authorization: Bearer $MUNITO_API_KEY"

curl -sS --fail-with-body \
  "https://api.munito.ai/sandboxes/v1/sandboxes/$SANDBOX/files?path=/workspace" \
  -H "Authorization: Bearer $MUNITO_API_KEY"
```
</Accordion>
</Accordions>

- A write creates the parent folders. It replaces a file that exists.
- `munito files write` reads the content from standard input. To send a local file, add `--from <file>`.
- `munito files read` prints the bytes of the file. To save them, add `> <file>`.
- `munito files ls` lists `/workspace` when you give no folder.

### 4. Find your sandboxes again [#4-find-your-sandboxes-again]

```bash
munito ls
```

`metadata` holds your own keys and values. Set them when you create a sandbox.
Pass each pair as `key=value` to `GET /sandboxes` to list sandboxes that match every pair:

<Accordions>
<Accordion title="Filter by metadata with curl">
```bash
curl -sS --fail-with-body \
  "https://api.munito.ai/sandboxes/v1/sandboxes?metadata=team%3Dgreen&metadata=job%3D42" \
  -H "Authorization: Bearer $MUNITO_API_KEY"
```
</Accordion>
</Accordions>

### 5. Keep the sandbox longer [#5-keep-the-sandbox-longer]

```bash
munito timeout $SANDBOX 3600
```

<Accordions>
<Accordion title="The same call with curl">
```bash
curl -sS --fail-with-body -X POST "https://api.munito.ai/sandboxes/v1/sandboxes/$SANDBOX/timeout" \
  -H "Authorization: Bearer $MUNITO_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"timeout": 3600}'
```
</Accordion>
</Accordions>

- The sandbox gets a new end, 3,600 seconds from the call. The answer gives the time in `expires_at`.
- A timeout has a [limit](https://docs.munito.ai/sandboxes/pricing#limits). To run longer, set a new timeout before the end.
- At the end of its timeout, a sandbox stops. A [persistent](https://docs.munito.ai/sandboxes/persistence#pause-and-resume) sandbox pauses.

### 6. Delete the sandbox [#6-delete-the-sandbox]

```bash
munito rm $SANDBOX
```

<Accordions>
<Accordion title="The same call with curl">
```bash
curl -sS --fail-with-body -X DELETE "https://api.munito.ai/sandboxes/v1/sandboxes/$SANDBOX" \
  -H "Authorization: Bearer $MUNITO_API_KEY"
```
</Accordion>
</Accordions>

The sandbox stops at once, and its files are deleted. `GET /sandboxes/{sandbox_id}` still shows the sandbox, with the `status` `stopped`.

## Report abuse [#report-abuse]

The ports and SSH of the sandboxes have addresses under `munito.run`, such as `https://8080-<id>.munito.run`. The code at these addresses belongs to the customers of Munito AI.

To report phishing, malware or other abuse at such an address, send its full URL to security@munito.ai.