Skip to main content

Python SDK for Daytona

Project description

Daytona Python SDK

The official Python SDK for Daytona, an open-source, secure and elastic infrastructure for running AI-generated code. Daytona provides full composable computers — sandboxes — that you can manage programmatically using the Daytona SDK.

The SDK provides an interface for sandbox management, file system operations, Git operations, language server protocol support, process and code execution, and computer use. For more information, see the documentation.

Installation

Install the package using pip:

pip install daytona

Get API key

Generate an API key from the Daytona Dashboard ↗ to authenticate SDK requests and access Daytona services. For more information, see the API keys documentation.

Configuration

Configure the SDK using environment variables or by passing a configuration object:

  • DAYTONA_API_KEY: Your Daytona API key
  • DAYTONA_API_URL: The Daytona API URL
  • DAYTONA_TARGET: Your target region environment (e.g. us, eu)
from daytona import Daytona, DaytonaConfig

# Initialize with environment variables
daytona = Daytona()

# Initialize with configuration object
config = DaytonaConfig(
    api_key="YOUR_API_KEY",
    api_url="YOUR_API_URL",
    target="us"
)

Create a sandbox

Create a sandbox to run your code securely in an isolated environment.

from daytona import Daytona, DaytonaConfig

config = DaytonaConfig(api_key="YOUR_API_KEY")
daytona = Daytona(config)
sandbox = daytona.create()
response = sandbox.process.code_run('print("Hello World")')

Examples and guides

Daytona provides examples and guides for common sandbox operations, best practices, and a wide range of topics, from basic usage to advanced topics, showcasing various types of integrations between Daytona and other tools.

Create a sandbox with custom resources

Create a sandbox with custom resources (CPU, memory, disk).

from daytona import Daytona, CreateSandboxFromImageParams, Image, Resources

daytona = Daytona()
sandbox = daytona.create(
    CreateSandboxFromImageParams(
        image=Image.debian_slim("3.12"),
        resources=Resources(cpu=2, memory=4, disk=8)
    )
)

Create an ephemeral sandbox

Create an ephemeral sandbox that is automatically deleted when stopped.

from daytona import Daytona, CreateSandboxFromSnapshotParams

daytona = Daytona()
sandbox = daytona.create(
    CreateSandboxFromSnapshotParams(ephemeral=True, auto_stop_interval=5)
)

Create a sandbox from a snapshot

Create a sandbox from a snapshot.

from daytona import Daytona, CreateSandboxFromSnapshotParams

daytona = Daytona()
sandbox = daytona.create(
    CreateSandboxFromSnapshotParams(
        snapshot="my-snapshot-name",
        language="python"
    )
)

Execute Commands

Execute commands in the sandbox.

# Execute a shell command
response = sandbox.process.exec('echo "Hello, World!"')
print(response.result)

# Run Python code
response = sandbox.process.code_run('''
x = 10
y = 20
print(f"Sum: {x + y}")
''')
print(response.result)

File Operations

Upload, download, and search files in the sandbox.

# Upload a file
sandbox.fs.upload_file(b'Hello, World!', 'path/to/file.txt')

# Download a file
content = sandbox.fs.download_file('path/to/file.txt')

# Search for files
matches = sandbox.fs.find_files(root_dir, 'search_pattern')

Git Operations

Clone, list branches, and add files to the sandbox.

# Clone a repository
sandbox.git.clone('https://github.com/example/repo', 'path/to/clone')

# List branches
branches = sandbox.git.branches('path/to/repo')

# Add files
sandbox.git.add('path/to/repo', ['file1.txt', 'file2.txt'])

Language Server Protocol

Create and start a language server to get code completions, document symbols, and more.

# Create and start a language server
lsp = sandbox.create_lsp_server('python', 'path/to/project')
lsp.start()

# Notify the lsp for the file
lsp.did_open('path/to/file.py')

# Get document symbols
symbols = lsp.document_symbols('path/to/file.py')

# Get completions
completions = lsp.completions('path/to/file.py', {"line": 10, "character": 15})

Contributing

Daytona is Open Source under the Apache License 2.0, and is the copyright of its contributors. If you would like to contribute to the software, read the Developer Certificate of Origin Version 1.1 (https://developercertificate.org/). Afterwards, navigate to the contributing guide to get started.

Code in _sync directory shouldn't be edited directly. It should be generated from the corresponding async code in the _async directory using the SDK generation scripts in the scripts directory.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

daytona-0.164.0a1.tar.gz (118.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

daytona-0.164.0a1-py3-none-any.whl (146.9 kB view details)

Uploaded Python 3

File details

Details for the file daytona-0.164.0a1.tar.gz.

File metadata

  • Download URL: daytona-0.164.0a1.tar.gz
  • Upload date:
  • Size: 118.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.10.12 Linux/6.8.0-107-generic

File hashes

Hashes for daytona-0.164.0a1.tar.gz
Algorithm Hash digest
SHA256 438884fa45d0266f4149ee05f52e7afdd2802ae399d67f9f992aeb3cdc20ca96
MD5 e0b63e7fabb3629dd3a906c35fc323fc
BLAKE2b-256 8cf0b5bcddc5ee5b33645b787a44d979518eb95c454957266e5d7574e0f8d6c9

See more details on using hashes here.

File details

Details for the file daytona-0.164.0a1-py3-none-any.whl.

File metadata

  • Download URL: daytona-0.164.0a1-py3-none-any.whl
  • Upload date:
  • Size: 146.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.10.12 Linux/6.8.0-107-generic

File hashes

Hashes for daytona-0.164.0a1-py3-none-any.whl
Algorithm Hash digest
SHA256 12b699d68484422f18f672be689fb00e94052abf1c9ce5311488056e0e3fcc97
MD5 4b9186a0feb93ab72547452e086b090b
BLAKE2b-256 beca80aa336a2e5e816d8412a8be2d86c428e8c8e8a2516f88f2deb948303415

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page