Skip to main content
This example demonstrates the fundamental operations for working with sandboxes in Daytona.

What You’ll Learn

  • Creating a new sandbox
  • Setting labels and metadata
  • Starting and stopping sandboxes
  • Retrieving sandbox information
  • Listing all sandboxes
  • Deleting sandboxes

Complete Example

from daytona import Daytona


def main():
    daytona = Daytona()

    print("Creating sandbox")
    sandbox = daytona.create()
    print("Sandbox created")

    _ = sandbox.set_labels(
        {
            "public": "true",
        }
    )

    print("Stopping sandbox")
    daytona.stop(sandbox)
    print("Sandbox stopped")

    print("Starting sandbox")
    daytona.start(sandbox)
    print("Sandbox started")

    print("Getting existing sandbox")
    existing_sandbox = daytona.get(sandbox.id)
    print("Get existing sandbox")

    response = existing_sandbox.process.exec('echo "Hello World from exec!"', cwd="/home/daytona", timeout=10)
    if response.exit_code != 0:
        print(f"Error: {response.exit_code} {response.result}")
    else:
        print(response.result)

    result = daytona.list()
    print("Total sandboxes count:", result.total)

    print(f"Printing first sandbox -> id: {result.items[0].id} state: {result.items[0].state}")

    print("Removing sandbox")
    daytona.delete(sandbox)
    print("Sandbox removed")


if __name__ == "__main__":
    main()

Expected Output

Creating sandbox
Sandbox created
Stopping sandbox
Sandbox stopped
Starting sandbox
Sandbox started
Getting existing sandbox
Got existing sandbox
Hello World from exec!
Total sandboxes count: 1
Printing first sandbox -> id: sandbox-abc123 state: running
Removing sandbox
Sandbox removed

Key Concepts

Creating a Sandbox

The simplest way to create a sandbox is calling create() with no parameters. This creates a sandbox with default settings:
  • Language: Python (default)
  • State: Running
  • Auto-archive: Enabled (after inactivity)

Setting Labels

Labels are key-value pairs that help organize and identify sandboxes:
sandbox.set_labels({
    "public": "true",
    "environment": "development",
    "team": "backend"
})

Sandbox States

Sandboxes can be in different states:
  • running - Sandbox is active and ready to use
  • stopped - Sandbox is paused and not consuming compute resources
  • archived - Sandbox is archived to save storage costs

Listing Sandboxes

The list() method returns paginated results:
result = daytona.list()
print(f"Total: {result.total}")
for sandbox in result.items:
    print(f"{sandbox.id}: {sandbox.state}")

Best Practices

Always clean up: Delete sandboxes when you’re done to avoid unnecessary costs.
Stopping vs Deleting: Stopping a sandbox preserves its state but you still pay for storage. Deleting removes everything.

Next Steps

File Operations

Learn how to upload and download files

Code Execution

Execute code in your sandboxes

Build docs developers (and LLMs) love