Skip to main content
Connect to sandboxes via WebSocket for real-time streaming of code execution, terminal output, and file changes.

Connection

wss://{sandbox_public_host}/execute/stream
wss://{sandbox_public_host}/terminal
wss://{sandbox_public_host}/files/watch

Authentication

Include JWT token in WebSocket connection:
Authorization: Bearer {auth_token}

Endpoints

Code Execution Stream

Stream code execution output in real-time:
wss://{sandbox_public_host}/execute/stream
Send:
{
  "type": "execute",
  "code": "print('Hello')",
  "language": "python"
}
Receive:
{
  "type": "stdout",
  "data": "Hello\n"
}

Terminal

Interactive terminal access:
wss://{sandbox_public_host}/terminal

File Watching

Watch for file system changes:
wss://{sandbox_public_host}/files/watch
Send:
{
  "action": "watch",
  "path": "/workspace"
}
Receive:
{
  "type": "change",
  "path": "/workspace/file.txt",
  "event": "created"
}

Examples

Python

import asyncio
import websockets
import json

async def stream_execution():
    uri = f"wss://{sandbox_public_host.replace('https://', '')}/execute/stream"
    headers = {"Authorization": f"Bearer {auth_token}"}
    
    async with websockets.connect(uri, extra_headers=headers) as ws:
        # Send execution request
        await ws.send(json.dumps({
            "type": "execute",
            "code": "for i in range(5): print(i)",
            "language": "python"
        }))
        
        # Receive messages
        async for message in ws:
            data = json.loads(message)
            if data["type"] == "stdout":
                print(data["data"], end="")
            elif data["type"] == "complete":
                print(f"\nExit code: {data['exit_code']}")
                break

asyncio.run(stream_execution())

JavaScript

const ws = new WebSocket(
  `wss://${sandboxPublicHost.replace('https://', '')}/execute/stream`,
  {
    headers: {
      'Authorization': `Bearer ${authToken}`
    }
  }
);

ws.onopen = () => {
  ws.send(JSON.stringify({
    type: 'execute',
    code: 'for (let i = 0; i < 5; i++) console.log(i)',
    language: 'javascript'
  }));
};

ws.onmessage = (event) => {
  const data = JSON.parse(event.data);
  if (data.type === 'stdout') {
    process.stdout.write(data.data);
  } else if (data.type === 'complete') {
    console.log(`\nExit code: ${data.exit_code}`);
    ws.close();
  }
};