Template

class Template(TemplateBase)

Synchronous template builder for E2B sandboxes.

build

@staticmethod
def build(template: TemplateClass,
          alias: str,
          cpu_count: int = 2,
          memory_mb: int = 1024,
          skip_cache: bool = False,
          on_build_logs: Optional[Callable[[LogEntry], None]] = None,
          api_key: Optional[str] = None,
          domain: Optional[str] = None) -> BuildInfo

Build and deploy a template to E2B infrastructure.

Arguments:

  • template: The template to build
  • alias: Alias name for the template
  • cpu_count: Number of CPUs allocated to the sandbox
  • memory_mb: Amount of memory in MB allocated to the sandbox
  • skip_cache: If True, forces a complete rebuild ignoring cache
  • on_build_logs: Callback function to receive build logs during the build process
  • api_key: E2B API key for authentication
  • domain: Domain of the E2B API Example
from e2b import Template

template = (
    Template()
    .from_python_image('3')
    .copy('requirements.txt', '/home/user/')
    .run_cmd('pip install -r /home/user/requirements.txt')
)

Template.build(
    template,
    alias='my-python-env',
    cpu_count=2,
    memory_mb=1024
)

build_in_background

@staticmethod
def build_in_background(template: TemplateClass,
                        alias: str,
                        cpu_count: int = 2,
                        memory_mb: int = 1024,
                        skip_cache: bool = False,
                        on_build_logs: Optional[Callable[[LogEntry],
                                                         None]] = None,
                        api_key: Optional[str] = None,
                        domain: Optional[str] = None) -> BuildInfo

Build and deploy a template to E2B infrastructure without waiting for completion.

Arguments:

  • template: The template to build
  • alias: Alias name for the template
  • cpu_count: Number of CPUs allocated to the sandbox
  • memory_mb: Amount of memory in MB allocated to the sandbox
  • skip_cache: If True, forces a complete rebuild ignoring cache
  • api_key: E2B API key for authentication
  • domain: Domain of the E2B API

Returns:

BuildInfo containing the template ID and build ID Example

from e2b import Template

template = (
    Template()
    .from_python_image('3')
    .run_cmd('echo "test"')
    .set_start_cmd('echo "Hello"', 'sleep 1')
)

build_info = Template.build_in_background(
    template,
    alias='my-python-env',
    cpu_count=2,
    memory_mb=1024
)

get_build_status

@staticmethod
def get_build_status(build_info: BuildInfo,
                     logs_offset: int = 0,
                     api_key: Optional[str] = None,
                     domain: Optional[str] = None)

Get the status of a build.

Arguments:

  • build_info: Build identifiers returned from build_in_background
  • logs_offset: Offset for fetching logs
  • api_key: E2B API key for authentication
  • domain: Domain of the E2B API

Returns:

TemplateBuild containing the build status and logs Example

from e2b import Template

build_info = Template.build_in_background(template, alias='my-template')
status = Template.get_build_status(build_info, logs_offset=0)