Creating a Sandbox is the first step in using Agent Sandbox Flash Edition. After the Sandbox is ready, you can run commands, read and write files, execute code, or start temporary HTTP services.
Create a Default Sandbox
import { Sandbox } from "e2b";
const sandbox = await Sandbox.create("code-interpreter-v1", {
apiKey: process.env.E2B_API_KEY,
apiUrl: process.env.E2B_API_URL,
domain: process.env.E2B_DOMAIN,
});
try {
const result = await sandbox.commands.run("echo hello");
console.log(result.stdout.trim());
} finally {
await sandbox.kill();
}
Python example:
import os
from e2b import Sandbox
sandbox = Sandbox.create(
template="code-interpreter-v1",
api_key=os.environ["E2B_API_KEY"],
api_url=os.environ["E2B_API_URL"],
domain=os.environ["E2B_DOMAIN"],
)
try:
result = sandbox.commands.run("echo hello")
print(result.stdout.strip())
finally:
sandbox.kill()
Specify a Template
When you need preinstalled dependencies or a fixed runtime environment, create the Sandbox from a template:
const sandbox = await Sandbox.create("code-interpreter-v1", {
apiKey: process.env.E2B_API_KEY,
apiUrl: process.env.E2B_API_URL,
domain: process.env.E2B_DOMAIN,
timeoutMs: 10 * 60 * 1000,
});
In the Python SDK, pass the template name or template ID through the template parameter:
sandbox = Sandbox.create(
template="code-interpreter-v1",
api_key=os.environ["E2B_API_KEY"],
api_url=os.environ["E2B_API_URL"],
domain=os.environ["E2B_DOMAIN"],
timeout=10 * 60,
)
In the TypeScript SDK, pass the template name or template ID as the first argument of Sandbox.create(template, options). Common options parameters include:
-
timeoutMs: Sandbox timeout in milliseconds. -
envs: Environment variables injected into the Sandbox runtime. -
metadata: Custom metadata written to the Sandbox control plane.
Recommendations
After creating the Sandbox, your application code should call sandbox.kill() in a try/finally block. If a task needs to preserve its current state, use Pause and Resume, but do not use pause as a substitute for releasing resources.