# Name and label miniVMs

Give miniVMs a name and key=value labels, so people and agents can tell them apart and select them in scripts.

A miniVM can have a **name** and **labels**. Both are optional.

- A name says what the miniVM is for, for example `fix-flaky-cache-test`. You see it in `ons ls` next to the id. `ons ls` lists only the miniVMs your API key created.
- Labels are `key=value` pairs, for example `task=cache-fix` or `team=red`. A script or an orchestrating agent lists the miniVMs that carry a label and acts on them.

We recommend that you name every miniVM you create and give it a `task` label. Nothing requires it.

> **Warning:** Every member of your organization can see the names and labels of its running and paused miniVMs. Never put a secret or personal data in a name or a label. Free metadata stays private to the API key that created the miniVM.

## Rules

|            | Name                                                                                                       | Labels                                                                                                           |
| ---------- | ---------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------- |
| How many   | At most one.                                                                                               | At most 16 per miniVM.                                                                                           |
| Length     | 1 to 64 characters.                                                                                        | Key: 1 to 63 characters. Value: at most 128 characters.                                                          |
| Characters | Characters that show as themselves: no control or invisible characters, no leading or trailing whitespace. | Key: letters, digits, `.`, `_`, `-` and `/`, starting with a letter or a digit. Value: visible ASCII, no spaces. |
| Unique     | No. Two miniVMs can have the same name.                                                                    | No.                                                                                                              |

