Daytona API Client

0.164.0 · active · verified Sat Apr 11

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

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the Daytona client, create a sandbox, execute Python code within it, and then clean up the sandbox. Authentication is handled via an API key, ideally from an environment variable.

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

view raw JSON →