Skip to main content

TemplateBase

Base class for building E2B sandbox templates.

Implements

  • TemplateFromImage
  • TemplateBuilder
  • TemplateFinal

Constructors

Parameters
ParameterType
options?TemplateOptions
Returns
TemplateBase

Methods

addMcpServer()

Install MCP servers using mcp-gateway. Note: Requires a base image with mcp-gateway pre-installed (e.g., mcp-gateway).
Parameters
ParameterTypeDescription
serverskeyof McpServer | keyof McpServer[]MCP server name(s)
Returns
TemplateBuilder
Throws
If the base template is not mcp-gateway
Example
Implementation of
TemplateBuilder.addMcpServer

aptInstall()

Install Debian/Ubuntu packages using apt-get.
Parameters
ParameterTypeDescription
packagesstring | string[]Package name(s)
options?{ fixMissing: boolean; noInstallRecommends: boolean; }-
options.fixMissing?boolean-
options.noInstallRecommends?boolean-
Returns
TemplateBuilder
Example
Implementation of
TemplateBuilder.aptInstall

betaDevContainerPrebuild()

Prebuild a devcontainer from the specified directory.
Parameters
ParameterTypeDescription
devcontainerDirectorystringPath to the devcontainer directory
Returns
TemplateBuilder
Example
Implementation of
TemplateBuilder.betaDevContainerPrebuild

betaSetDevContainerStart()

Start a devcontainer from the specified directory.
Parameters
ParameterTypeDescription
devcontainerDirectorystringPath to the devcontainer directory
Returns
TemplateFinal
Example
Implementation of
TemplateBuilder.betaSetDevContainerStart

bunInstall()

Install Bun packages using bun.
Parameters
ParameterTypeDescription
packages?string | string[]Package name(s) or undefined for package.json
options?{ dev: boolean; g: boolean; }Install options
options.dev?boolean-
options.g?boolean-
Returns
TemplateBuilder
Example
Implementation of
TemplateBuilder.bunInstall

copy()

Copy files or directories into the template.
Parameters
ParameterTypeDescription
srcPathLike | PathLike[]Source path(s)
destPathLikeDestination path
options?{ forceUpload: true; mode: number; resolveSymlinks: boolean; user: string; }Copy options
options.forceUpload?true-
options.mode?number-
options.resolveSymlinks?boolean-
options.user?string-
Returns
TemplateBuilder
Example
Implementation of
TemplateBuilder.copy

copyItems()

Copy multiple items with individual options.
Parameters
ParameterTypeDescription
itemsCopyItem[]Array of copy items
Returns
TemplateBuilder
Example
Implementation of
TemplateBuilder.copyItems

fromAWSRegistry()

Start from a Docker image in AWS ECR.
Parameters
ParameterTypeDescription
imagestringFull ECR image path
credentials{ accessKeyId: string; region: string; secretAccessKey: string; }AWS credentials
credentials.accessKeyIdstring-
credentials.regionstring-
credentials.secretAccessKeystring-
Returns
TemplateBuilder
Example
Implementation of

fromBaseImage()

Start from E2B’s default base image (e2bdev/base:latest).
Returns
TemplateBuilder
Example
Implementation of

fromBunImage()

Start from a Bun-based Docker image.
Parameters
ParameterTypeDefault valueDescription
variantstring'latest'Bun variant (default: ‘latest’)
Returns
TemplateBuilder
Example
Implementation of

fromDebianImage()

Start from a Debian-based Docker image.
Parameters
ParameterTypeDefault valueDescription
variantstring'stable'Debian variant (default: ‘stable’)
Returns
TemplateBuilder
Example
Implementation of

fromDockerfile()

Parse a Dockerfile and convert it to Template SDK format.
Parameters
ParameterTypeDescription
dockerfileContentOrPathstringDockerfile content or path
Returns
TemplateBuilder
Example
Implementation of

fromGCPRegistry()

Start from a Docker image in Google Container Registry.
Parameters
ParameterTypeDescription
imagestringFull GCR/GAR image path
credentials{ serviceAccountJSON: string | object; }GCP service account credentials
credentials.serviceAccountJSONstring | object-
Returns
TemplateBuilder
Example
Implementation of

