Daytona SDK
The Daytona Python SDK is the official Python library for Daytona, an open-source, secure, and elastic infrastructure for running AI-generated code. It provides interfaces for managing sandboxes (isolated execution environments), file system operations, Git operations, language server protocol support, and process and code execution. The library is actively maintained and currently at version 0.166.0, with regular updates. [1, 13]
Common errors
-
daytona.exceptions.DaytonaAuthenticationException: Invalid or missing API key
cause The SDK could not authenticate with the Daytona API. This typically means the `DAYTONA_API_KEY` is incorrect, missing, or has expired. [7]fixEnsure the `DAYTONA_API_KEY` environment variable is correctly set, or pass a valid API key directly to `DaytonaConfig`. Generate a new API key from the Daytona Dashboard if needed. [3, 7] -
daytona.exceptions.DaytonaBadRequestException: Invalid request parameters
cause A request made to the Daytona API (e.g., creating a sandbox or snapshot) contained malformed data or invalid parameters, such as an unsupported language or invalid resource specification. [7]fixReview the parameters passed to your SDK calls (e.g., `daytona.create()`, `CreateSandboxFromSnapshotParams`) against the official documentation to ensure they are valid and correctly formatted for your SDK version. [7] -
daytona.exceptions.DaytonaConnectionException: Cannot reach Daytona API: [network error details]
cause The SDK failed to establish a network connection with the Daytona API. This could be due to DNS issues, a firewall blocking access, an incorrect API URL, or general network instability. [7]fixVerify your internet connection, check the `DAYTONA_API_URL` configuration if explicitly set (default is `https://app.daytona.io/api`), and ensure no firewalls are preventing outbound connections to the Daytona API. [7, 13]
Warnings
- breaking Major breaking changes were introduced in Daytona SDK v0.21.0, shifting from a 'declarative image builder' to a 'snapshot-based workflow'. This affects how sandboxes are created and how resources are managed. [2, 19]
- breaking Resource configuration for sandboxes is now tied to the snapshot rather than being directly configurable on the sandbox object during creation in the new workflow (v0.21.0+). [19]
- deprecated Older sandbox management methods, such as `daytona.remove(sandbox)`, are no longer supported. Sandbox details are also now available as top-level properties on the `Sandbox` object. [2]
- gotcha The SDK license changed from AGPL to Apache 2.0 starting from v0.21.0. While this is generally less restrictive, users on older versions should be aware of the original AGPL license implications if they have not upgraded. [19]
Install
-
pip install daytona-sdk
Imports
- Daytona
from daytona import Daytona
- DaytonaConfig
from daytona import DaytonaConfig
- CreateSandboxFromSnapshotParams
from daytona import CreateSandboxFromSnapshotParams
- AsyncDaytona
from daytona import AsyncDaytona
Quickstart
import os
from daytona import Daytona, DaytonaConfig
# Configure with API key from environment variable (recommended) or direct string
api_key = os.environ.get("DAYTONA_API_KEY", "") # Replace with your actual key or ensure env var is set
if not api_key:
print("Warning: DAYTONA_API_KEY not found in environment variables. Please set it or pass it directly.")
# For demonstration purposes, you might uncomment and replace:
# api_key = "YOUR_API_KEY_HERE"
# if api_key == "YOUR_API_KEY_HERE":
# raise ValueError("Please provide a valid Daytona API key.")
config = DaytonaConfig(api_key=api_key)
daytona = Daytona(config)
try:
# Create a sandbox
print("Creating Daytona sandbox...")
sandbox = daytona.create()
print(f"Created Sandbox ID: {sandbox.id}")
# Run code securely inside the Sandbox
print("Running code in sandbox...")
response = sandbox.process.code_run('print("Hello World from Daytona!")')
print(f"Code Output: {response.result}")
# Optional: Execute a shell command
# shell_response = sandbox.process.exec("ls -la")
# print(f"Shell Command Output:\n{shell_response.result}")
finally:
# Clean up the sandbox (important to release resources)
if 'sandbox' in locals() and sandbox:
print(f"Deleting Sandbox ID: {sandbox.id}...")
sandbox.delete()
print("Sandbox deleted.")