Connect to a Sandbox
Connecting to a Sandbox lets you reuse the same Sandbox across different processes, services, or functions. A typical pattern is that request A creates the Sandbox and returns sandboxId, then request B continues the task by reconnecting with sandboxId.
Save the Sandbox ID
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,
});
console.log(sandbox.sandboxId);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"],
)
print(sandbox.sandbox_id)Connect to an existing sandbox
const sandbox = await Sandbox.connect(process.env.SANDBOX_ID, {
apiKey: process.env.E2B_API_KEY,
apiUrl: process.env.E2B_API_URL,
domain: process.env.E2B_DOMAIN,
});
const result = await sandbox.commands.run("pwd");
console.log(result.stdout.trim());Python example:
sandbox = Sandbox.connect(
os.environ["SANDBOX_ID"],
api_key=os.environ["E2B_API_KEY"],
api_url=os.environ["E2B_API_URL"],
domain=os.environ["E2B_DOMAIN"],
)
result = sandbox.commands.run("pwd")
print(result.stdout.strip())If the target Sandbox is paused, Sandbox.connect() automatically resumes it before returning a usable Sandbox object. Pause and resume is currently available only to allowlisted accounts.
Recommendations
sandboxIdis the key identifier for reconnecting to a Sandbox and should be stored by your business system.Reconnecting restores control-plane access and runtime access only; it does not automatically recover task state for your application.
If you cannot confirm that the Sandbox is still usable, reconnect and call
sandbox.getInfo()or run a lightweight command as a health check.