fromImage()

Start from a custom Docker image.
Parameters
ParameterTypeDescription
baseImagestringDocker image name
credentials?{ password: string; username: string; }Optional credentials for private registries
credentials.password?string-
credentials.username?string-
Returns
TemplateBuilder
Example
Implementation of

fromNodeImage()

Start from a Node.js-based Docker image.
Parameters
ParameterTypeDefault valueDescription
variantstring'lts'Node.js variant (default: ‘lts’)
Returns
TemplateBuilder
Example
Implementation of

fromPythonImage()

Start from a Python-based Docker image.
Parameters
ParameterTypeDefault valueDescription
versionstring'3'Python version (default: ‘3’)
Returns
TemplateBuilder
Example
Implementation of

fromTemplate()

Start from an existing E2B template.
Parameters
ParameterTypeDescription
templatestringE2B template ID or alias
Returns
TemplateBuilder
Example
Implementation of

fromUbuntuImage()

Start from an Ubuntu-based Docker image.
Parameters
ParameterTypeDefault valueDescription
variantstring'latest'Ubuntu variant (default: ‘latest’)
Returns
TemplateBuilder
Example
Implementation of

gitClone()

Clone a Git repository.
Parameters
ParameterTypeDescription
urlstringRepository URL
path?PathLikeOptional destination path
options?{ branch: string; depth: number; user: string; }Clone options
options.branch?string-
options.depth?number-
options.user?string-
Returns
TemplateBuilder
Example
Implementation of
TemplateBuilder.gitClone

makeDir()

Create directories.
Parameters
ParameterTypeDescription
pathPathLike | PathLike[]Directory path(s)
options?{ mode: number; user: string; }Directory options
options.mode?number-
options.user?string-
Returns
TemplateBuilder
Example
Implementation of
TemplateBuilder.makeDir
Create a symbolic link.
Parameters
ParameterTypeDescription
srcPathLikeSource path (target)
destPathLikeDestination path (symlink location)
options?{ force: boolean; user: string; }Symlink options
options.force?boolean-
options.user?string-
Returns
TemplateBuilder
Example
Implementation of
TemplateBuilder.makeSymlink

npmInstall()

Install Node.js packages using npm.
Parameters
ParameterTypeDescription
packages?string | string[]Package name(s) or undefined for package.json
options?{ dev: boolean; g: boolean; }Install options
options.dev?boolean-
options.g?boolean-
Returns
TemplateBuilder
Example
Implementation of
TemplateBuilder.npmInstall

pipInstall()

Install Python packages using pip.
Parameters
ParameterTypeDescription
packages?string | string[]Package name(s) or undefined for current directory
options?{ g: boolean; }Install options
options.g?booleanInstall globally as root (default: true). Set to false for user-only installation with —user flag
Returns
TemplateBuilder
Example
Implementation of
TemplateBuilder.pipInstall

remove()

Remove files or directories.
Parameters
ParameterTypeDescription
pathPathLike | PathLike[]Path(s) to remove
options?{ force: boolean; recursive: boolean; user: string; }Remove options
options.force?boolean-
options.recursive?boolean-
options.user?string-
Returns
TemplateBuilder
Example
Implementation of
TemplateBuilder.remove

rename()

Rename or move a file or directory.
Parameters
ParameterTypeDescription
srcPathLikeSource path
destPathLikeDestination path
options?{ force: boolean; user: string; }Rename options
options.force?boolean-
options.user?string-
Returns
TemplateBuilder
Example
Implementation of
TemplateBuilder.rename

runCmd()

Call Signature
Run a shell command.
Parameters
ParameterTypeDescription
commandstringCommand string
options?{ user: string; }Command options
options.user?string-
Returns
TemplateBuilder
Example
Implementation of
TemplateBuilder.runCmd
Call Signature
Run multiple shell commands.
Parameters
ParameterTypeDescription
commandsstring[]Array of command strings
options?{ user: string; }Command options
options.user?string-
Returns
TemplateBuilder
Implementation of
TemplateBuilder.runCmd

setEnvs()

