Skip to main content
Templates let you pre-build sandbox images with specific packages, files, and environment variables. Once built, reference a template by alias in Sandbox.create({ template: 'my-alias' }).

TemplateBase

TemplateBase is a fluent class for defining template contents. Chain methods to build the definition, then pass it to Template.build().

.fromBaseImage(image?)

Set the base Docker image.
string
default:"'ubuntu:22.04'"
Docker image tag.
Returns this

.aptInstall(...packages)

Install apt packages.
string[]
required
One or more package names to install via apt-get install.
Returns this

.runCmd(cmds)

Add a build-time command (equivalent to a Dockerfile RUN).
string[]
required
Command as an array of strings (executable + arguments).
Returns this

.copy(src, dst, mode?)

Copy a local file into the image at build time.
Not supported yet. A template build cannot upload local files, so building a template that uses copy() throws InvalidArgumentError. Fetch the file in a runCmd step (for example with curl), or build from a Dockerfile with fromDockerfile().
string
required
Local path to the file.
string
required
Destination path inside the image.
number
Unix file permission bits (e.g. 0o755 for executable).
Returns this

.setEnvs(envs)

Set environment variables for commands run in sandboxes created from the template. A sandbox’s own envs win on conflict.
Record<string, string>
required
Key-value pairs to set as environment variables.
Returns this

.setStartCmd(cmd)

Set a command to run when the sandbox boots.
The start command is recorded with the template but is not yet run when a sandbox boots. Start long-running processes with sandbox.commands.run(cmd, { background: true }) after creating the sandbox.
string
required
Shell command to execute on sandbox start.
Returns this

.toJSON()

Serialize the template to a JSON-friendly object for the API. Returns Record<string, any>

Template

Template is a static class for submitting and querying template builds. All methods are async.

Template.build()

Build a template and wait for the build to finish. Builds usually take several minutes; while waiting, each new line of build output is passed to onBuildLogs.
TemplateBase
required
The template definition to build.
string
required
Human-readable name used as template in Sandbox.create().
TemplateBuildOpts
Optional build options.

TemplateBuildOpts

number
Number of CPUs for the build worker.
number
Memory in MB for the build worker.
number
default:"4096"
Disk size in MB for the template: 512, 2048, 4096, 8192 or 16384. Other sizes round up to the next one.
(log: string) => void
Called with each new line of build output while the build runs. The server keeps the newest 2,000 lines; if more arrive between two checks, a ... [earlier build output truncated] line marks the gap.
number
default:"3600000"
Milliseconds to wait for the build to finish. If it is still running, TimeoutError is thrown; the build keeps running, and getBuildStatus() follows it.
string
API key override.
string
Domain override.
number
Per-request HTTP timeout in milliseconds.
Returns Promise<BuildInfo> with status completed. Throws
  • BuildError — the build failed. The message quotes the end of the build output; .buildId and .logs hold the rest.
  • TimeoutError — the build was still running after buildTimeout.
  • The error from a status check that keeps failing: temporary failures (5xx, 429, no response) are retried for up to two minutes, other errors (e.g. NotFoundError) end the wait at once. The build itself keeps running.
  • InvalidArgumentError — the template uses copy(), which is not supported yet.

Template.buildInBackground()

Submit a template build and return immediately without waiting.
Parameters are the same as Template.build() except onBuildLogs and buildTimeout, which only apply while waiting. Returns Promise<BuildInfo> with status building.

Template.rebuild()

Needs @declaw/sdk 1.6.0 or later. Upgrade with npm install @declaw/sdk@latest.
Retry a template whose build failed, and wait for the rebuild like build(). The rebuild reuses the template’s stored spec.
Templates are immutable once built, so a ready template, or one whose build is still running, is refused with ConflictError. A failed template keeps its alias: build() with the same alias is refused with a ConflictError whose message names the template to rebuild. Options (TemplateRebuildOpts) are onBuildLogs, buildTimeout, apiKey, domain and requestTimeout, as for build(); there are no resources to set. Returns Promise<BuildInfo> with status completed. Throws BuildError, ConflictError, TimeoutError.

Template.rebuildInBackground()

Queue a rebuild of a failed template and return as soon as the server has accepted it, with status building. Follow it with getBuildStatus().
Returns Promise<BuildInfo> with status building.

Template.getBuildStatus()

Poll the status of a background build.
string
required
Build ID from a previous build() or buildInBackground() call. Must be alphanumeric with hyphens/underscores.
GetBuildStatusOpts
Optional: apiKey, domain, requestTimeout.
Returns Promise<TemplateBuildStatus>

Data models

BuildInfo

TemplateBuildStatus

CopyItem

GetBuildStatusOpts


Polling a background build

Using a template