Skip to main content
The Pty module provides interactive pseudo-terminal (PTY) sessions over WebSocket. Access it via sandbox.pty.

Creating a PTY Session

const session = await sandbox.pty.create({
  cols: 120,
  rows: 40,
  onOutput: (data) => {
    process.stdout.write(data);
  },
});

sandbox.pty.create(opts?)

Opens a new interactive terminal session.
opts
PtyOpts
Returns: Promise<PtySession>

PtySession

Property / MethodTypeDescription
sessionIdstringUnique session identifier
send(data)(data: string | Uint8Array) => voidSend input to the terminal
close()() => voidClose the PTY session

Examples

Run an Interactive Command

const session = await sandbox.pty.create({
  onOutput: (data) => {
    const text = new TextDecoder().decode(data);
    process.stdout.write(text);
  },
});

// Type a command
session.send('ls -la\n');

// Wait a moment for output, then close
setTimeout(() => session.close(), 2000);

Pipe User Input

const session = await sandbox.pty.create({
  cols: 80,
  rows: 24,
  onOutput: (data) => {
    process.stdout.write(new TextDecoder().decode(data));
  },
});

// Forward stdin to the PTY
process.stdin.setRawMode(true);
process.stdin.on('data', (chunk) => {
  session.send(chunk);
});
PTY sessions use WebSocket connections under the hood. Each session maintains a persistent connection to the sandbox worker.