Quickstart
Start with custom sandbox templates
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 runs during the template build and is captured in a snapshot — so the process is already running when you create a sandbox from that template. This gives you 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.
Install the E2B CLI
Install the latest version of the E2B CLI
Initialize a new template
e2b template initFollow the prompts
Follow the prompts to create a new template.
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 dotenvCreate 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, '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, 'template-tag', {
cpuCount: 1,
memoryMB: 1024,
onBuildLogs: defaultBuildLogger(),
});
}
main().catch(console.error);Build the template
Build the development template
npx tsx build.dev.tsBuild the production template
npx tsx build.prod.tsCreate 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 name is the identifier that can be used to create a new Sandbox.
Scaling templates
You can create a large number of templates with low overhead, for example one template per customer, per project, or per agent run. There’s currently no limit on the number of templates, though pricing for total storage used by templates may be introduced in the future.
The template build environment is a full sandbox environment, so you can do anything during the build that you can do inside a running sandbox, including running Docker containers as part of the setup.
Compared to snapshots, templates start faster and use fewer resources because the guest OS is restarted before the long-running process is captured and prefetching is significantly more effective. If your workload involves spinning up many per-customer or per-project environments, prefer templates over snapshots.
Layering templates with fromTemplate
When you build many similar templates (e.g. a customer-specific template per customer that all share the same base setup), use fromTemplate to start from an existing template instead of rebuilding the shared layers from scratch each time. This keeps per-customer builds fast and reuses the cached base.
import { Template } from 'e2b'
// Per-customer template built on top of a shared base template
export const template = Template()
.fromTemplate('my-base-template')
.copy('./customers/acme', '/app/config')
.setEnvs({ CUSTOMER_ID: 'acme' })Build limits
Template builds are subject to the following limits:
- Max build duration: Builds can run for up to 1 hour. If a build exceeds this, it will be terminated.
- Max vCPUs per build: 8 on Hobby, 8+ on Pro, custom on Enterprise.
- Max memory per build: 8 GB on Hobby, 8+ GB on Pro, custom on Enterprise.
- Max disk size per build: 10 GB on Hobby, 20+ GB on Pro, custom on Enterprise.
- Concurrent builds: 20 on Hobby and Pro, custom on Enterprise.
See Billing & limits for the full list of plan limits. Need higher limits? Contact support@e2b.dev.