Skip to main content
The Template class creates and manages reusable sandbox base images. Templates are built once and then used to launch any number of identically configured sandboxes in seconds. See the Templates overview for concepts, or jump to the Create a template guide for the end-to-end flow.

Import

Factory methods

Template.create

Register a new template and queue its first build. Returns as soon as the template row exists and a build_id is assigned - the build itself runs asynchronously. Call waitUntilReady to block until the build finishes.
Options: Throws SandboxError if the API response is missing build_id.

Template.connect

Load an existing template by UUID.

Template.list

List all templates visible to the authenticated team (includes system templates).
Returns an array of TemplateInfo.

Template.deleteById / Template.delete_by_id

Delete a template by UUID without instantiating it. Idempotent on 404.
Throws ConflictError if any non-destroyed sandbox still references the template. The template’s name is released for reuse as soon as the call succeeds.

Methods on template

getInfo / get_info

Fetch the current server-side state. Returns a fresh TemplateInfo - the template’s own properties are snapshots and are not mutated.

waitUntilReady / wait_until_ready

Block until the current build reaches a terminal status. The SDK polls GET /templates/{id}/builds/{buildId} for the authoritative build status — SSE is used only for live log delivery when onLog / on_log is provided, never to detect termination. Returns a refreshed TemplateInfo with status = "ready".
Options: Throws:
  • BuildError if the build transitions to failed. The backend’s error_message follows a "<code>: <detail>" convention; the SDK splits it so BuildError.code is the stable prefix and BuildError.message is the human-readable detail. See BuildSpec: Build error codes.
  • ConflictError if the build is cancelled

streamBuildLogs / stream_build_logs

Pure SSE stream of build logs. The promise resolves when the stream closes. Does not wait for a terminal status - use waitUntilReady for that.
Options: Throws SandboxError if no buildId is supplied and the template has no latestBuildId.

rebuild

Queue a new build for this template. Returns the new TemplateBuildInfo. Idempotent: if an in-flight build already matches the same configuration, the existing build is returned.

listBuilds / list_builds

List recent builds for this template, newest first.
Returns an array of TemplateBuildInfo.

getBuild / get_build

Fetch a single build by ID.

cancelBuild / cancel_build

Cancel an in-flight build. Idempotent - no-op for builds already in a terminal state, and for 404.

delete

Delete this template. Idempotent on 404.
Throws ConflictError if any non-destroyed sandbox still references the template - destroy those sandboxes first.

Properties on template

All properties are read-only snapshots taken at construction. Call getInfo() / get_info() to refresh.

Types

TemplateStatus

  • pending - template exists; first build is queued
  • building - a build is in progress
  • ready - at least one successful build exists; sandboxes can boot from this template
  • failed - the latest build failed; rebuild or fix the spec

TemplateBuildStatus

TemplateInfo

TemplateBuildInfo

BuildLogEvent

stream == "system" events are status messages from the build runner (build-step boundaries, snapshot progress). Filter them out when rendering to a user-facing console. finished: true marks the last event in a stream and carries the terminal status.

BuildStep

A discriminated union - exactly one of run / env / workdir / user must be set per step. See BuildSpec: Steps for semantics.

BuildError

Thrown by waitUntilReady / wait_until_ready when the build transitions to failed.
The backend’s error_message follows a "<code>: <detail>" convention (e.g. "image_too_large: image is too large for the requested disk_mib"). The SDK splits it so code holds the prefix and the error message holds the detail. When the backend doesn’t include a prefix, code falls back to "build_failed" and the message falls back to "Template build failed". See the full list of build error codes and the general Errors reference.

Retries

GET and DELETE requests (including Template.list, Template.connect, getInfo, listBuilds, getBuild, cancelBuild, delete, deleteById) auto-retry on 429, 502, 503, 504, and network errors with exponential backoff + jitter (3 attempts max). POST requests (create, rebuild) are not retried - the SDK surfaces the error so you can decide.

Errors

Template methods commonly raise:
  • AuthenticationError - missing or invalid API key
  • ValidationError - bad request body (invalid name, unsupported base image, steps with multiple keys)
  • NotFoundError - template or build doesn’t exist (except delete / deleteById / cancelBuild, which swallow 404)
  • ConflictError - template has dependent sandboxes (on delete) or build was cancelled (on waitUntilReady)
  • BuildError - build transitioned to failed (from waitUntilReady only)
  • ServerError - platform error
See Errors for the full hierarchy.