Labels follow the same rules as [agent run labels](https://onshore.sh/docs/guides/agent-runs). A create or a change that breaks a rule is refused with `400`, and the message names the rule broken.

## Set them at create

With `ons`:

```bash
ons create --name fix-flaky-cache-test --label task=cache-fix --label team=red
```

- `--name` sets the name. `--label <key>=<value>` adds a label; repeat it for each one.
- With `--count`, every miniVM of the batch gets the same name and labels.

With the API:

```bash
curl -s https://api.onshore.sh/v2/sandboxes \
  -H "X-API-Key: $ONS_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"templateID": "base", "timeout": 600, "name": "fix-flaky-cache-test", "labels": {"task": "cache-fix", "team": "red"}}'
```

- `ONS_API_KEY` is an environment variable that holds your onshore.sh API key.
- `POST /v1/sandboxes/batch` takes `name` and `labels` too, and gives every miniVM of the batch the same values.

### With the E2B SDKs

The E2B SDKs have no name or label field, so onshore.sh reads two metadata keys:

#### Python

```python
from e2b import Sandbox

sbx = Sandbox.create(metadata={"name": "fix-flaky-cache-test", "msb.label.task": "cache-fix"})
print(sbx.get_info().metadata)
```

#### JavaScript

```javascript
import { Sandbox } from 'e2b'

const sbx = await Sandbox.create({ metadata: { name: 'fix-flaky-cache-test', 'msb.label.task': 'cache-fix' } })
console.log((await sbx.getInfo()).metadata)
```

Set `E2B_DOMAIN=onshore.sh` and `E2B_API_KEY` before you run them. See [Migrate from E2B](https://onshore.sh/docs/guides/migrate-from-e2b).

- Metadata `name` becomes the miniVM's name when it passes the name rule. Otherwise it stays plain metadata and the miniVM has no name (control or format characters, such as zero-width ones, are refused with `400`, as in any metadata).
- Metadata `msb.label.<key>` becomes the label `<key>`. An invalid one is refused with `400`. A metadata key cannot hold `/`, so a label key with `/` needs the API's `labels` field or `ons`.
- Both keys also stay in the metadata. `get_info().metadata` (`getInfo()` in JavaScript) reads them back as you sent them, and the metadata filter of `Sandbox.list` still finds the miniVM. After a rename, the metadata follows the new name and labels.
- You can give the name or a label both as a field and as a metadata key. With the same value, that is fine. With different values, the create is refused with `400`: `name set twice (field "name" and metadata "name") with different values · send one` (the same for a label). The 16-label limit counts the labels of both together.

> **Warning:** If your E2B integration already sets a metadata `name`, that value now becomes the miniVM's name, and every member of your organization can see it. Keep anything private out of it.

### Agent runs

`ons agent new` and `ons agent start --template` create a miniVM for the run: `--name` names that miniVM, and `--label` labels both the run and that miniVM. On an existing miniVM, `--label` labels the run only; label the miniVM with `ons label set` (below). Run labels are covered in [Agent runs](https://onshore.sh/docs/guides/agent-runs).

## Change them later

You can change the name and labels of a miniVM that has not ended, running or paused.

```bash
ons rename <minivm-id> review-cache-fix
ons rename <minivm-id> --clear
ons label set <minivm-id> task=cache-review stage=review
ons label rm <minivm-id> stage
```

- `<minivm-id>` is the id `ons create` printed (`ons ls` lists yours).
- `ons rename --clear` removes the name.
- `ons label set` adds or changes the labels you give. The other labels stay.
- `ons label rm` removes the labels with these keys. A key the miniVM does not have is ignored.
- Each command prints the updated miniVM, as `ons ls` does. With `--json`, it prints the same object as `ons inspect --json`.

With the API, send a JSON merge patch to `PATCH /v1/sandboxes/<minivm-id>`:

```bash
curl -s -X PATCH https://api.onshore.sh/v1/sandboxes/<minivm-id> \
  -H "X-API-Key: $ONS_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"name": "review-cache-fix", "labels": {"stage": "review", "team": null}}'
```

- A string sets the name or a label. `null` clears the name or removes a label. `"labels": null` removes every label. A field or label you leave out is kept.
- The answer is `200` with the updated miniVM, as the list returns it.
- `{}`, with nothing to change, is refused with `400`. So is a change that leaves more than 16 labels.
- Only the API key that created the miniVM can change it, as with pause and kill. Another key gets `404`, as for an unknown id. Code inside the miniVM, with its [self token](https://onshore.sh/docs/reference/api#self-token), gets `403`: a miniVM cannot rename or relabel itself.
- A miniVM that has ended answers `409`: its name and labels are read-only.

## List and filter

`ons ls` shows a `NAME` and a `LABELS` column (`-` when empty). `ons inspect <minivm-id>` shows the same row.

```bash
ons ls --label task=cache-fix
ons ls --label team=red --label task=cache-fix
ons ls --json --label task=cache-fix
```

- `--label <key>=<value>` keeps only the miniVMs with that label. Repeat it: every label must match.
- With `--json`, every item has `name` (a string, or `null`) and `labels` (an object, `{}` when empty).
- The text columns of `ons ls` changed with names and labels. Scripts should read `--json`.

With the API, add `label=<key>=<value>` to `GET /v2/sandboxes`, once per label:

```bash
curl -s "https://api.onshore.sh/v2/sandboxes?label=task=cache-fix&label=team=red" \
  -H "X-API-Key: $ONS_API_KEY"
```

- Every label must match. The same key twice with different values is refused with `400`.
- `label=` combines with `metadata`, `state`, `template` and paging.
- `label=` matches labels only: free metadata `task=cache-fix` does not match `label=task=cache-fix`.
- Every item carries `name` and `labels`.

A key lists only the miniVMs it created, with or without a label filter.

In the console, **Fleet** shows each miniVM's name and labels. Click a label to list only the miniVMs that carry it. Click others to combine them: every label must match. Remove a filter from the bar above the list. The filters are kept in the page's link, so you can share a filtered view.

## Who sees what

|                 | Who can see it                                                                                                    |
| --------------- | ----------------------------------------------------------------------------------------------------------------- |
| Name and labels | The API key that created the miniVM, and every member of your organization while the miniVM is running or paused. |
| Free metadata   | Only the API key that created the miniVM.                                                                         |

> **Warning:** Never put a secret, a token, a customer name or any other personal data in a name or a label. Put such data in neither, or keep it in free metadata if only the creating key may read it.

## Retention

A miniVM that ended keeps its name and labels, read-only, as long as the rest of its record: your organization's run data retention, 90 days after the miniVM ended (see [Data handling](https://onshore.sh/docs/security/data-handling#how-long-we-keep-it)). Until then, the API list shows them for ended miniVMs with `state=killed`. After that, they are deleted with the record.

Markdown version: https://onshore.sh/docs/guides/names-and-labels.md
