Daytona
class Daytona()
Main class for interacting with the Daytona API.
This class provides methods to create, manage, and interact with Daytona Sandboxes. It can be initialized either with explicit configuration or using environment variables.
Attributes:
api_key
str - API key for authentication.api_url
str - URL of the Daytona API.target
str - Default target location for Sandboxes.
Example:
Using environment variables:
daytona = Daytona() # Uses DAYTONA_API_KEY, DAYTONA_API_URL
Using explicit configuration:
config = DaytonaConfig( api_key="your-api-key", api_url="https://your-api.com", target="us")daytona = Daytona(config)
Daytona.__init__
def __init__(config: Optional[DaytonaConfig] = None)
Initializes Daytona instance with optional configuration.
If no config is provided, reads from environment variables:
DAYTONA_API_KEY
: Required API key for authenticationDAYTONA_API_URL
: Required api URLDAYTONA_TARGET
: Optional target environment (defaults to SandboxTargetRegion.US)
Arguments:
config
Optional[DaytonaConfig] - Object containing api_key, api_url, and target.
Raises:
DaytonaError
- If API key is not provided either through config or environment variables
Example:
from daytona_sdk import Daytona, DaytonaConfig# Using environment variablesdaytona1 = Daytona()# Using explicit configurationconfig = DaytonaConfig( api_key="your-api-key", api_url="https://your-api.com", target="us")daytona2 = Daytona(config)
Daytona.create
@intercept_errors(message_prefix="Failed to create sandbox: ")def create(params: Optional[CreateSandboxParams] = None, timeout: Optional[float] = 60) -> Sandbox
Creates Sandboxes with default or custom configurations. You can specify various parameters, including language, image, resources, environment variables, and volumes for the Sandbox.
Arguments:
params
Optional[CreateSandboxParams] - Parameters for Sandbox creation. If not provided, defaults to Python language.timeout
Optional[float] - Timeout (in seconds) for sandbox creation. 0 means no timeout. Default is 60 seconds.
Returns:
Sandbox
- The created Sandbox instance.
Raises:
DaytonaError
- If timeout or auto_stop_interval is negative; If sandbox fails to start or times out
Example:
Create a default Python Sandbox:
sandbox = daytona.create()
Create a custom Sandbox:
params = CreateSandboxParams( language="python", name="my-sandbox", image="debian:12.9", env_vars={"DEBUG": "true"}, resources=SandboxResources(cpu=2, memory=4096), auto_stop_interval=0)sandbox = daytona.create(params, 40)
Daytona.remove
@intercept_errors(message_prefix="Failed to remove sandbox: ")def remove(sandbox: Sandbox, timeout: Optional[float] = 60) -> None
Removes a Sandbox.
Arguments:
sandbox
Sandbox - The Sandbox instance to remove.timeout
Optional[float] - Timeout (in seconds) for sandbox removal. 0 means no timeout. Default is 60 seconds.
Raises:
DaytonaError
- If sandbox fails to remove or times out
Example:
sandbox = daytona.create()# ... use sandbox ...daytona.remove(sandbox) # Clean up when done
Daytona.get_current_workspace
@deprecated(reason=( "Method is deprecated. Use `get_current_sandbox` instead. This method will be removed in a future version."))def get_current_workspace(workspace_id: str) -> Workspace
Gets a Sandbox by its ID.
Arguments:
workspace_id
str - The ID of the Sandbox to retrieve.
Returns:
Workspace
- The Sandbox instance.
Daytona.get_current_sandbox
@intercept_errors(message_prefix="Failed to get sandbox: ")def get_current_sandbox(sandbox_id: str) -> Sandbox
Gets a Sandbox by its ID.
Arguments:
sandbox_id
str - The ID of the Sandbox to retrieve.
Returns:
Sandbox
- The Sandbox instance.
Raises:
DaytonaError
- If sandbox_id is not provided.
Example:
sandbox = daytona.get_current_sandbox("my-sandbox-id")print(sandbox.status)
Daytona.list
@intercept_errors(message_prefix="Failed to list sandboxes: ")def list() -> List[Sandbox]
Lists all Sandboxes.
Returns:
List[Sandbox]
- List of all available Sandbox instances.
Example:
sandboxes = daytona.list()for sandbox in sandboxes: print(f"{sandbox.id}: {sandbox.status}")
Daytona.start
def start(sandbox: Sandbox, timeout: Optional[float] = 60) -> None
Starts a Sandbox and waits for it to be ready.
Arguments:
sandbox
Sandbox - The Sandbox to start.timeout
Optional[float] - Optional timeout in seconds to wait for the Sandbox to start. 0 means no timeout. Default is 60 seconds.
Raises:
DaytonaError
- If timeout is negative; If Sandbox fails to start or times out
Daytona.stop
def stop(sandbox: Sandbox, timeout: Optional[float] = 60) -> None
Stops a Sandbox and waits for it to be stopped.
Arguments:
sandbox
Sandbox - The sandbox to stoptimeout
Optional[float] - Optional timeout (in seconds) for sandbox stop. 0 means no timeout. Default is 60 seconds.
Raises:
DaytonaError
- If timeout is negative; If Sandbox fails to stop or times out
CodeLanguage
@dataclassclass CodeLanguage(Enum)
Programming languages supported by Daytona
Enum Members:
PYTHON
(“python”)TYPESCRIPT
(“typescript”)JAVASCRIPT
(“javascript”)
DaytonaConfig
class DaytonaConfig(BaseModel)
Configuration options for initializing the Daytona client.
Attributes:
api_key
Optional[str] - API key for authentication with the Daytona API. If not set, it must be provided via the environment variableDAYTONA_API_KEY
, or a JWT token must be provided instead.jwt_token
Optional[str] - JWT token for authentication with the Daytona API. If not set, it must be provided via the environment variableDAYTONA_JWT_TOKEN
, or an API key must be provided instead.organization_id
Optional[str] - Organization ID used for JWT-based authentication. Required if a JWT token is provided, and must be set either here or in the environment variableDAYTONA_ORGANIZATION_ID
.api_url
Optional[str] - URL of the Daytona API. Defaults to'https://app.daytona.io/api'
if not set here or in the environment variableDAYTONA_API_URL
.server_url
Optional[str] - Deprecated. Useapi_url
instead. This property will be removed in a future version.target
Optional[SandboxTargetRegion] - Target environment for the Sandbox. Defaults to'us'
if not set here or in the environment variableDAYTONA_TARGET
.
Example:
config = DaytonaConfig(api_key="your-api-key")
config = DaytonaConfig(jwt_token="your-jwt-token", organization_id="your-organization-id")
SandboxResources
@dataclassclass SandboxResources()
Resources configuration for Sandbox.
Attributes:
cpu
Optional[int] - Number of CPU cores to allocate.memory
Optional[int] - Amount of memory in GB to allocate.disk
Optional[int] - Amount of disk space in GB to allocate.gpu
Optional[int] - Number of GPUs to allocate.
Example:
resources = SandboxResources( cpu=2, memory=4, # 4GB RAM disk=20, # 20GB disk gpu=1)params = CreateSandboxParams( language="python", resources=resources)
CreateSandboxParams
class CreateSandboxParams(BaseModel)
Parameters for creating a new Sandbox.
Attributes:
language
Optional[CodeLanguage] - Programming language for the Sandbox (“python”, “javascript”, “typescript”). Defaults to “python”.id
Optional[str] - Custom identifier for the Sandbox. If not provided, a random ID will be generated.name
Optional[str] - Display name for the Sandbox. Defaults to Sandbox ID if not provided.image
Optional[str] - Custom Docker image to use for the Sandbox.os_user
Optional[str] - OS user for the Sandbox.env_vars
Optional[Dict[str, str]] - Environment variables to set in the Sandbox.labels
Optional[Dict[str, str]] - Custom labels for the Sandbox.public
Optional[bool] - Whether the Sandbox should be public.target
Optional[str] - Target location for the Sandbox. Can be “us”, “eu”, or “asia”.resources
Optional[SandboxResources] - Resource configuration for the Sandbox.timeout
Optional[float] - Timeout in seconds for Sandbox to be created and started.auto_stop_interval
Optional[int] - Interval in minutes after which Sandbox will automatically stop if no Sandbox event occurs during that time. Default is 15 minutes. 0 means no auto-stop.
Example:
params = CreateSandboxParams( language="python", name="my-sandbox", env_vars={"DEBUG": "true"}, resources=SandboxResources(cpu=2, memory=4), auto_stop_interval=20)sandbox = daytona.create(params, 50)