Connect to a Sandbox

Updated at:

Use the CLI to connect to an FC Agent Sandbox instance that is already running and open an interactive terminal:

e2b sandbox connect <sandbox-id>

After the connection succeeds, you can run commands as in a normal remote terminal:

ls
pwd
exit

Unlike e2b sandbox create <template>, connect only attaches to an existing sandbox. Exiting the terminal only closes the local session. It does not automatically terminate the sandbox.

If you need to connect to an existing sandbox in server-side code, use the SDK:

const sandbox = await Sandbox.connect("<sandbox-id>", {
  apiKey: process.env.E2B_API_KEY,
  apiUrl: process.env.E2B_API_URL,
  domain: process.env.E2B_DOMAIN,
});

Python example:

sandbox = Sandbox.connect(
    "<sandbox-id>",
    api_key=os.environ["E2B_API_KEY"],
    api_url=os.environ["E2B_API_URL"],
    domain=os.environ["E2B_DOMAIN"],
)