Set environment variables. Note: Environment variables defined here are available only during template build.
Parameters
ParameterTypeDescription
envsRecord<string, string>Environment variables
Returns
TemplateBuilder
Example
Implementation of
TemplateBuilder.setEnvs

setReadyCmd()

Set or update the ready check command.
Parameters
ParameterTypeDescription
readyCommandstring | ReadyCmdCommand to check readiness
Returns
TemplateFinal
Example
Implementation of
TemplateBuilder.setReadyCmd

setStartCmd()

Set the start command and ready check.
Parameters
ParameterTypeDescription
startCommandstringCommand to run on startup
readyCommandstring | ReadyCmdCommand to check readiness
Returns
TemplateFinal
Example
Implementation of
TemplateBuilder.setStartCmd

setUser()

Set the user for subsequent commands.
Parameters
ParameterTypeDescription
userstringUsername
Returns
TemplateBuilder
Example
Implementation of
TemplateBuilder.setUser

setWorkdir()

Set the working directory.
Parameters
ParameterTypeDescription
workdirPathLikeWorking directory path
Returns
TemplateBuilder
Example
Implementation of
TemplateBuilder.setWorkdir

skipCache()

Skip cache for all subsequent build instructions from this point.
Returns
this
Example
Implementation of
TemplateBuilder.skipCache

aliasExists()

Check if a template with the given alias exists.
Parameters
ParameterTypeDescription
aliasstringTemplate alias to check
options?ConnectionOptsAuthentication options
Returns
Promise<boolean> True if the alias exists, false otherwise
Deprecated
Use exists instead.
Example

assignTags()

Assign tag(s) to an existing template build.
Parameters
ParameterTypeDescription
targetNamestringTemplate name in ‘name:tag’ format (the source build to tag from)
tagsstring | string[]Tag or tags to assign
options?ConnectionOptsAuthentication options
Returns
Promise<TemplateTagInfo> Tag info with buildId and assigned tags
Example

build()

Call Signature
Build and deploy a template to E2B infrastructure.
Parameters
ParameterTypeDescription
templateTemplateClassThe template to build
namestringTemplate name in ‘name’ or ‘name:tag’ format
options?Omit<BuildOptions, "alias">Optional build configuration options
Returns
Promise<BuildInfo>
Example
Call Signature
Build and deploy a template to E2B infrastructure.
Parameters
ParameterTypeDescription
templateTemplateClassThe template to build
optionsBuildOptionsBuild configuration options with alias (deprecated)
Returns
Promise<BuildInfo>
Deprecated
Use the overload with name parameter instead.
Example

buildInBackground()

Call Signature
Build and deploy a template to E2B infrastructure without waiting for completion.
Parameters
ParameterTypeDescription
templateTemplateClassThe template to build
namestringTemplate name in ‘name’ or ‘name:tag’ format
options?Omit<BuildOptions, "alias">Optional build configuration options
Returns
Promise<BuildInfo>
Example
Call Signature
Build and deploy a template to E2B infrastructure without waiting for completion.
Parameters
ParameterTypeDescription
templateTemplateClassThe template to build
optionsBuildOptionsBuild configuration options with alias (deprecated)
Returns
Promise<BuildInfo>
Deprecated
Use the overload with name parameter instead.
Example

exists()

Check if a template with the given name exists.
Parameters
ParameterTypeDescription
namestringTemplate name to check
options?ConnectionOptsAuthentication options
Returns
Promise<boolean> True if the name exists, false otherwise
Example

getBuildStatus()

Get the status of a build.
Parameters
ParameterTypeDescription
dataPick<BuildInfo, "buildId" | "templateId">Build identifiers
options?GetBuildStatusOptionsAuthentication options
Returns
Promise<TemplateBuildStatusResponse>
Example

getTags()

Get all tags for a template.
Parameters
ParameterTypeDescription
templateIdstringTemplate ID or name
options?ConnectionOptsAuthentication options
Returns
Promise<TemplateTag[]> Array of tag details including tag name, buildId, and creation date
Example

removeTags()

Remove tag(s) from a template.
Parameters
ParameterTypeDescription
namestringTemplate name
tagsstring | string[]Tag or tags to remove
options?ConnectionOptsAuthentication options
Returns
Promise<void>
Example

toDockerfile()

