AsyncDaytona | Daytona

AsyncDaytona

class AsyncDaytona()

Main class for interacting with the Daytona API.

This class provides asynchronous methods to create, manage, and interact with Daytona Sandboxes. It can be initialized either with explicit configuration or using environment variables.

Attributes:

Example:

Using environment variables:

async with AsyncDaytona() as daytona:  # Uses DAYTONA_API_KEY, DAYTONA_API_URL
    sandbox = await daytona.create()

Using explicit configuration:

config = DaytonaConfig(
    api_key="your-api-key",
    api_url="https://your-api.com",
    target="us"
)
try:
    daytona = AsyncDaytona(config)
    sandbox = await daytona.create()
finally:
    await daytona.close()

Using OpenTelemetry tracing:

config = DaytonaConfig(
    api_key="your-api-key",
    experimental={"otelEnabled": True}
)
async with AsyncDaytona(config) as daytona:
    sandbox = await daytona.create()
    # All SDK operations will be traced
# OpenTelemetry traces are flushed on close

AsyncDaytona.init

def __init__(config: DaytonaConfig | None = None)

Initializes Daytona instance with optional configuration.

If no config is provided, reads from environment variables:

Arguments:

Raises:

Example:

from daytona import Daytona, DaytonaConfig

# Using environment variables

daytona1 = AsyncDaytona()
await daytona1.close()

# Using explicit configuration
config = DaytonaConfig(
    api_key="your-api-key",
    api_url="https://your-api.com",
    target="us"
)
daytona2 = AsyncDaytona(config)
await daytona2.close()

AsyncDaytona.aenter

async def __aenter__()

Async context manager entry.

AsyncDaytona.aexit

async def __aexit__(exc_type: type[BaseException] | None = None,
                    exc_value: BaseException | None = None,
                    traceback: TracebackType | None = None)

Async context manager exit - ensures proper cleanup.

AsyncDaytona.close

async def close()

Close the HTTP session and clean up resources. This method should be called when you’re done using the AsyncDaytona instance to properly close the underlying HTTP sessions and avoid resource leaks.

Example:

daytona = AsyncDaytona()
try:
    sandbox = await daytona.create()
    # ... use sandbox ...
finally:
    await daytona.close()

Or better yet, use as async context manager:

async with AsyncDaytona() as daytona:
    sandbox = await daytona.create()
    # ... use sandbox ...
# Automatically closed

AsyncDaytona.create

@overload
async def create(params: CreateSandboxFromSnapshotParams | None = None,
                 *,
                 timeout: float = 60) -> AsyncSandbox

Creates Sandboxes from specified or default snapshot. You can specify various parameters, including language, image, environment variables, and volumes.

Arguments:

Returns:

Raises:

Example: Create a default Python Sandbox:

sandbox = await daytona.create()

Create a custom Sandbox:

params = CreateSandboxFromSnapshotParams(
    language="python",
    snapshot="my-snapshot-id",
    env_vars={"DEBUG": "true"},
    auto_stop_interval=0,
    auto_archive_interval=60,
    auto_delete_interval=120
)
sandbox = await daytona.create(params, timeout=40)

AsyncDaytona.delete

@with_instrumentation()
async def delete(sandbox: AsyncSandbox,
                 timeout: float = 60,
                 wait: bool = False) -> None

Deletes a Sandbox.

By default returns as soon as the deletion request is accepted (fire-and-forget). Pass wait=True to block until the Sandbox reaches the ‘destroyed’ state.

Arguments:

Raises:

Example:

sandbox = await daytona.create()

# ... use sandbox ...

await daytona.delete(sandbox)  # Clean up when done

AsyncDaytona.get

@intercept_errors(message_prefix="Failed to get sandbox: ")
@with_instrumentation()
async def get(sandbox_id_or_name: str,
              request_timeout: float | None = None) -> AsyncSandbox

Gets a Sandbox by its ID or name.

Arguments:

Returns:

Raises:

Example:

sandbox = await daytona.get("my-sandbox-id-or-name")
print(sandbox.state)

AsyncDaytona.list

@intercept_errors(message_prefix="Failed to list sandboxes: ")
@with_instrumentation()
async def list(
        query: ListSandboxesQuery | None = None,
        request_timeout: float | None = None) -> AsyncIterator[AsyncSandbox]

Iterates over Sandboxes matching the given query.

Arguments:

Yields:

Example:

from daytona import ListSandboxesQuery

async for sandbox in daytona.list(ListSandboxesQuery(labels={"env": "dev"})):
    print(sandbox.id)

AsyncDaytona.start

@with_instrumentation()
async def start(sandbox: AsyncSandbox, timeout: float = 60) -> None

Starts a Sandbox and waits for it to be ready.

Arguments:

Raises:

AsyncDaytona.stop

@with_instrumentation()
async def stop(sandbox: AsyncSandbox, timeout: float = 60) -> None

Stops a Sandbox and waits for it to be stopped.

Arguments:

Raises:

CodeLanguage

class CodeLanguage(str, Enum)

Programming languages supported by Daytona

Enum Members:

DaytonaConfig

class DaytonaConfig(BaseModel)

Configuration options for initializing the Daytona client.

Attributes:

Example:

config = DaytonaConfig(api_key="your-api-key")
config = DaytonaConfig(jwt_token="your-jwt-token", organization_id="your-organization-id")

CreateSandboxBaseParams

class CreateSandboxBaseParams(BaseModel)

Base parameters for creating a new Sandbox.

Attributes:

CreateSandboxFromImageParams

class CreateSandboxFromImageParams(CreateSandboxBaseParams)

Parameters for creating a new Sandbox from an image.

Attributes:

CreateSandboxFromSnapshotParams

class CreateSandboxFromSnapshotParams(CreateSandboxBaseParams)

Parameters for creating a new Sandbox from a snapshot.

Attributes: