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:
import 'dotenv/config';import { Sandbox } from 'e2b';// Create a Sandbox from development templateconst sandbox = await Sandbox.create("template-tag-dev");// Create a Sandbox from production templateconst sandbox = await Sandbox.create("template-tag");
The template name is the identifier that can be used to create a new Sandbox.
There is no practical limit on how many templates you can have. It’s perfectly fine to create tens or hundreds of thousands of templates — for example, one template per customer, per project, or per agent run.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.
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 templateexport const template = Template() .fromTemplate('my-base-template') .copyDirectory('./customers/acme', '/app/config') .setEnvs({ CUSTOMER_ID: 'acme' })