Maintenance May 12, 8–9 PM PDT (May 13, 03:00–04:00 UTC). ~1 min disruption to sandbox management may occur. Already running sandboxes will not be affected. Questions? Contact us
Maintenance May 12, 8–9 PM PDT (May 13, 03:00–04:00 UTC). ~1 min disruption to sandbox management may occur. Already running sandboxes will not be affected. Questions? Contact us
The PTY (pseudo-terminal) module allows you to create interactive terminal sessions in the sandbox with real-time, bidirectional communication.Unlike 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
PTY sessions have a configurable timeout that controls the session duration. The default is 60 seconds. For interactive or long-running sessions, set timeoutMs: 0 (JavaScript) or timeout=0 (Python) to keep the session open indefinitely.
import { Sandbox } from 'e2b'const sandbox = await Sandbox.create()const terminal = await sandbox.pty.create({ cols: 80, rows: 24, onData: (data) => process.stdout.write(data), timeoutMs: 0, // Keep the session open indefinitely})
from e2b import Sandboxsandbox = Sandbox()# end='' prevents print() from adding an extra newline# (PTY output already contains newlines)terminal = sandbox.pty.create( cols=80, rows=24, on_data=lambda data: print(data.decode(), end=''), timeout=0, # Keep the session open indefinitely)
Use sendInput() 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 will be delivered to your onData callback.
import { Sandbox } from 'e2b'const sandbox = await Sandbox.create()const terminal = await sandbox.pty.create({ cols: 80, rows: 24, onData: (data) => process.stdout.write(data),})// Send a command (don't forget the newline!)await sandbox.pty.sendInput( terminal.pid, new TextEncoder().encode('echo "Hello from PTY"\n'))
from e2b import Sandboxsandbox = Sandbox()terminal = sandbox.pty.create( cols=80, rows=24, on_data=lambda data: print(data.decode(), end=''), # end='' prevents extra newline)# Send a command as bytes (b'...' is Python's byte string syntax)# Don't forget the newline!sandbox.pty.send_stdin(terminal.pid, b'echo "Hello from PTY"\n')
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
import { Sandbox } from 'e2b'const sandbox = await Sandbox.create()// Create a PTY sessionconst terminal = await sandbox.pty.create({ cols: 80, rows: 24, onData: (data) => console.log('Handler 1:', new TextDecoder().decode(data)),})const pid = terminal.pid// Send a commandawait sandbox.pty.sendInput(pid, new TextEncoder().encode('echo hello\n'))// Disconnect - PTY keeps running in the backgroundawait terminal.disconnect()// Later: reconnect with a new data handlerconst reconnected = await sandbox.pty.connect(pid, { onData: (data) => console.log('Handler 2:', new TextDecoder().decode(data)),})// Continue using the sessionawait sandbox.pty.sendInput(pid, new TextEncoder().encode('echo world\n'))// Wait for the terminal to exitawait reconnected.wait()
import timefrom e2b import Sandboxsandbox = Sandbox()# Create a PTY sessionterminal = sandbox.pty.create( cols=80, rows=24, on_data=lambda data: print('Handler 1:', data.decode()),)pid = terminal.pid# Send a commandsandbox.pty.send_stdin(pid, b'echo hello\n')time.sleep(0.5)# Disconnect - PTY keeps running in the backgroundterminal.disconnect()# Later: reconnect with a new data handlerreconnected = sandbox.pty.connect( pid, on_data=lambda data: print('Handler 2:', data.decode()),)# Continue using the sessionsandbox.pty.send_stdin(pid, b'echo world\n')# Wait for the terminal to exitreconnected.wait()
import { Sandbox } from 'e2b'const sandbox = await Sandbox.create()const terminal = await sandbox.pty.create({ cols: 80, rows: 24, onData: (data) => process.stdout.write(data),})// Kill the PTYconst killed = await sandbox.pty.kill(terminal.pid)console.log('Killed:', killed) // true if successful// Or use the handle method// await terminal.kill()
from e2b import Sandboxsandbox = Sandbox()terminal = sandbox.pty.create( cols=80, rows=24, on_data=lambda data: print(data.decode(), end=''), # end='' prevents extra newline)# Kill the PTYkilled = sandbox.pty.kill(terminal.pid)print('Killed:', killed) # True if successful# Or use the handle method# terminal.kill()
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 same sandbox.pty API documented above.