Cua Docs

Sandbox SDK reference

API reference for the Cua Sandbox Python package: Sandbox, Image, Localhost, and top-level functions.

Package#

cua-sandbox is a Python package imported as cua.

FieldValue
Packagecua-sandbox
Import namecua
Python version3.12+
Installpip install cua

Top-level exports#

ExportDescription
SandboxVM or container control class
ImageImage builder class
LocalhostDirect host control class
SandboxInfoSandbox metadata structure
configure()Global SDK configuration function
login()Browser login function
whoami()Authenticated user lookup function

Environment variables#

VariableDescription
CUA_API_KEYCua API key for cloud sandboxes

Sandbox class#

Sandbox controls a VM or container through .mouse, .keyboard, .screen, .clipboard, .shell, .window, .terminal, .mobile, and .tunnel.

Sandbox.ephemeral#

Async context manager. The sandbox is destroyed automatically on exit.

async def ephemeral(cls, image: Image, name=None, api_key=None, local=False, runtime=None, cpu=None, memory_mb=None, disk_gb=None, region='us-east-1') -> AsyncIterator[Sandbox]
ParameterTypeDefaultDescription
imageImagerequiredImage to run
namestr or NoneNoneOptional name
api_keystr or NoneNoneCua API key; reads CUA_API_KEY if None
localboolFalseUse local runtime (Docker/Lume/QEMU)
runtimeRuntime or NoneNoneExplicit runtime backend
cpuint or NoneNoneCPU count (cloud)
memory_mbint or NoneNoneMemory in MB (cloud)
disk_gbint or NoneNoneDisk size in GB (cloud)
regionstr'us-east-1'Cloud region

Sandbox.create#

Provisions a persistent sandbox. The sandbox keeps running after the script exits.

async def create(cls, image: Image, name=None, api_key=None, local=False, runtime=None, cpu=None, memory_mb=None, disk_gb=None, region='us-east-1') -> Sandbox

Same parameters as ephemeral.

Sandbox.connect#

Connects to an existing sandbox by name. Supports both await and async with. When used as a context manager, disconnect() is called on exit. The sandbox keeps running.

def connect(cls, name: str, api_key=None, local=False, ws_url=None, http_url=None, container_name=None, cpu=None, memory_mb=None, disk_gb=None, region='us-east-1') -> _ConnectResult
ParameterTypeDefaultDescription
namestrrequiredSandbox name
api_keystr or NoneNoneCua API key; reads CUA_API_KEY if None
localboolFalseUse local runtime (Docker/Lume/QEMU)
ws_urlstr or NoneNoneWebSocket URL
http_urlstr or NoneNoneHTTP URL
container_namestr or NoneNoneContainer name
cpuint or NoneNoneCPU count (cloud)
memory_mbint or NoneNoneMemory in MB (cloud)
disk_gbint or NoneNoneDisk size in GB (cloud)
regionstr'us-east-1'Cloud region

Sandbox instance methods#

MethodSignatureDescription
disconnectasync disconnect() -> NoneDrop transport connection. Sandbox keeps running.
destroyasync destroy() -> NoneDisconnect and permanently delete the sandbox.
screenshotasync screenshot(text=None, format='png', quality=95) -> bytesCapture screenshot. format: 'png' or 'jpeg'. quality: 1-95 (JPEG only).
screenshot_base64async screenshot_base64(text=None, format='png', quality=95) -> strScreenshot as base64 string.
get_environmentasync get_environment() -> strReturns 'linux', 'mac', 'windows', or 'browser'.
get_display_urlasync get_display_url(share=False) -> strURL to view the sandbox display. share=True returns a public link (cloud only).
get_dimensionsasync get_dimensions() -> tuple[int, int]Screen width and height in pixels.

Sandbox class methods#

MethodSignatureDescription
listasync list(local=False, api_key=None) -> list[SandboxInfo]List running and suspended sandboxes.
get_infoasync get_info(name, local=False, api_key=None) -> SandboxInfoMetadata for a specific sandbox.
suspendasync suspend(name, local=False, api_key=None) -> NoneSuspend a sandbox (save state). Local QEMU: QMP snapshot. Local Docker: pause. Local Lume: stop. Cloud: POST /v1/vms/{name}/stop.
resumeasync resume(name, local=False, api_key=None) -> SandboxResume a suspended sandbox. Returns connected Sandbox.
restartasync restart(name, local=False, api_key=None) -> SandboxRestart (suspend then resume). Returns connected Sandbox.
deleteasync delete(name, local=False, api_key=None) -> NonePermanently delete. Local: stops and removes state file. Cloud: DELETE /v1/vms/{name}.

Sandbox attributes#

AttributeTypeDescription
namestr or NoneSandbox name
shellShellRun shell commands
mouseMouseMouse control
keyboardKeyboardKeyboard control
screenScreenScreenshots and screen info
clipboardClipboardClipboard read/write
tunnelTunnelPort forwarding
terminalTerminalPTY sessions
windowWindowWindow management
mobileMobileMobile (Android) touch and hardware keys

SandboxInfo#

Returned by Sandbox.list() and Sandbox.get_info().

FieldTypeDescription
namestrSandbox name
statusstrCurrent status
sourcestrRuntime source
os_typestr or NoneOS type
hoststr or NoneHost address
vnc_urlstr or NoneVNC URL
api_urlstr or NoneAPI URL
created_atstr or NoneISO timestamp

configure, login, whoami#

def configure(api_key=None, base_url=None) -> None
# Set global SDK config. Equivalent to setting CUA_API_KEY.
 
def login(base_url=None) -> None
# Open Cua login page in browser; store credentials in ~/.cua/credentials (Clerk flow).
 
def whoami(api_key=None) -> Dict[str, Any]
# Return dict with user info (id, email, etc.) from Cua API.

Localhost#

Direct host control via cua_auto. No sandbox. Attributes: screen, mouse, keyboard, clipboard, shell, window, terminal.

# plain await
host = await Localhost.connect()
await host.shell.run('echo hello')
await host.disconnect()
 
# context manager
async with Localhost.connect() as host:
    await host.shell.run('echo hello')

Methods: same signatures as Sandbox (screenshot, screenshot_base64, get_environment, get_dimensions, disconnect).

See Sandbox interfaces reference for the full API of sb.shell, sb.mouse, sb.keyboard, sb.screen, sb.clipboard, sb.tunnel, sb.terminal, sb.window, and sb.mobile. See Image reference for the full Image builder API.