Overview
Access running sandboxes through interactive terminal sessions with full PTY support. Use the CLI for command-line access or integrate terminals directly into your web application.Prerequisites
Before using interactive terminals, ensure you have:- Installed the Sandbox SDK (see Quickstart)
- Created and started a sandbox instance
- Set your
SANDBOX_API_KEYenvironment variable - Basic familiarity with shell commands and WebSockets
CLI usage
Connect to a sandbox
Connect to a running sandbox from your terminal:Select a shell
Choose your preferred shell (bash, zsh, fish, or sh):Features
When connected via CLI:- Full PTY support: Colors, cursor control, vim, tmux, nano work perfectly
- Terminal resize: Automatically detects and syncs terminal size changes
- Interactive input: Real-time input forwarding with proper buffering
- Graceful disconnect: Press Ctrl+C to disconnect cleanly
Browser integration
Creating a terminal via SDK
Using raw API directly
Using raw API directly
Connecting via WebSocket
Connect to the terminal using the WebSocket URL:Using xterm.js
Example React component using xterm.js:Multiple terminals
Create multiple terminals per sandbox
Each sandbox can have multiple independent terminal sessions:List terminals
Get all active terminals for a sandbox:Close a terminal
Delete a specific terminal session:Multiple clients (screen sharing)
Multiple WebSocket clients can connect to the same terminal for collaborative sessions:WebSocket protocol
Control messages
Control messages are JSON objects sent from client to server or server to client: Resize terminal (client → server):Raw terminal data
All other messages are raw terminal input/output:- Client → Server: Raw bytes of keyboard input
- Server → Client: Raw bytes of terminal output (including ANSI escape codes)
Terminal lifecycle
Auto-cleanup
Terminals are automatically cleaned up after 30 minutes of inactivity when no clients are connected:Terminals with at least one connected client are never automatically cleaned up, regardless of idle time.
Manual cleanup
Close terminals explicitly when done:Sandbox termination
When a sandbox is deleted or killed, all its terminals are automatically closed:Common patterns
Interactive debugging session
Start a terminal for debugging a running application:Remote shell for CI/CD
Use terminals in CI pipelines for interactive debugging:Collaborative debugging
Multiple developers can connect to the same terminal:Running interactive tools
Use terminals for tools that require TTY:Troubleshooting
WebSocket connection fails
If the WebSocket connection fails immediately:- Check authentication: Ensure your API key or session is valid
- Check sandbox state: Sandbox must be in “started” state
- Check terminal exists: Verify the terminal ID is valid
- Check network: Ensure WebSocket traffic is not blocked by firewall
Terminal output appears garbled
If terminal output looks corrupted:- Check terminal size: Send a resize event with your terminal dimensions
- Check xterm.js version: Ensure you’re using a compatible version
- Clear and reset: Try sending Ctrl+L or running
resetcommand
Input not working
If keyboard input is not reaching the terminal:- Check WebSocket state: Verify
ws.readyState === WebSocket.OPEN - Check raw mode: For CLI, ensure terminal is in raw mode
- Check focus: For browser, ensure terminal element has focus
Terminal exits immediately
If terminals close right after creation:- Check shell: Some shells exit without input (try
bashinstead ofsh) - Check working directory: Ensure
cwdparameter points to existing directory - Check logs: Look at terminal output before it closed
Performance issues
If terminal feels slow or laggy:- Check network latency: High latency affects responsiveness
- Reduce output: Avoid commands that produce huge amounts of output
- Use pagination: For large output, use
less,more, orhead/tail
Best practices
- Choose the right shell: Use bash for compatibility, zsh/fish for features
- Clean up terminals: Delete terminals when done to free resources
- Handle disconnections: Implement reconnection logic in your application
- Set proper dimensions: Send accurate terminal size for best display
- Use for debugging: Keep terminals as a debugging tool, not primary interface
- Secure access: Always authenticate WebSocket connections
- Monitor activity: Track terminal usage for billing and security
Limitations
- Max idle time: Terminals auto-cleanup after 30 minutes without clients
- No session persistence: Terminals don’t survive sandbox restarts
- No buffer history: Reconnecting clients don’t receive previous output
- PTY only: Requires PTY library (
@lydell/node-ptyin production) - Shell availability: Only supports bash, zsh, fish, sh (must be installed in image)
- Single PTY per terminal: Each terminal is one PTY process
API endpoints
Create terminal
List terminals
Get terminal info
Delete terminal
204 No Content