Skip to main content
E2B templates allow you to define custom sandboxes. You can define the base image, environment variables, files to copy, commands to run, and a start command that will be already running when you spawn the sandbox. This way, you can have fully configured sandboxes with running processes ready to use with zero wait time for your users. There are two ways how you can start creating a new template:
  • using the CLI
  • manually using the SDK

CLI

You can use the E2B CLI to create a new template.
1

Install the E2B CLI

Install the latest version of the E2B CLI
2

Initialize a new template

e2b template init
3

Follow the prompts

Follow the prompts to create a new template.
4

Done

Check the generated README.md file to see how to build and use your new template.

Manual

Install the packages

Requires the E2B SDK version at least 2.3.0
npm install e2b dotenv
Create the .env file
E2B_API_KEY=e2b_***

Create a new template file

Create a template file with the following name and content
// template.ts
import { Template, waitForTimeout } from "e2b";

export const template = Template()
  .fromBaseImage()
  .setEnvs({
    HELLO: "Hello, World!",
  })
  .setStartCmd("echo $HELLO", waitForTimeout(5_000));

Create a development build script

// build.dev.ts
import "dotenv/config";
import { Template, defaultBuildLogger } from "e2b";
import { template } from "./template";

async function main() {
  await Template.build(template, {
    alias: "template-tag-dev",
    cpuCount: 1,
    memoryMB: 1024,
    onBuildLogs: defaultBuildLogger(),
  });
}

main().catch(console.error);

Create a production build script

// build.prod.ts
import "dotenv/config";
import { Template, defaultBuildLogger } from "e2b";
import { template } from "./template";

async function main() {
  await Template.build(template, {
    alias: "template-tag",
    cpuCount: 1,
    memoryMB: 1024,
    onBuildLogs: defaultBuildLogger(),
  });
}

main().catch(console.error);

Build the template

Build the development template
npx tsx build.dev.ts
Build the production template
npx tsx build.prod.ts

Create a new Sandbox from the Template

import "dotenv/config";
import { Sandbox } from "e2b";

// Create a Sandbox from development template
const sandbox = await Sandbox.create("template-tag-dev");

// Create a Sandbox from production template
const sandbox = await Sandbox.create("template-tag");
The template alias is the identifier that can be used to create a new Sandbox.
I