Use this file to discover all available pages before exploring further.
The Daytona Python SDK provides a simple interface for interacting with Daytona Sandboxes, enabling sandbox management, code execution, file operations, and more.
from daytona import Daytona# Initialize using environment variablesdaytona = Daytona()# Create a sandboxsandbox = daytona.create()# Run code in the sandboxresponse = sandbox.process.code_run('print("Hello World!")')print(response.result)# Clean up when donedaytona.delete(sandbox)
from daytona import Daytona, CreateSandboxFromSnapshotParamsdaytona = Daytona()params = CreateSandboxFromSnapshotParams( language="python", env_vars={"PYTHON_ENV": "development"}, auto_stop_interval=60, # Auto-stop after 1 hour of inactivity auto_archive_interval=60, # Auto-archive after 1 hour of being stopped auto_delete_interval=120 # Auto-delete after 2 hours of being stopped)sandbox = daytona.create(params)
# List all sandboxesresult = daytona.list()print(f"Total sandboxes: {result.total}")for sandbox in result.items: print(f"ID: {sandbox.id}, State: {sandbox.state}")# List with paginationresult = daytona.list(page=1, limit=10)
# Upload a single file from bytescontent = b'Hello, World!'sandbox.fs.upload_file(content, '/tmp/hello.txt')# Upload a file from local pathfrom daytona import FileUploadsandbox.fs.upload_files([ FileUpload(source='local-file.txt', destination='/tmp/remote-file.txt')])# Upload multiple filessandbox.fs.upload_files([ FileUpload(source='file1.txt', destination='/tmp/file1.txt'), FileUpload(source=b'content', destination='/tmp/file2.txt')])
# Download a single file to memorycontent = sandbox.fs.download_file('/tmp/hello.txt')print(content.decode('utf-8'))# Download multiple filesfrom daytona import FileDownloadRequestresults = sandbox.fs.download_files([ FileDownloadRequest(source='/tmp/file1.txt', destination='local-file1.txt'), FileDownloadRequest(source='/tmp/file2.txt') # To memory])for result in results: if result.error: print(f"Error: {result.error}") elif isinstance(result.result, str): print(f"Downloaded to: {result.result}") else: print(f"Downloaded to memory: {len(result.result)} bytes")
# List files in a directoryfiles = sandbox.fs.list_files('/tmp')for file in files: print(f"{file.name}: {file.size} bytes")# Create a foldersandbox.fs.create_folder('/tmp/new-folder', mode='755')# Search for filesmatches = sandbox.fs.search_files('/tmp', '*.txt')print(f"Found {len(matches.files)} files")for file in matches.files: print(file)# Replace content in filessandbox.fs.replace_in_files( ['/tmp/config.json'], '"debug": true', '"debug": false')
# Clone a public repositorysandbox.git.clone( 'https://github.com/daytonaio/daytona.git', '/tmp/daytona')# Clone with authenticationsandbox.git.clone( 'https://github.com/private/repo.git', '/tmp/repo', username='user', password='token')# Clone a specific branchsandbox.git.clone( 'https://github.com/example/repo.git', '/tmp/repo', branch='develop')
# Create and start a TypeScript LSP serverlsp = sandbox.create_lsp_server('typescript', '/workspace/project')lsp.start()# Notify LSP about opened filelsp.did_open('/workspace/project/src/index.ts')
from daytona import Daytonadef process_data(): daytona = Daytona() sandbox = daytona.create() try: # Do work with sandbox result = sandbox.process.code_run('print("Hello")') return result finally: # Always cleanup daytona.delete(sandbox)