Skip to main content
Templates provide a code-first approach to defining sandbox environments. Instead of configuring images manually, you define them programmatically using the SDK. The system supports two workflows:
  1. Declarative images — build images with varying dependencies on demand when creating sandboxes
  2. Pre-built snapshots — create and register ready-to-use snapshots that can be shared across multiple sandboxes

Declarative image building

Build images on-the-fly when creating sandboxes. Ideal for iterating quickly without creating separate snapshots. Declarative images are cached by content hash — identical manifests produce the same image. Subsequent runs reuse the cached image instantly.

Sandbox.create(image=..., on_build_log=...)

When you pass an image to Sandbox.create(), the server:
  1. Hashes the image manifest to compute a cache key
  2. If cached, creates the sandbox from the existing checkpoint instantly
  3. If not cached, boots a build sandbox, executes each step, checkpoints the result, then creates your sandbox from it
Image
A declarative image definition built with the Image builder.
Callable[[str], None]
Callback for streaming build log messages via SSE. Receives step-by-step progress updates during image building.

Creating pre-built snapshots

Create named snapshots that persist permanently and can be shared across sandboxes. Snapshots are visible in the dashboard and don’t need to be rebuilt.

Snapshots(api_key=..., api_url=...)

str
API key. Falls back to OPENCOMPUTER_API_KEY env var.
str
API base URL. Falls back to OPENCOMPUTER_API_URL env var.

await snapshots.create(name, image, on_build_logs=None)

Creates a pre-built snapshot from a declarative image.
str
required
Unique name for this snapshot.
Image
required
Declarative image definition.
Callable[[str], None]
Callback for streaming build log messages.
Returns: dict with snapshot info (id, name, status, etc.)

await snapshots.list()

Returns all named snapshots for the current organization.
Returns: list[dict]

await snapshots.get(name)

Gets a snapshot by name.
str
required
Snapshot name.
Returns: dict

await snapshots.delete(name)

Deletes a named snapshot. Existing sandboxes created from it are not affected.
str
required
Snapshot name to delete.
Returns: None

Sandbox.create(snapshot=...)

Create a sandbox from a pre-built snapshot by name.
str
Name of a pre-built snapshot to create the sandbox from.
Returns: Sandbox

Image configuration

The Image class provides a fluent, immutable API for defining sandbox environments. Each method returns a new Image instance — the original is never modified.

Image.base()

Creates a new image starting from the default OpenSandbox environment (Ubuntu 22.04 with Python, Node.js, build tools, and common utilities).

image.apt_install(packages)

Install system packages via apt-get.
list[str]
required
List of apt package names to install.

image.pip_install(packages)

Install Python packages via pip.
list[str]
required
List of pip package names to install.

image.run_commands(*commands)

Run one or more shell commands during image build.
str (variadic)
required
Shell commands to execute sequentially.

image.env(vars)

Set environment variables. Written to /etc/environment so they’re available in all sessions.
dict[str, str]
required
Key-value pairs of environment variables.

image.workdir(path)

Set the default working directory.
str
required
Absolute path for the working directory.

image.add_file(remote_path, content)

Embed a file with inline content into the image.
str
required
Absolute path inside the sandbox.
str
required
String content of the file.

image.add_local_file(local_path, remote_path)

Read a file from the local machine and embed it into the image.
str
required
Path to the file on the local machine.
str
required
Absolute path inside the sandbox.

image.add_local_dir(local_path, remote_path)

Recursively read a local directory and embed all files into the image.
str
required
Path to the directory on the local machine.
str
required
Absolute path inside the sandbox where the directory will be created.

image.to_dict()

Returns the image manifest as a plain dict. Returns: dict

image.cache_key()

Computes a deterministic SHA-256 hash of the manifest for cache lookups. Returns: str

Snapshot info fields


Complete example