# Operations (/docs/operations)

> For AI agents: the complete documentation index is at https://www.daytona.com/llms.txt.

Operations are the reads and updates made on resources that already exist. A list reads a scope page by page, and a get reads one resource by its identifier.

Operations on sandboxes work the same on every sandbox type, whether the sandbox is running or stopped. Each operation reaches only the sandboxes of one project, the project that owns the API key of the request.

Use an operation to find a sandbox, read its state, or change its labels after it is created. To create a sandbox, see [Sandboxes](https://www.daytona.com/docs/sandboxes.md#create-sandboxes), and to change its state, see [Lifecycle](https://www.daytona.com/docs/lifecycle.md).

## List sandboxes [#list-sandboxes]

List sandboxes to see the sandboxes of a project with their IDs, names, and states. A paginated list returns a large collection in pages, and each page continues where the last one ended.

A list of sandboxes covers one project and returns its sandboxes one page at a time.

Use a list to find a sandbox whose ID you do not know, and [search](#search-sandboxes) to filter by name, state, class, labels, or resources. To read your own writes, such as a sandbox created a moment ago, use [get](#get-a-sandbox).

**Python**

```python title="main.py"
from daytona import Daytona

daytona = Daytona()
page = daytona.v2.list()
for sandbox in page.items:
    print(sandbox.id, sandbox.name, sandbox.state)
```

| **Parameter** | **Description** |
| --- | --- |
| `cursor` | Continues from a previous page.<br />**Format:** the next_cursor of a previous page |
| `limit` | Sets the page size.<br />**Default:** `100`<br />**Minimum:** `1`<br />**Maximum:** `200` |

**TypeScript**

```typescript title="index.ts"
import { Daytona } from '@daytona/sdk'

const daytona = new Daytona()
const page = await daytona.v2.list()
for (const sandbox of page.items) {
  console.log(sandbox.id, sandbox.name, sandbox.state)
}
```

| **Parameter** | **Description** |
| --- | --- |
| `cursor` | Continues from a previous page.<br />**Format:** the nextCursor of a previous page |
| `limit` | Sets the page size.<br />**Default:** `100`<br />**Minimum:** `1`<br />**Maximum:** `200` |

**Go**

```go title="main.go"
package main

import (
	"context"
	"fmt"
	"log"

	"go.daytona.com/sdk/v2"
)

func main() {
	ctx := context.Background()
	client, err := daytona.New()
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	page, err := client.List(ctx, nil)
	if err != nil {
		log.Fatal(err)
	}
	for _, sandbox := range page.Items {
		fmt.Println(sandbox.ID(), sandbox.Name(), sandbox.State())
	}
}
```

| **Parameter** | **Description** |
| --- | --- |
| `Cursor` | Continues from a previous page.<br />**Format:** the NextCursor of a previous page |
| `Limit` | Sets the page size.<br />**Default:** `100`<br />**Minimum:** `1`<br />**Maximum:** `200` |

**CLI**

```bash title="Terminal"
daytona sandbox list
```

| **Flag** | **Description** |
| --- | --- |
| `--cursor` | Continues from a previous page.<br />**Format:** the cursor that the previous page prints |
| `--limit` | Sets the page size.<br />**Default:** `100`<br />**Minimum:** `1`<br />**Maximum:** `200` |
| `--region` | Lists only the sandboxes in this region or a region inside it.<br />**Default:** every region<br />**Value:** `earth`, `us`, `us-east`, `us-east-ash-1`, `us-east-bos-1`, `us-east-iad-1`, or the ID of a [BYOC region](https://www.daytona.com/docs/bring-your-own-compute.md) of the organization<br />**Example:** `us-east-ash-1` |

**API**

```bash title="Terminal"
curl 'https://api.daytona.com/sandboxes' \
  --header 'Authorization: Bearer YOUR_API_KEY'
```

| **Field** | **Description** |
| --- | --- |
| `cursor` | Continues from a previous page.<br />**Format:** the next_cursor of a previous page |
| `limit` | Sets the page size.<br />**Default:** `100`<br />**Minimum:** `1`<br />**Maximum:** `200` |


### Iterate over all sandboxes [#iterate-over-all-sandboxes]

Iterate over all sandboxes to read every sandbox of the project without handling page cursors. Iteration over a paginated list reads one page after another until no page remains.

Iteration over the sandboxes of a project yields the sandboxes of every page in turn, and it ends after the last page, when the server returns no cursor.

Use iteration when a script needs every sandbox of the project rather than one page. To find only some of them, [search](#search-sandboxes) with filters instead.

**Python**

```python title="main.py"
from daytona import Daytona

daytona = Daytona()
for sandbox in daytona.v2.iter():
    print(sandbox.id, sandbox.name, sandbox.state)
```

**TypeScript**

```typescript title="index.ts"
import { Daytona } from '@daytona/sdk'

const daytona = new Daytona()
for await (const sandbox of daytona.v2.iter()) {
  console.log(sandbox.id, sandbox.name, sandbox.state)
}
```

**Go**

```go title="main.go"
package main

import (
	"context"
	"fmt"
	"log"

	"go.daytona.com/sdk/v2"
)

func main() {
	ctx := context.Background()
	client, err := daytona.New()
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	for sandbox, err := range client.ListAll(ctx, nil) {
		if err != nil {
			log.Fatal(err)
		}
		fmt.Println(sandbox.ID(), sandbox.Name(), sandbox.State())
	}
}
```

**CLI**

```bash title="Terminal"
# Follow every page until the server returns no cursor
daytona sandbox list --all
```

| **Flag** | **Description** |
| --- | --- |
| `--all` | Follows every page until the last one. |


## Search sandboxes [#search-sandboxes]

Search sandboxes to find the ones that match a text query or a set of filters. A search matches stored resources against the query and filters and returns them sorted, one page at a time.

A search of sandboxes covers the project in every region and reads an eventually consistent central index, not the sandboxes themselves. Deleted sandboxes are left out of the results.

Use a search to find sandboxes by text, or by their state, class, labels, or persistence. Filters combine with AND. A create, update, or delete becomes searchable about 2 to 3 seconds after it is committed, so a sandbox created a moment ago may be missing. To read your own writes, use [get](#get-a-sandbox).

**Python**

```python title="main.py"
from daytona import Daytona

daytona = Daytona()

# Free text over name and image, or an exact sandbox id
page = daytona.v2.search(q="my-sandbox")
for sandbox in page.items:
    print(sandbox.id, sandbox.name, sandbox.state)
```

| **Parameter** | **Description** |
| --- | --- |
| `cursor` | Continues from a previous page.<br />**Format:** the next_cursor of a previous page with the same filters and sort |
| `limit` | Sets the page size.<br />**Default:** `100`<br />**Minimum:** `1`<br />**Maximum:** `200` |
| `order` | Sets the sort order.<br />**Default:** `desc`<br />**Value:** `asc` or `desc`<br />**Example:** `"asc"` |
| `persistent` | Filters by persistence.<br />**Value:** `True` for persistent sandboxes, `False` for ephemeral ones |
| `q` | Matches free text against the name and image, or one sandbox ID exactly.<br />**Example:** `"my-sandbox"` |
| `sort` | Sets the sort field.<br />**Default:** `created_at`<br />**Value:** `created_at`, `updated_at`, `last_activity_at`, `name`, `cores`, `memory_mib`, or `disk_mib`<br />**Example:** `"name"` |
| `states` | Filters by state.<br />**Format:** one state or a list of states<br />**Value:** `queued`, `creating`, `starting`, `started`, `pausing`, `paused`, `stopping`, `stopped`, `destroying`, `destroyed`, or `error`<br />**Example:** `"started"` |

**TypeScript**

```typescript title="index.ts"
import { Daytona } from '@daytona/sdk'

const daytona = new Daytona()

// Free text over name and image, or an exact sandbox id
const page = await daytona.v2.search({ q: 'my-sandbox' })
for (const sandbox of page.items) {
  console.log(sandbox.id, sandbox.name, sandbox.state)
}
```

| **Parameter** | **Description** |
| --- | --- |
| `cursor` | Continues from a previous page.<br />**Format:** the nextCursor of a previous page with the same filters and sort |
| `limit` | Sets the page size.<br />**Default:** `100`<br />**Minimum:** `1`<br />**Maximum:** `200` |
| `order` | Sets the sort order.<br />**Default:** `desc`<br />**Value:** `asc` or `desc`<br />**Example:** `'asc'` |
| `persistent` | Filters by persistence.<br />**Value:** `true` for persistent sandboxes, `false` for ephemeral ones |
| `q` | Matches free text against the name and image, or one sandbox ID exactly.<br />**Example:** `'my-sandbox'` |
| `sort` | Sets the sort field.<br />**Default:** `created_at`<br />**Value:** `created_at`, `updated_at`, `last_activity_at`, `name`, `cores`, `memory_mib`, or `disk_mib`<br />**Example:** `'name'` |
| `states` | Filters by state.<br />**Format:** one state or a list of states<br />**Value:** `queued`, `creating`, `starting`, `started`, `pausing`, `paused`, `stopping`, `stopped`, `destroying`, `destroyed`, or `error`<br />**Example:** `'started'` |

**Go**

```go title="main.go"
package main

import (
	"context"
	"fmt"
	"log"

	"go.daytona.com/sdk/v2"
)

func main() {
	ctx := context.Background()
	client, err := daytona.New()
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	// Free text over name and image, or an exact sandbox id
	page, err := client.Search(ctx, &daytona.SearchSandboxesOptions{Q: "my-sandbox"})
	if err != nil {
		log.Fatal(err)
	}
	for _, sandbox := range page.Items {
		fmt.Println(sandbox.ID(), sandbox.Name(), sandbox.State())
	}
}
```

| **Parameter** | **Description** |
| --- | --- |
| `Cursor` | Continues from a previous page.<br />**Format:** the NextCursor of a previous page with the same filters and sort |
| `Limit` | Sets the page size.<br />**Default:** `100`<br />**Minimum:** `1`<br />**Maximum:** `200` |
| `Order` | Sets the sort order.<br />**Default:** `daytona.SortOrderDescending`<br />**Value:** `daytona.SortOrderAscending` or `daytona.SortOrderDescending`<br />**Example:** `daytona.SortOrderAscending` |
| `Persistent` | Filters by persistence.<br />**Format:** a pointer to a bool<br />**Value:** `true` for persistent sandboxes, `false` for ephemeral ones |
| `Q` | Matches free text against the name and image, or one sandbox ID exactly.<br />**Example:** `"my-sandbox"` |
| `Sort` | Sets the sort field.<br />**Default:** `daytona.SearchSortCreatedAt`<br />**Value:** `daytona.SearchSortCreatedAt`, `daytona.SearchSortUpdatedAt`, `daytona.SearchSortLastActivityAt`, `daytona.SearchSortName`, `daytona.SearchSortCores`, `daytona.SearchSortMemoryMiB`, or `daytona.SearchSortDiskMiB`<br />**Example:** `daytona.SearchSortName` |
| `States` | Filters by state.<br />**Format:** a non-empty list of states<br />**Value:** `daytona.SandboxStateQueued`, `daytona.SandboxStateCreating`, `daytona.SandboxStateStarting`, `daytona.SandboxStateStarted`, `daytona.SandboxStatePausing`, `daytona.SandboxStatePaused`, `daytona.SandboxStateStopping`, `daytona.SandboxStateStopped`, `daytona.SandboxStateDestroying`, `daytona.SandboxStateDestroyed`, or `daytona.SandboxStateError`<br />**Example:** `[]daytona.SandboxState{daytona.SandboxStateStarted}` |

**CLI**

```bash title="Terminal"
# Free text over name and image, or an exact sandbox id
daytona sandbox search my-sandbox
```

| **Flag** | **Description** |
| --- | --- |
| `<query>` | Matches free text against the name and image, or one sandbox ID exactly.<br />**Example:** `my-sandbox` |
| `--class` | Filters by sandbox class.<br />**Format:** a comma-separated list, or the flag repeated<br />**Value:** `linux` or `macos`<br />**Example:** `linux` |
| `--cursor` | Continues from a previous page.<br />**Format:** the cursor that a previous page with the same filters and sort prints |
| `--limit` | Sets the page size.<br />**Default:** `100`<br />**Minimum:** `1`<br />**Maximum:** `200` |
| `--order` | Sets the sort order.<br />**Default:** `desc`<br />**Value:** `asc` or `desc`<br />**Example:** `asc` |
| `--persistent` | Filters by persistence.<br />**Value:** `true` for persistent sandboxes, `false` for ephemeral ones |
| `--sort` | Sets the sort field.<br />**Default:** `created_at`<br />**Value:** `created_at`, `updated_at`, `last_activity_at`, `name`, `cores`, `memory_mib`, or `disk_mib`<br />**Example:** `name` |
| `--status` | Filters by API status.<br />**Format:** a comma-separated list, or the flag repeated<br />**Value:** `queued`, `pending`, `assigned`, `booting`, `running`, `pausing`, `paused`, `stopping`, `stopped`, `deleting`, `deleted`, `failed`, or `lost`<br />**Example:** `running` |

**API**

```bash title="Terminal"
# Free text over name and image, or an exact sandbox id
curl 'https://api.daytona.com/sandboxes/search?q=my-sandbox' \
  --header 'Authorization: Bearer YOUR_API_KEY'
```

| **Field** | **Description** |
| --- | --- |
| `cursor` | Continues from a previous page.<br />**Format:** the next_cursor of a previous page with the same filters and sort |
| `limit` | Sets the page size.<br />**Default:** `100`<br />**Minimum:** `1`<br />**Maximum:** `200` |
| `order` | Sets the sort order.<br />**Default:** `desc`<br />**Value:** `asc` or `desc`<br />**Example:** `asc` |
| `persistent` | Filters by persistence.<br />**Value:** `true` for persistent sandboxes, `false` for ephemeral ones |
| `q` | Matches free text against the name and image, or one sandbox ID exactly.<br />**Example:** `my-sandbox` |
| `sort` | Sets the sort field.<br />**Default:** `created_at`<br />**Value:** `created_at`, `updated_at`, `last_activity_at`, `name`, `cores`, `memory_mib`, or `disk_mib`<br />**Example:** `name` |
| `status` | Filters by API status.<br />**Format:** a comma-separated list<br />**Value:** `queued`, `pending`, `assigned`, `booting`, `running`, `pausing`, `paused`, `stopping`, `stopped`, `deleting`, `deleted`, `failed`, or `lost`<br />**Example:** `running` |


### Iterate over search results [#iterate-over-search-results]

Iterate over search results to read every sandbox that a search matches without handling page cursors. Iteration over a paginated search reads one page after another until no page remains.

Iteration over a search of sandboxes takes the same filters, sort, and order as the search, and it ends after the last page, when the server returns no cursor. The results stay eventually consistent, as in a search.

Use iteration when a script needs every match rather than one page. To read every sandbox of the project without filters, [iterate over all sandboxes](#iterate-over-all-sandboxes) instead.

**Python**

```python title="main.py"
from daytona import Daytona

daytona = Daytona()

# Follow every page of the search
for sandbox in daytona.v2.search_iter(q="my-sandbox"):
    print(sandbox.id, sandbox.name, sandbox.state)
```

| **Parameter** | **Description** |
| --- | --- |
| `q` | Matches free text against the name and image, or one sandbox ID exactly.<br />**Example:** `"my-sandbox"` |

**TypeScript**

```typescript title="index.ts"
import { Daytona } from '@daytona/sdk'

const daytona = new Daytona()

// Follow every page of the search
for await (const sandbox of daytona.v2.searchIter({ q: 'my-sandbox' })) {
  console.log(sandbox.id, sandbox.name, sandbox.state)
}
```

| **Parameter** | **Description** |
| --- | --- |
| `q` | Matches free text against the name and image, or one sandbox ID exactly.<br />**Example:** `'my-sandbox'` |

**Go**

```go title="main.go"
package main

import (
	"context"
	"fmt"
	"log"

	"go.daytona.com/sdk/v2"
)

func main() {
	ctx := context.Background()
	client, err := daytona.New()
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	// Follow every page of the search
	for sandbox, err := range client.SearchAll(ctx, &daytona.SearchSandboxesOptions{Q: "my-sandbox"}) {
		if err != nil {
			log.Fatal(err)
		}
		fmt.Println(sandbox.ID(), sandbox.Name(), sandbox.State())
	}
}
```

| **Parameter** | **Description** |
| --- | --- |
| `Q` | Matches free text against the name and image, or one sandbox ID exactly.<br />**Example:** `"my-sandbox"` |

**CLI**

```bash title="Terminal"
# Follow every page until the server returns no cursor
daytona sandbox search my-sandbox --all
```

| **Flag** | **Description** |
| --- | --- |
| `<query>` | Matches free text against the name and image, or one sandbox ID exactly.<br />**Example:** `my-sandbox` |
| `--all` | Follows every page until the last one. |


## Get a sandbox [#get-a-sandbox]

Get a sandbox by its ID to read its current state, resources, labels, and lifecycle settings. A get reads one resource by its identifier and changes nothing on the server.

A get of a sandbox reads it from the control plane directly, not from the search index, so the result reflects a change as soon as the change is made.

Use a get to read a sandbox right after a change, which [search](#search-sandboxes) can miss for a few seconds. Store the sandbox ID, or the sandbox data as JSON, to get the sandbox again later.

**Python**

```python title="main.py"
from daytona import Daytona

daytona = Daytona()

# Fetch the sandbox from the control plane
sandbox = daytona.v2.get("{sandboxId}")
print(sandbox.state, sandbox.cpu, sandbox.memory, sandbox.disk)

# Store the sandbox data as JSON; it carries no URL or credential
saved = sandbox.to_json()  # {"sandboxId": "...", "name": "...", "state": "...", ...}

# Later, get the sandbox again by its ID
sandbox = daytona.v2.get(saved["sandboxId"])
```

| **Parameter** | **Description** |
| --- | --- |
| `sandbox_id` | Sets the sandbox to get.<br />**Required**<br />**Format:** a sandbox ID |
| `toolbox_url` | Sets the URL that the sandbox sends its toolbox requests and its credential to, in place of the [sandbox toolbox URL](https://www.daytona.com/docs/sandboxes.md#toolbox). Pass only a URL you control.<br />**Default:** the sandbox toolbox URL<br />**Format:** an HTTP or HTTPS URL without a query or fragment that already holds the sandbox ID, or that holds `{sandbox_id}` or `{sandboxId}` in its path, which the SDK replaces with the sandbox ID<br />**Example:** `"https://proxy.example.com/toolbox/{sandbox_id}"` |

**TypeScript**

```typescript title="index.ts"
import { Daytona } from '@daytona/sdk'

const daytona = new Daytona()

// Fetch the sandbox from the control plane
const sandbox = await daytona.v2.get('{sandboxId}')
console.log(sandbox.state, sandbox.cpu, sandbox.memory, sandbox.disk)

// Store the sandbox data as JSON; it carries no URL or credential
const saved = sandbox.toJSON() // { sandboxId: '...', name: '...', state: '...', ... }

// Later, get the sandbox again by its ID
const again = await daytona.v2.get(saved.sandboxId)
```

| **Parameter** | **Description** |
| --- | --- |
| `id` | Sets the sandbox to get.<br />**Required**<br />**Format:** a sandbox ID |
| `toolboxUrl` | Sets the URL that the sandbox sends its toolbox requests and its credential to, in place of the [sandbox toolbox URL](https://www.daytona.com/docs/sandboxes.md#toolbox). Pass only a URL you control.<br />**Default:** the sandbox toolbox URL<br />**Format:** an HTTP or HTTPS URL without a query or fragment that already holds the sandbox ID, or that holds `{sandboxId}` or `{sandbox_id}` in its path, which the SDK replaces with the sandbox ID<br />**Example:** `'https://proxy.example.com/toolbox/{sandboxId}'` |

**Go**

```go title="main.go"
package main

import (
	"context"
	"fmt"
	"log"

	"go.daytona.com/sdk/v2"
)

func main() {
	ctx := context.Background()
	client, err := daytona.New()
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	// Fetch the sandbox from the control plane
	sandbox, err := client.Get(ctx, "{sandboxId}", nil)
	if err != nil {
		log.Fatal(err)
	}
	info := sandbox.Info()
	fmt.Println(info.State, info.Resources.CPU, info.Resources.MemoryGiB, info.Resources.DiskGiB)

	// Store the sandbox data as JSON; it carries no URL or credential
	saved := sandbox.ToJSON() // {SandboxID: "...", Name: "...", State: "...", ...}

	// Later, get the sandbox again by its ID
	sandbox, err = client.Get(ctx, saved.SandboxID, nil)
	if err != nil {
		log.Fatal(err)
	}
}
```

| **Parameter** | **Description** |
| --- | --- |
| `id` | Sets the sandbox to get.<br />**Required**<br />**Format:** a sandbox ID |
| `ToolboxURL` | Sets the URL that the sandbox sends its toolbox requests and its credential to, in place of the [sandbox toolbox URL](https://www.daytona.com/docs/sandboxes.md#toolbox). Pass only a URL you control.<br />**Default:** the sandbox toolbox URL<br />**Format:** an HTTP or HTTPS URL without a query or fragment that already holds the sandbox ID, or that holds `{sandboxId}` or `{sandbox_id}` in its path, which the SDK replaces with the sandbox ID<br />**Example:** `"https://proxy.example.com/toolbox/{sandboxId}"` |

**CLI**

```bash title="Terminal"
daytona sandbox get {sandboxId}
```

**API**

```bash title="Terminal"
curl 'https://api.daytona.com/sandboxes/{sandboxId}' \
  --header 'Authorization: Bearer YOUR_API_KEY'
```


### Refresh a sandbox [#refresh-a-sandbox]

Refresh a sandbox to update a local object with the current data of the sandbox. A refresh replaces the fields that a client holds with the values the server holds now.

A refresh of a sandbox reads it from the control plane and updates the object in place. Until then, the object keeps the fields from the request that returned it, so its state, resources, and labels can be out of date after a change made by another client.

Refresh a sandbox before you act on its state or labels when another client may have changed them. Lifecycle operations such as start and stop refresh the object when they finish.

**Python**

```python title="main.py"
from daytona import Daytona

daytona = Daytona()
sandbox = daytona.v2.create()

# Re-read the sandbox from the control plane
sandbox.refresh_data()
print(sandbox.state, sandbox.labels)
```

**TypeScript**

```typescript title="index.ts"
import { Daytona } from '@daytona/sdk'

const daytona = new Daytona()
const sandbox = await daytona.v2.create()

// Re-read the sandbox from the control plane
await sandbox.refreshData()
console.log(sandbox.state, sandbox.labels)
```

**Go**

```go title="main.go"
package main

import (
	"context"
	"fmt"
	"log"

	"go.daytona.com/sdk/v2"
)

func main() {
	ctx := context.Background()
	client, err := daytona.New()
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	sandbox, err := client.Create(ctx, "", nil)
	if err != nil {
		log.Fatal(err)
	}

	// Re-read the sandbox from the control plane
	if err := sandbox.Refresh(ctx); err != nil {
		log.Fatal(err)
	}
	fmt.Println(sandbox.State(), sandbox.Info().Labels)
}
```


## Find sandboxes by name [#find-sandboxes-by-name]

Find sandboxes by name to look them up without their IDs. A name lookup returns every resource whose name matches exactly, because a name, unlike an ID, need not be unique.

The name of a sandbox is the name label it was created with. A sandbox whose labels no longer include the name label drops out of name lookups and of searches that filter on that label. The dashboard matches the start of a name, so it also lists sandboxes with longer names.

Find a sandbox by name to reuse it instead of creating a new one. The result is a list, newest first, and is empty when no sandbox matches. The lookup reads the [search](#search-sandboxes) index, so a sandbox created a moment ago can be missing.

1. Go to [Daytona Sandboxes ↗](https://app.daytona.com/dashboard/sandboxes)
2. Click the search match mode
3. Select **Name starts with**
4. Enter the sandbox name

**Python**

```python title="main.py"
from daytona import Daytona

daytona = Daytona()

# Reuse a named sandbox, or create it when none exists
found = daytona.v2.find_by_name("my-sandbox")
sandbox = found[0] if found else daytona.v2.create(name="my-sandbox")
```

| **Parameter** | **Description** |
| --- | --- |
| `name` | Matches the sandbox name exactly.<br />**Required**<br />**Example:** `"my-sandbox"` |
| `region` | Limits the lookup to the sandboxes in this region or a region inside it.<br />**Default:** every region<br />**Value:** `earth`, `us`, `us-east`, `us-east-ash-1`, `us-east-bos-1`, `us-east-iad-1`, or the ID of a [BYOC region](https://www.daytona.com/docs/bring-your-own-compute.md) of the organization, or `"local"` for every region<br />**Example:** `"us-east-ash-1"` |

**TypeScript**

```typescript title="index.ts"
import { Daytona } from '@daytona/sdk'

const daytona = new Daytona()

// Reuse a named sandbox, or create it when none exists
const found = await daytona.v2.findByName('my-sandbox')
const sandbox = found[0] ?? (await daytona.v2.create({ name: 'my-sandbox' }))
```

| **Parameter** | **Description** |
| --- | --- |
| `name` | Matches the sandbox name exactly.<br />**Required**<br />**Example:** `'my-sandbox'` |
| `region` | Limits the lookup to the sandboxes in this region or a region inside it.<br />**Default:** every region<br />**Value:** `earth`, `us`, `us-east`, `us-east-ash-1`, `us-east-bos-1`, `us-east-iad-1`, or the ID of a [BYOC region](https://www.daytona.com/docs/bring-your-own-compute.md) of the organization, or `'local'` for every region<br />**Example:** `'us-east-ash-1'` |

**Go**

```go title="main.go"
package main

import (
	"context"
	"fmt"
	"log"

	"go.daytona.com/sdk/v2"
)

func main() {
	ctx := context.Background()
	client, err := daytona.New()
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	// Reuse a named sandbox, or create it when none exists
	found, err := client.FindByName(ctx, "my-sandbox", nil)
	if err != nil {
		log.Fatal(err)
	}
	var sandbox *daytona.Sandbox
	if len(found) > 0 {
		sandbox = found[0]
	} else {
		sandbox, err = client.Create(ctx, "", &daytona.CreateOptions{Name: "my-sandbox"})
		if err != nil {
			log.Fatal(err)
		}
	}
	fmt.Println(sandbox.ID())
}
```

| **Parameter** | **Description** |
| --- | --- |
| `name` | Matches the sandbox name exactly.<br />**Required**<br />**Example:** `"my-sandbox"` |
| `Region` | Limits the lookup to the sandboxes in this region or a region inside it.<br />**Default:** every region<br />**Value:** `earth`, `us`, `us-east`, `us-east-ash-1`, `us-east-bos-1`, `us-east-iad-1`, or the ID of a [BYOC region](https://www.daytona.com/docs/bring-your-own-compute.md) of the organization, or `"local"` for every region<br />**Example:** `"us-east-ash-1"` |

**API**

```bash title="Terminal"
# Search by the name label: labels={"name":"my-sandbox"}
curl 'https://api.daytona.com/sandboxes/search?labels=%7B%22name%22%3A%22my-sandbox%22%7D' \
  --header 'Authorization: Bearer YOUR_API_KEY'
```

| **Field** | **Description** |
| --- | --- |
| `labels` | Filters by labels that each sandbox must contain.<br />**Format:** a URL-encoded JSON object of string keys and values<br />**Example:** `{"name":"my-sandbox"}` |


## Create an access token [#create-an-access-token]

Create an access token to give a person or program limited access to one sandbox. An access token is a short-lived credential that grants a fixed set of operations on one resource.

An access token of a sandbox works only for that sandbox, and only for the operations and private preview ports it grants. It lasts until it expires and cannot be refreshed or revoked. A client that holds it cannot use it to create other credentials.

Create an access token for a collaborator, CI job, or browser instead of a project API key. The token lasts one hour by default and at most 24 hours. Create a new one when it expires.

**Python**

```python title="main.py"
from daytona import Daytona

daytona = Daytona()
sandbox = daytona.v2.create()

# Grant file system and SSH access for 15 minutes
token = sandbox.create_access_token(operations=["toolbox:fs", "ssh"], ttl_seconds=900)
print(token.access_token, token.expires_at)
```

| **Parameter** | **Description** |
| --- | --- |
| `operations` | Sets the operations that the token grants.<br />**Default:** every operation that the API key can grant<br />**Value:** any of `toolbox`, `toolbox:exec`, `toolbox:fs`, `toolbox:git`, `desktop`, `ssh`, `sandbox:stop`, `sandbox:pause`, and `sandbox:delete`, or `[]` for preview ports only<br />**Example:** `["toolbox:fs", "ssh"]` |
| `ports` | Sets the private preview ports that the token can reach.<br />**Default:** all ports<br />**Maximum:** 128 ports<br />**Format:** ports from `1` to `65535`, except the sandbox daemon ports 2280, 2281, and 22220<br />**Value:** `[]` for no ports<br />**Example:** `[3000]` |
| `ttl_seconds` | Sets the token lifetime in seconds.<br />**Default:** `3600`<br />**Minimum:** `1`<br />**Maximum:** `86400` |

**TypeScript**

```typescript title="index.ts"
import { Daytona } from '@daytona/sdk'

const daytona = new Daytona()
const sandbox = await daytona.v2.create()

// Grant file system and SSH access for 15 minutes
const token = await sandbox.createAccessToken({ operations: ['toolbox:fs', 'ssh'], ttlSeconds: 900 })
console.log(token.accessToken, token.expiresAt)
```

| **Parameter** | **Description** |
| --- | --- |
| `operations` | Sets the operations that the token grants.<br />**Default:** every operation that the API key can grant<br />**Value:** any of `toolbox`, `toolbox:exec`, `toolbox:fs`, `toolbox:git`, `desktop`, `ssh`, `sandbox:stop`, `sandbox:pause`, and `sandbox:delete`, or `[]` for preview ports only<br />**Example:** `['toolbox:fs', 'ssh']` |
| `ports` | Sets the private preview ports that the token can reach.<br />**Default:** all ports<br />**Maximum:** 128 ports<br />**Format:** ports from `1` to `65535`, except the sandbox daemon ports 2280, 2281, and 22220<br />**Value:** `[]` for no ports<br />**Example:** `[3000]` |
| `ttlSeconds` | Sets the token lifetime in seconds.<br />**Default:** `3600`<br />**Minimum:** `1`<br />**Maximum:** `86400` |

**Go**

```go title="main.go"
package main

import (
	"context"
	"fmt"
	"log"
	"time"

	"go.daytona.com/sdk/v2"
)

func main() {
	ctx := context.Background()
	client, err := daytona.New()
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	sandbox, err := client.Create(ctx, "", nil)
	if err != nil {
		log.Fatal(err)
	}

	// Grant file system and SSH access for 15 minutes
	token, err := sandbox.CreateAccessToken(ctx, &daytona.CreateAccessTokenOptions{
		Operations: []string{"toolbox:fs", "ssh"},
		TTL:        15 * time.Minute,
	})
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(token.AccessToken, token.ExpiresAt)
}
```

| **Parameter** | **Description** |
| --- | --- |
| `Operations` | Sets the operations that the token grants.<br />**Default:** every operation that the API key can grant<br />**Value:** any of `toolbox`, `toolbox:exec`, `toolbox:fs`, `toolbox:git`, `desktop`, `ssh`, `sandbox:stop`, `sandbox:pause`, and `sandbox:delete`, or `[]string{}` for preview ports only<br />**Example:** `[]string{"toolbox:fs", "ssh"}` |
| `Ports` | Sets the private preview ports that the token can reach.<br />**Default:** all ports<br />**Maximum:** 128 ports<br />**Format:** ports from `1` to `65535`, except the sandbox daemon ports 2280, 2281, and 22220<br />**Value:** `[]int{}` for no ports<br />**Example:** `[]int{3000}` |
| `TTL` | Sets the token lifetime.<br />**Default:** `time.Hour`<br />**Minimum:** `time.Second`<br />**Maximum:** `24*time.Hour`<br />**Format:** a whole number of seconds |

**CLI**

```bash title="Terminal"
# Grant file system and SSH access for 15 minutes
daytona sandbox access-token {sandboxId} --operations toolbox:fs,ssh --ttl 15m
```

| **Flag** | **Description** |
| --- | --- |
| `--operations` | Sets the operations that the token grants.<br />**Default:** every operation that the API key can grant<br />**Format:** a comma-separated list<br />**Value:** any of `toolbox`, `toolbox:exec`, `toolbox:fs`, `toolbox:git`, `desktop`, `ssh`, `sandbox:stop`, `sandbox:pause`, and `sandbox:delete`, or `none` for preview ports only<br />**Example:** `toolbox:fs,ssh` |
| `--ports` | Sets the private preview ports that the token can reach.<br />**Default:** all ports<br />**Maximum:** 128 ports<br />**Format:** a comma-separated list of ports from `1` to `65535`, except the sandbox daemon ports 2280, 2281, and 22220<br />**Value:** `none` for no ports<br />**Example:** `3000` |
| `--ttl` | Sets the token lifetime.<br />**Default:** `1h`<br />**Minimum:** `1s`<br />**Maximum:** `24h`<br />**Format:** a duration in whole seconds, such as 30s, 10m, or 1h |

**API**

```bash title="Terminal"
# Grant file system and SSH access for 15 minutes
curl 'https://api.daytona.com/sandboxes/{sandboxId}/access-token' \
  --request POST \
  --header 'Content-Type: application/json' \
  --header 'Authorization: Bearer YOUR_API_KEY' \
  --data '{"operations": ["toolbox:fs", "ssh"], "ttl_seconds": 900}'
```

| **Field** | **Description** |
| --- | --- |
| `operations` | Sets the operations that the token grants.<br />**Default:** every operation that the API key can grant<br />**Value:** any of `toolbox`, `toolbox:exec`, `toolbox:fs`, `toolbox:git`, `desktop`, `ssh`, `sandbox:stop`, `sandbox:pause`, and `sandbox:delete`, or `[]` for preview ports only<br />**Example:** `["toolbox:fs", "ssh"]` |
| `ports` | Sets the private preview ports that the token can reach.<br />**Default:** all ports<br />**Maximum:** 128 ports<br />**Format:** ports from `1` to `65535`, except the sandbox daemon ports 2280, 2281, and 22220<br />**Value:** `[]` for no ports<br />**Example:** `[3000]` |
| `ttl_seconds` | Sets the token lifetime in seconds.<br />**Default:** `3600`<br />**Minimum:** `1`<br />**Maximum:** `86400` |


## Label sandboxes [#label-sandboxes]

Label sandboxes to group them and find them again. Labels are key-value pairs that describe a resource, such as the team, environment, or run it belongs to.

The labels of a sandbox are stored with it as one set. A label update replaces the whole set, and it removes any label that is left out of the new label set.

Label sandboxes to filter them by key and value when you [search](#search-sandboxes). A sandbox holds at most 100 labels, and each key and each value holds at most 512 bytes.

**Python**

```python title="main.py"
from daytona import Daytona

daytona = Daytona()
sandbox = daytona.v2.create()

# Replace the full label set
labels = sandbox.set_labels({"team": "research"})
print(labels)
```

| **Parameter** | **Description** |
| --- | --- |
| `labels` | Replaces the labels of the sandbox, except the name label when the new set omits it.<br />**Required**<br />**Maximum:** 100 labels<br />**Format:** a non-empty key and a value of at most 512 bytes each<br />**Value:** `{}` for no labels other than the name label<br />**Example:** `{"team": "research"}` |

**TypeScript**

```typescript title="index.ts"
import { Daytona } from '@daytona/sdk'

const daytona = new Daytona()
const sandbox = await daytona.v2.create()

// Replace the full label set
const labels = await sandbox.setLabels({ team: 'research' })
console.log(labels)
```

| **Parameter** | **Description** |
| --- | --- |
| `labels` | Replaces the labels of the sandbox, except the name label when the new set omits it.<br />**Required**<br />**Maximum:** 100 labels<br />**Format:** a non-empty key and a value of at most 512 bytes each<br />**Value:** `{}` for no labels other than the name label<br />**Example:** `{ team: 'research' }` |

**Go**

```go title="main.go"
package main

import (
	"context"
	"fmt"
	"log"

	"go.daytona.com/sdk/v2"
)

func main() {
	ctx := context.Background()
	client, err := daytona.New()
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	sandbox, err := client.Create(ctx, "", nil)
	if err != nil {
		log.Fatal(err)
	}

	// Replace the full label set
	labels, err := sandbox.SetLabels(ctx, map[string]string{"team": "research"})
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(labels)
}
```

| **Parameter** | **Description** |
| --- | --- |
| `labels` | Replaces the labels of the sandbox, except the name label when the new set omits it.<br />**Required**<br />**Maximum:** 100 labels<br />**Format:** a non-empty key and a value of at most 512 bytes each<br />**Value:** `nil` for no labels other than the name label<br />**Example:** `map[string]string{"team": "research"}` |

**CLI**

```bash title="Terminal"
# Replace the full label set
daytona sandbox update {sandboxId} --label team=research
```

| **Flag** | **Description** |
| --- | --- |
| `--clear-labels` | Removes every label except the name label. |
| `--label` | Replaces the labels of the sandbox, except the name label when the new set omits it.<br />**Maximum:** 100 labels<br />**Format:** KEY=VALUE, a non-empty key and a value of at most 512 bytes each, with the flag repeated for each label<br />**Example:** `team=research` |

**API**

```bash title="Terminal"
# Replace the full label set
curl 'https://api.daytona.com/sandboxes/{sandboxId}/labels' \
  --request PUT \
  --header 'Content-Type: application/json' \
  --header 'Authorization: Bearer YOUR_API_KEY' \
  --data '{ "labels": { "team": "research" } }'
```

| **Field** | **Description** |
| --- | --- |
| `labels` | Replaces the labels of the sandbox.<br />**Default:** `{}`<br />**Maximum:** 100 labels<br />**Format:** a non-empty key and a value of at most 512 bytes each<br />**Value:** `{}` for no labels<br />**Example:** `{"team": "research"}` |


## Logs [#logs]

Logs are the text output that a program or operating system writes as it runs. They show what happened inside a sandbox after the fact.

The logs of a sandbox are the output of its serial console, which records the boot of the operating system and the messages it prints. A follow reads new output as it is written, until it is interrupted.

Read the logs of a sandbox to find out why it failed to boot or stopped responding. Read them in the dashboard, or follow them from a terminal while the sandbox starts.

1. Go to [Daytona Sandboxes ↗](https://app.daytona.com/dashboard/sandboxes)
2. Click the sandbox
3. Open the **Logs** tab
4. Open the **Serial console** tab

**CLI**

```bash title="Terminal"
# Serial console log of the sandbox
daytona sandbox logs {sandboxId}

# Keep printing new output until interrupted
daytona sandbox logs {sandboxId} --follow
```

| **Flag** | **Description** |
| --- | --- |
| `--entrypoint` | Reads the entrypoint log instead of the serial console log. |
| `--follow` | Polls for new log output until interrupted. |

**API**

```bash title="Terminal"
# Serial console log of the sandbox
curl 'https://api.daytona.com/sandboxes/{sandboxId}/console-logs' \
  --header 'Authorization: Bearer YOUR_API_KEY'
```


## Metrics [#metrics]

Metrics are measurements of how much CPU, memory, and disk is in use at one moment. They show whether a sandbox has spare capacity or is close to its limits.

1. Go to [Daytona Sandboxes ↗](https://app.daytona.com/dashboard/sandboxes)
2. Click the sandbox
3. Open the **Metrics** tab

**Python**

```python title="main.py"
from daytona import Daytona

daytona = Daytona()
sandbox = daytona.v2.create()

metrics = sandbox.metrics()
print(metrics.cpu_utilization, metrics.memory_usage_bytes, metrics.disk_usage_bytes)
```

**TypeScript**

```typescript title="index.ts"
import { Daytona } from '@daytona/sdk'

const daytona = new Daytona()
const sandbox = await daytona.v2.create()

const metrics = await sandbox.metrics()
console.log(metrics.cpuUtilization, metrics.memoryUsageBytes, metrics.diskUsageBytes)
```

**Go**

```go title="main.go"
package main

import (
	"context"
	"fmt"
	"log"

	"go.daytona.com/sdk/v2"
)

func main() {
	ctx := context.Background()
	client, err := daytona.New()
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	sandbox, err := client.Create(ctx, "", nil)
	if err != nil {
		log.Fatal(err)
	}

	metrics, err := sandbox.Metrics(ctx)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(metrics.CPUUtilization, metrics.MemoryUsageBytes, metrics.DiskUsageBytes)
}
```

**API**

```bash title="Terminal"
curl '{toolboxProxyUrl}/{sandboxId}/system/metrics' \
  --header 'Authorization: Bearer YOUR_API_KEY'
```
