commands.run() which executes a command and returns output after completion, PTY provides:
- Real-time streaming - Output is streamed as it happens via callbacks
- Bidirectional input - Send input while the terminal is running
- Interactive shell - Full terminal support with ANSI colors and escape sequences
- Session persistence - Disconnect and reconnect to running sessions
The SDK asks the sandbox for a keepalive ping every 50 seconds on every stream, so a stream normally stays open for more than an hour even while nothing is written. No single connection is guaranteed that long, so build your client to reconnect. See Stream connections.
Create a PTY session
Usesandbox.pty.create() to start an interactive bash shell. This example runs echo 'hello world', then exits and prints the terminal output.
The PTY runs an interactive bash shell with
TERM=xterm-256color, which supports ANSI colors and escape sequences.Timeout
PTY sessions have a configurable timeout that controls the session duration. The default is 60 seconds. For interactive or long-running sessions, settimeoutMs: 0 (JavaScript) or timeout=0 (Python) so the SDK does not close the session on its own. If the connection drops, pty.connect() reattaches to the same running session. See Disconnect and reconnect.
Send input to PTY
UsesendInput() in JavaScript or send_stdin() in Python to send data to the terminal. These methods return a Promise (JavaScript) or complete synchronously (Python) - the actual output is delivered to your onData callback (JavaScript) or to the on_pty callback you pass to wait() (Python).
Resize the terminal
When the user’s terminal window changes size, notify the PTY withresize(). The cols and rows parameters are measured in characters, not pixels.
Disconnect and reconnect
You can disconnect from a PTY session while keeping it running, then reconnect later with a new data handler. This is useful for:- Resuming terminal sessions after network interruptions
- Sharing terminal access between multiple clients
- Implementing terminal session persistence
Kill the PTY
Terminate the PTY session withkill().
Wait for PTY to exit
Usewait() to wait for the terminal session to end (e.g., when the user types exit).
Interactive terminal (SSH-like)
Building a fully interactive terminal (like SSH) requires handling raw mode, stdin forwarding, and terminal resize events. For a production implementation, see the E2B CLI source code which uses the samesandbox.pty API documented above.