Convert a template to Dockerfile format. Note: Templates based on other E2B templates cannot be converted to Dockerfile.
Parameters
ParameterTypeDescription
templateTemplateClassThe template to convert
Returns
string Dockerfile string representation
Throws
Error if the template is based on another E2B template

toJSON()

Convert a template to JSON representation.
Parameters
ParameterTypeDefault valueDescription
templateTemplateClassundefinedThe template to convert
computeHashesbooleantrueWhether to compute file hashes for cache invalidation
Returns
Promise<string> JSON string representation of the template

Interfaces

TemplateBuilder

Main builder state for constructing templates. Provides methods for customizing the template environment.

Methods

addMcpServer()

Install MCP servers using mcp-gateway. Note: Requires a base image with mcp-gateway pre-installed (e.g., mcp-gateway).
Parameters
ParameterTypeDescription
serverskeyof McpServer | keyof McpServer[]MCP server name(s)
Returns
TemplateBuilder
Throws
If the base template is not mcp-gateway
Example

aptInstall()

Install Debian/Ubuntu packages using apt-get.
Parameters
ParameterTypeDescription
packagesstring | string[]Package name(s)
options?{ fixMissing: boolean; noInstallRecommends: boolean; }-
options.fixMissing?boolean-
options.noInstallRecommends?boolean-
Returns
TemplateBuilder
Example

betaDevContainerPrebuild()

Prebuild a devcontainer from the specified directory.
Parameters
ParameterTypeDescription
devcontainerDirectorystringPath to the devcontainer directory
Returns
TemplateBuilder
Example

betaSetDevContainerStart()

Start a devcontainer from the specified directory.
Parameters
ParameterTypeDescription
devcontainerDirectorystringPath to the devcontainer directory
Returns
TemplateFinal
Example

bunInstall()

Install Bun packages using bun.
Parameters
ParameterTypeDescription
packages?string | string[]Package name(s) or undefined for package.json
options?{ dev: boolean; g: boolean; }Install options
options.dev?boolean-
options.g?boolean-
Returns
TemplateBuilder
Example

copy()

Copy files or directories into the template.
Parameters
ParameterTypeDescription
srcPathLike | PathLike[]Source path(s)
destPathLikeDestination path
options?{ forceUpload: true; mode: number; resolveSymlinks: boolean; user: string; }Copy options
options.forceUpload?true-
options.mode?number-
options.resolveSymlinks?boolean-
options.user?string-
Returns
TemplateBuilder
Example

copyItems()

Copy multiple items with individual options.
Parameters
ParameterTypeDescription
itemsCopyItem[]Array of copy items
Returns
TemplateBuilder
Example

gitClone()

Clone a Git repository.
Parameters
ParameterTypeDescription
urlstringRepository URL
path?PathLikeOptional destination path
options?{ branch: string; depth: number; user: string; }Clone options
options.branch?string-
options.depth?number-
options.user?string-
Returns
TemplateBuilder
Example

makeDir()

Create directories.
Parameters
ParameterTypeDescription
pathPathLike | PathLike[]Directory path(s)
options?{ mode: number; user: string; }Directory options
options.mode?number-
options.user?string-
Returns
TemplateBuilder
Example
Create a symbolic link.
Parameters
ParameterTypeDescription
srcPathLikeSource path (target)
destPathLikeDestination path (symlink location)
options?{ force: boolean; user: string; }Symlink options
options.force?boolean-
options.user?string-
Returns
TemplateBuilder
Example

npmInstall()

Install Node.js packages using npm.
Parameters
ParameterTypeDescription
packages?string | string[]Package name(s) or undefined for package.json
options?{ dev: boolean; g: boolean; }Install options
options.dev?boolean-
options.g?boolean-
Returns
TemplateBuilder
Example

pipInstall()

Install Python packages using pip.
Parameters
ParameterTypeDescription
packages?string | string[]Package name(s) or undefined for current directory
options?{ g: boolean; }Install options
options.g?booleanInstall globally as root (default: true). Set to false for user-only installation with —user flag
Returns
TemplateBuilder
Example

remove()

