Daytona API Client
Daytona is an open-source platform that provides secure, isolated 'sandboxes' – composable computers – for running AI-generated code and streamlining development environments for engineering teams. The Python SDK allows programmatic interaction with these sandboxes, offering functionalities like lifecycle management, code execution, file system operations, and Git integration. It aims to simplify environment setup across various infrastructures. The library is actively maintained with frequent releases, often on a near-daily basis.
Warnings
- breaking The package name and import path for the Python SDK changed from `daytona-sdk` to `daytona`. Code using `pip install daytona-sdk` or `from daytona_sdk import Daytona` will fail or use an outdated client.
- breaking Error handling has been standardized to use specific, semantically meaningful exception subclasses (e.g., `DaytonaNotFoundError`, `DaytonaAuthenticationError`) instead of the generic `DaytonaError`. While `DaytonaError` still acts as a catch-all, relying on string parsing of error messages to differentiate failures is now brittle and may break.
- gotcha The Python SDK's dotenv handling (loading environment variables from `.env` files) has been made more isolated. Only environment variables prefixed with `DAYTONA_` are now loaded, preventing unintended pollution of the global environment.
- gotcha The Daytona SDK undergoes rapid development with frequent minor version releases (often daily or every few days). While this ensures continuous improvement, users should plan for regular updates and monitor release notes for changes that might affect their applications.
Install
-
pip install daytona
Imports
- Daytona
from daytona import Daytona
- DaytonaConfig
from daytona import Daytona, DaytonaConfig
Quickstart
import os
from daytona import Daytona, DaytonaConfig
# It is highly recommended to set API_KEY as an environment variable (DAYTONA_API_KEY)
api_key = os.environ.get('DAYTONA_API_KEY', 'YOUR_DAYTONA_API_KEY')
if api_key == 'YOUR_DAYTONA_API_KEY':
print("Warning: Please set the DAYTONA_API_KEY environment variable or replace 'YOUR_DAYTONA_API_KEY' with your actual key.")
exit()
# Configure Daytona client (using environment variables by default, or explicitly)
config = DaytonaConfig(api_key=api_key)
daytona = Daytona(config)
try:
# Create a new sandbox
print("Creating a Daytona sandbox...")
sandbox = daytona.create()
print(f"Sandbox created with ID: {sandbox.id} and state: {sandbox.state}")
# Run a simple Python command in the sandbox
print("Running 'Hello World!' in the sandbox...")
response = sandbox.process.code_run('print("Hello World from Daytona sandbox!")')
print(f"Sandbox output: {response.result}")
print(f"Exit code: {response.exit_code}")
# Clean up the sandbox
print(f"Deleting sandbox {sandbox.id}...")
daytona.delete(sandbox)
print("Sandbox deleted successfully.")
except Exception as e:
print(f"An error occurred: {e}")
# Add more specific error handling as per warnings below