Skip to main content

What You’ll Learn

  • Creating a sandbox with Sandbox.create()
  • Running a shell command with sbx.commands.run()
  • Reading command output via result.stdout and result.exit_code
  • Using the async API with AsyncSandbox (Python)
  • Proper cleanup with try/finally and sbx.kill()

Prerequisites

Code Walkthrough

Import both the synchronous and asynchronous sandbox classes:
from declaw import AsyncSandbox, Sandbox
Synchronous usage — create a sandbox, run a command, clean up:
def sync_example() -> None:
    sbx = Sandbox.create(template="base", timeout=300)
    try:
        print(f"Sandbox created: {sbx.sandbox_id}")

        result = sbx.commands.run('echo "Hello from Declaw sandbox!"')
        print(f"stdout: {result.stdout}")
        print(f"exit_code: {result.exit_code}")
    finally:
        sbx.kill()
        print("Sandbox killed.")
Async usage — identical flow using await:
async def async_example() -> None:
    sbx = await AsyncSandbox.create(template="base", timeout=300)
    try:
        print(f"Sandbox created: {sbx.sandbox_id}")

        result = await sbx.run_command('echo "Hello from async Declaw sandbox!"')
        print(f"stdout: {result.stdout}")
        print(f"exit_code: {result.exit_code}")
    finally:
        await sbx.kill()
        print("Sandbox killed.")
Run both from main():
import asyncio

def main() -> None:
    sync_example()
    asyncio.run(async_example())

Expected Output

==================================================
Declaw Hello World Example
==================================================

--- Sync: Creating Sandbox ---
Sandbox created: sbx_abc123

--- Sync: Running Command ---
stdout: Hello from Declaw sandbox!
exit_code: 0

--- Sync: Cleaning Up ---
Sandbox killed.

--- Async: Creating Sandbox ---
Sandbox created: sbx_def456

--- Async: Running Command ---
stdout: Hello from async Declaw sandbox!
exit_code: 0

--- Async: Cleaning Up ---
Sandbox killed.

==================================================
Done!
==================================================