Remove files or directories.
Parameters
ParameterTypeDescription
pathPathLike | PathLike[]Path(s) to remove
options?{ force: boolean; recursive: boolean; user: string; }Remove options
options.force?boolean-
options.recursive?boolean-
options.user?string-
Returns
TemplateBuilder
Example

rename()

Rename or move a file or directory.
Parameters
ParameterTypeDescription
srcPathLikeSource path
destPathLikeDestination path
options?{ force: boolean; user: string; }Rename options
options.force?boolean-
options.user?string-
Returns
TemplateBuilder
Example

runCmd()

Call Signature
Run a shell command.
Parameters
ParameterTypeDescription
commandstringCommand string
options?{ user: string; }Command options
options.user?string-
Returns
TemplateBuilder
Example
Call Signature
Run multiple shell commands.
Parameters
ParameterTypeDescription
commandsstring[]Array of command strings
options?{ user: string; }Command options
options.user?string-
Returns
TemplateBuilder
Call Signature
Run command(s).
Parameters
ParameterTypeDescription
commandOrCommandsstring | string[]Command or commands
options?{ user: string; }Command options
options.user?string-
Returns
TemplateBuilder

setEnvs()

Set environment variables. Note: Environment variables defined here are available only during template build.
Parameters
ParameterTypeDescription
envsRecord<string, string>Environment variables
Returns
TemplateBuilder
Example

setReadyCmd()

Set or update the ready check command.
Parameters
ParameterTypeDescription
readyCommandstring | ReadyCmdCommand to check readiness
Returns
TemplateFinal
Example

setStartCmd()

Set the start command and ready check.
Parameters
ParameterTypeDescription
startCommandstringCommand to run on startup
readyCommandstring | ReadyCmdCommand to check readiness
Returns
TemplateFinal
Example

setUser()

Set the user for subsequent commands.
Parameters
ParameterTypeDescription
userstringUsername
Returns
TemplateBuilder
Example

setWorkdir()

Set the working directory.
Parameters
ParameterTypeDescription
workdirPathLikeWorking directory path
Returns
TemplateBuilder
Example

skipCache()

Skip cache for all subsequent build instructions from this point.
Returns
this
Example

Type Aliases

BuildInfo

Information about a built template.

Type declaration

NameTypeDescription
aliasstringFirst alias from the build (for backward compatibility). Deprecated Use name instead.
buildIdstringBuild identifier.
namestringName of the template.
tagsstring[]Tags assigned to this build.
templateIdstringTemplate identifier.

BuildOptions

Options for building a template with authentication.

BuildStatusReason

Reason for the current build status (typically for errors).

Type declaration

NameTypeDescription
logEntriesLogEntry[]Log entries related to the status reason.
messagestringMessage with the status reason.
step?stringStep that failed.

CopyItem

Configuration for a single file/directory copy operation.

Type declaration

NameType
destPathLike
forceUpload?true
mode?number
resolveSymlinks?boolean
srcPathLike | PathLike[]
user?string

GetBuildStatusOptions

Options for getting build status.

Type declaration

NameType
logsOffset?number

McpServerName

MCP server names that can be installed.

TemplateBuildStatus

Status of a template build.

TemplateBuildStatusResponse

Response from getting build status.

Type declaration

NameTypeDescription
buildIDstringBuild identifier.
logEntriesLogEntry[]Build log entries.
logsstring[]Build logs (raw strings). Deprecated Use logEntries instead.
reason?BuildStatusReasonReason for the current status (typically for errors).
statusTemplateBuildStatusCurrent status of the build.
templateIDstringTemplate identifier.

TemplateClass

Type representing a template in any state (builder or final).

TemplateTag

Detailed information about a single template tag.

Type declaration

NameTypeDescription
buildIdstringBuild identifier associated with this tag.
createdAtDateWhen this tag was assigned.
tagstringName of the tag.

TemplateTagInfo

Information about assigned template tags.

Type declaration

NameTypeDescription
buildIdstringBuild identifier associated with this tag.
tagsstring[]Assigned tags of the template.

Functions

Template()

Create a new E2B template builder instance.

Parameters

ParameterTypeDescription
options?TemplateOptionsOptional configuration for the template builder

Returns

TemplateFromImage A new template builder instance

Example