Skip to content
Build Templates

Build Templates

Use the SDK to build and deploy sandbox templates to the cloud.

If your template is based on an official image from a public registry like Docker Hub, it is recommended to use fromImage() and specify the full image path via a mirror proxy, for example crp-internal.sufycs.com/registry-1.docker.io/library/python:3.

Note: Template aliases are globally unique. Use a business prefix to ensure uniqueness.

Build and Wait for Completion

The build() method builds a template and waits for the build process to finish, returning information including the template ID and build ID.

javascript
import { Template } from '@e2b/code-interpreter'

async function main() {
  const template = Template().fromImage(
    'crp-internal.sufycs.com/registry-1.docker.io/library/python:3'
  )

  const buildInfo = await Template.build(template, {
    alias: 'my-template',
    cpuCount: 2,
    memoryMB: 2048,
    skipCache: false,
  })

  // Returns: { alias, templateId, buildId }
  console.log('Template ID:', buildInfo.templateId)
}

main()

Background Build

The buildInBackground() method starts a build without waiting for it to complete, suitable for scenarios where you need to check the status later.

javascript
import { Template } from '@e2b/code-interpreter'

async function main() {
  const template = Template().fromImage(
    'crp-internal.sufycs.com/registry-1.docker.io/library/python:3'
  )

  const buildInfo = await Template.buildInBackground(template, {
    alias: 'my-template',
    cpuCount: 2,
    memoryMB: 2048,
  })

  // Returns immediately: { alias, templateId, buildId }
  console.log('Build started:', buildInfo.buildId)
}

main()

## Check Build Status

The `getBuildStatus()` method checks the status of a build started with `buildInBackground()`.

```javascript
import { Template } from '@e2b/code-interpreter'

async function main() {
  // Assume buildInfo was obtained from buildInBackground()
  const buildInfo = { templateId: 'xxx', buildId: 'yyy' }

  const status = await Template.getBuildStatus(buildInfo, {
    logsOffset: 0,
  })

  // Returns: { status: 'building' | 'ready' | 'error', logEntries: [...] }
  console.log('Status:', status.status)
}

main()

Check if Alias Exists

Use the aliasExists() method to check if a template alias already exists, avoiding duplicate builds.

javascript
import { Template } from '@e2b/code-interpreter'

async function main() {
  // Check if alias exists
  const exists = await Template.aliasExists('my-template')

  if (!exists) {
    const template = Template().fromImage(
      'crp-internal.sufycs.com/registry-1.docker.io/library/python:3'
    )
    await Template.build(template, { alias: 'my-template' })
  }
}

main()

Example: Polling Background Build Status

The following example demonstrates how to use a background build and poll its status until completion.

javascript
import { Template } from '@e2b/code-interpreter'

async function main() {
  const template = Template().fromImage(
    'crp-internal.sufycs.com/registry-1.docker.io/library/python:3'
  )

  const buildInfo = await Template.buildInBackground(template, {
    alias: 'my-template',
    cpuCount: 2,
    memoryMB: 2048,
  })

  let logsOffset = 0
  let status = 'building'

  while (status === 'building') {
    const buildStatus = await Template.getBuildStatus(buildInfo, {
      logsOffset,
    })

    logsOffset += buildStatus.logEntries.length
    status = buildStatus.status

    buildStatus.logEntries.forEach(
      (logEntry) => console.log(logEntry.toString())
    )

    await new Promise(resolve => setTimeout(resolve, 2000))
  }

  if (status === 'ready') {
    console.log('Build completed successfully')
  } else {
    console.error('Build failed')
  }
}

main()