Skip to main content
Complete API reference for the BoxLite Python SDK. Version: 0.5.3 Python: 3.10+ Platforms: macOS (Apple Silicon), Linux (x86_64, ARM64)

Installation

pip install boxlite
For the sync API (greenlet-based):
pip install boxlite[sync]

Runtime Management

boxlite.Boxlite

The main runtime for creating and managing boxes.
from boxlite import Boxlite, Options, BoxOptions

Class Methods

MethodSignatureDescription
default()() -> BoxliteCreate runtime with default settings (~/.boxlite)
__init__()(options: Options) -> BoxliteCreate runtime with custom options

Instance Methods

MethodSignatureDescription
create()(options: BoxOptions, name: str = None) -> BoxCreate a new box (async)
get()(box_id: str) -> BoxReattach to an existing box by ID (async)
list()() -> List[BoxInfo]List all boxes (async)
metrics()() -> RuntimeMetricsGet runtime-wide metrics (async)

Example

# Default runtime
runtime = Boxlite.default()

# Custom runtime
runtime = Boxlite(Options(home_dir="/custom/path"))

# Create a box
box = await runtime.create(BoxOptions(image="alpine:latest"))

# List all boxes
for info in await runtime.list():
    print(f"{info.id}: {info.status}")

boxlite.Options

Runtime configuration options.
FieldTypeDefaultDescription
home_dirstr~/.boxliteBase directory for runtime data
image_registriesList[str][]Custom image registries for unqualified references

boxlite.BoxOptions

Configuration options for creating a box.
FieldTypeDefaultDescription
imagestrRequiredOCI image URI (e.g., "python:slim", "alpine:latest")
cpusint1Number of CPU cores (1 to host CPU count)
memory_mibint512Memory limit in MiB (128-65536)
disk_size_gbint | NoneNonePersistent disk size in GB (None = ephemeral)
working_dirstr"/root"Working directory inside container
envList[Tuple[str, str]][]Environment variables as (key, value) pairs
volumesList[Tuple[str, str, str]][]Volume mounts as (host_path, guest_path, mode)
portsList[Tuple[int, int, str]][]Port forwarding as (host_port, guest_port, protocol)
auto_removeboolTrueAuto cleanup when stopped
detachboolFalseSurvive parent process exit

Volume Mount Format

volumes=[
    ("/host/config", "/etc/app/config", "ro"),  # Read-only
    ("/host/data", "/mnt/data", "rw"),          # Read-write
]

Port Forwarding Format

ports=[
    (8080, 80, "tcp"),    # HTTP
    (5432, 5432, "tcp"),  # PostgreSQL
    (53, 53, "udp"),      # DNS
]

Box Handle

boxlite.Box

Handle to a running or stopped box.

Properties

PropertyTypeDescription
idstrUnique box identifier (ULID format)

Methods

MethodSignatureDescription
exec()(cmd, args, env, tty) -> ExecutionExecute command (async)
stop()() -> NoneStop the box gracefully (async)
remove()() -> NoneDelete box and its data (async)
info()() -> BoxInfoGet box metadata (async)
metrics()() -> BoxMetricsGet resource usage metrics (async)

boxlite.BoxInfo

Metadata about a box.
FieldTypeDescription
idstrUnique box identifier (ULID)
namestr | NoneOptional user-assigned name
statusstrCurrent status: "running", "stopped", "created"
created_atdatetimeCreation timestamp
pidint | NoneProcess ID (if running)
imagestrOCI image used
cpusintAllocated CPU cores
memory_mibintAllocated memory in MiB

boxlite.BoxStateInfo

Detailed state information for a box.
ValueDescription
CreatedBox created but not yet started
StartingBox is initializing
RunningBox is running and ready
StoppingBox is shutting down
StoppedBox is stopped
FailedBox encountered an error

Sync API

Synchronous wrappers using greenlet fiber switching. Requires pip install boxlite[sync].
from boxlite import SyncBoxlite, SyncBox, SyncSimpleBox, SyncCodeBox

When to Use

Use CaseAPI
New async applicationsAsync API (default)
Existing sync codebaseSync API
Jupyter notebooksSync API
REPL/interactive useSync API
Inside async functionsAsync API only

Comparison Table

Async APISync APINotes
BoxliteSyncBoxlite
BoxSyncBox
ExecutionSyncExecution
ExecStdoutSyncExecStdoutRegular iterator
ExecStderrSyncExecStderrRegular iterator
SimpleBoxSyncSimpleBox
CodeBoxSyncCodeBox

Classes

SyncBoxlite

from boxlite import SyncBoxlite, BoxOptions

with SyncBoxlite.default() as runtime:
    box = runtime.create(BoxOptions(image="alpine:latest"))
    execution = box.exec("echo", ["Hello"])
    for line in execution.stdout():
        print(line)
    box.stop()

SyncSimpleBox

from boxlite import SyncSimpleBox

with SyncSimpleBox(image="python:slim") as box:
    result = box.exec("python", "-c", "print('Hello!')")
    print(result.stdout)

SyncCodeBox

from boxlite import SyncCodeBox

with SyncCodeBox() as cb:
    result = cb.run("print('Hello, World!')")
    print(result)

Architecture

The sync API uses greenlet fiber switching:
  1. A dispatcher fiber runs the asyncio event loop
  2. User code runs in the main fiber
  3. Sync methods switch to dispatcher, await async operations, then switch back
The sync API cannot be used inside an async context (when an event loop is already running).

Constants

Default values used by BoxLite.
ConstantValueDescription
DEFAULT_CPUS1Default CPU cores
DEFAULT_MEMORY_MIB2048Default memory in MiB
COMPUTERBOX_CPUS2ComputerBox default CPUs
COMPUTERBOX_MEMORY_MIB2048ComputerBox default memory
COMPUTERBOX_DISPLAY_WIDTH1024Screen width in pixels
COMPUTERBOX_DISPLAY_HEIGHT768Screen height in pixels
COMPUTERBOX_GUI_HTTP_PORT3000HTTP GUI port
COMPUTERBOX_GUI_HTTPS_PORT3001HTTPS GUI port
DESKTOP_READY_TIMEOUT60Desktop ready timeout (seconds)

See Also