Skip to main content
The WebSocket terminal provides interactive, real-time access to a PTY (pseudo-terminal) in your sandbox. This enables you to run interactive commands, monitor output in real-time, and handle terminal-specific features like resizing.

Prerequisites

Before you begin, make sure you have:
  • Active sandbox - A running sandbox (see Creating Sandboxes)
  • WebSocket library - For Python: pip install websockets (for JavaScript, WebSocket is built-in)
  • Understanding of async/await - Familiarity with asynchronous programming patterns
  • Terminal access - Basic understanding of terminal/command-line operations

Overview

The WebSocket terminal is ideal for:
  • Interactive command-line tools and REPLs
  • Real-time monitoring of long-running processes
  • Terminal applications that require PTY support
  • Building terminal UIs or dashboards
  • Debugging and troubleshooting
The WebSocket terminal requires the websockets library for Python. Install it with: pip install websockets

Connecting to Terminal

Connect to the terminal using the terminal resource:

Sending Input

Send commands and input to the terminal:

Receiving Output

Receive terminal output in real-time using async iteration:

Resizing Terminal

Resize the terminal window to match your display:

Complete Example

Here’s a complete example that demonstrates interactive terminal usage:

Message Types

The terminal sends different message types:
  • output: Terminal output data
  • exit: Process exit notification

Error Handling

Handle connection errors and timeouts:

Best Practices

Use async context managers in Python to ensure proper WebSocket cleanup. The async with statement automatically closes the connection.
Include newline characters (\n) when sending commands to execute them immediately. Without \n, the command waits for more input.
Terminal connections are stateful. Each connection maintains its own shell session. If you need a fresh environment, create a new terminal connection.
Resize the terminal before running commands that format output based on terminal width (like ls -la or tree).

API Reference

Python SDK

  • sandbox.terminal.connect(*, timeout=30) - Connect to terminal WebSocket
  • sandbox.terminal.send_input(ws, data) - Send input to terminal
  • sandbox.terminal.resize(ws, cols, rows) - Resize terminal window
  • sandbox.terminal.iter_output(ws) - Async iterator for terminal messages

JavaScript SDK

  • sandbox.terminal.connect() - Connect to terminal WebSocket
  • sandbox.terminal.sendInput(ws, data) - Send input to terminal
  • sandbox.terminal.resize(ws, cols, rows) - Resize terminal window
  • sandbox.terminal.output(ws) - Async iterator for terminal messages

API Endpoint

  • WebSocket: /terminal - Connect to interactive terminal
  • Message Types: input, resize, output, exit

Next Steps