Daytona API Client (Async)

0.164.0 · active · verified Sat Apr 11

The `daytona-api-client-async` library provides an asynchronous Python client for interacting with the Daytona platform. Daytona offers a secure and elastic infrastructure for running AI-generated code within isolated development environments called sandboxes. This client enables programmatic management of these sandboxes, including creation, code execution, and lifecycle operations. The library is part of the larger Daytona SDK ecosystem, which receives frequent updates, often multiple times a week.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the asynchronous Daytona client, create a sandbox, execute a simple command within it, and then delete the sandbox. It assumes `DAYTONA_API_KEY` is set as an environment variable or passed explicitly.

import asyncio
import os
from daytona import AsyncDaytona, DaytonaConfig

async def main():
    # API key can also be set via DAYTONA_API_KEY environment variable
    # For this example, we fetch it from env or use a placeholder if not set.
    api_key = os.environ.get('DAYTONA_API_KEY', 'YOUR_API_KEY_HERE')
    if api_key == 'YOUR_API_KEY_HERE':
        print("WARNING: DAYTONA_API_KEY environment variable not set. Using placeholder.")

    config = DaytonaConfig(api_key=api_key)

    try:
        async with AsyncDaytona(config) as daytona:
            # Create a new sandbox
            print("Creating a new Daytona sandbox...")
            sandbox = await daytona.create()
            print(f"Sandbox '{sandbox.name}' created with ID: {sandbox.id}")

            # Execute a command in the sandbox
            command = "echo 'Hello from Daytona Sandbox!'"
            print(f"Executing command: '{command}'...")
            response = await sandbox.process.exec(command)
            print("Command output:")
            print(response.result)

            # Clean up: delete the sandbox
            print(f"Deleting sandbox '{sandbox.name}'...")
            await daytona.delete(sandbox.id)
            print(f"Sandbox '{sandbox.name}' deleted.")
    except Exception as e:
        print(f"An error occurred: {e}")

if __name__ == "__main__":
    asyncio.run(main())

view raw JSON →