All Products
Search
Document Center

Function Compute:PTY

Last Updated:Jul 21, 2026

PTY creates a pseudo-terminal session. It is useful for tools that require terminal behavior, such as colored output, shells, interactive commands, or CLIs that depend on TTY detection.

Create a PTY session

TypeScript example:

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 terminal = await sandbox.pty.create({
    cols: 80,
    rows: 24,
    timeoutMs: 0,
    onData: (data) => {
      process.stdout.write(data);
    },
  });

  await sandbox.pty.sendInput(
    terminal.pid,
    new TextEncoder().encode("python3 - <<'PY'\nimport sys\nprint(sys.stdout.isatty())\nPY\n")
  );
  await sandbox.pty.sendInput(terminal.pid, new TextEncoder().encode("exit\n"));

  const result = await terminal.wait();
  console.log(result.exitCode);
} finally {
  await sandbox.kill();
}

Python example:

import os

from e2b import PtySize, Sandbox

sandbox = Sandbox.create(
    "code-interpreter-v1",
    api_key=os.environ["E2B_API_KEY"],
    api_url=os.environ["E2B_API_URL"],
    domain=os.environ["E2B_DOMAIN"],
)

try:
    terminal = sandbox.pty.create(
        PtySize(rows=24, cols=80),
        timeout=0,
    )

    sandbox.pty.send_stdin(
        terminal.pid,
        b"python3 - <<'PY'\nimport sys\nprint(sys.stdout.isatty())\nPY\n",
    )
    sandbox.pty.send_stdin(terminal.pid, b"exit\n")

    result = terminal.wait(
        on_pty=lambda data: print(data.decode(), end=""),
    )
    print(result.exit_code)
finally:
    sandbox.kill()

sandbox.pty.create() starts an interactive terminal session. TypeScript uses sandbox.pty.sendInput() to send input. Python uses sandbox.pty.send_stdin(). Wait for the session to exit with terminal.wait(). To disconnect and reconnect later, save terminal.pid and use sandbox.pty.connect(pid).

Resize and terminate a PTY session

  • When the terminal size changes, call resize

    • TypeScript: sandbox.pty.resize(pid, { cols, rows }).

    • Python: sandbox.pty.resize(pid, PtySize(rows, cols))

  • To terminate a PTY session, call sandbox.pty.kill(pid) or kill() on the session object.

  • PTY input and output flow through a terminal data stream, so PTY is not suitable for tasks that require strict stdout/stderr separation.

When to use PTY

PTY is a good fit when:

  • The command needs to behave like it is running in a real terminal.

  • The tool disables colors, progress indicators, or interactive features in a non-TTY environment.

  • You need to send standard input to an interactive process.

PTY is not recommended when:

  • You only need stable stdout and stderr parsing for batch commands.

  • Your task requires strict separation between stdout and stderr.

  • You are processing large volumes of structured log output. PTY can change the output format and increase parsing cost.

By default, prefer regular commands.run(). Only use sandbox.pty when the command clearly depends on terminal behavior.