Daytona SDK

0.166.0 · active · verified Thu Apr 16

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the Daytona SDK, create an isolated sandbox environment, execute Python code within it, and then clean up the sandbox. Ensure your `DAYTONA_API_KEY` is set as an environment variable or passed directly to `DaytonaConfig`. [3, 6, 13]

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.")

view raw JSON →