Skip to main content
Execute code in a running sandbox. Supports multiple programming languages including Python, JavaScript, Bash, and Go.

Endpoint

POST {sandbox_public_host}/execute
POST {sandbox_public_host}/execute/rich

Authentication

VM Agent API endpoints require JWT authentication. Use the auth_token from the sandbox creation response:
Authorization: Bearer {auth_token}

Request

Headers

Authorization: Bearer {auth_token}
Content-Type: application/json

Request Body

{
  "code": "print('Hello, World!')",
  "language": "python",
  "timeout": 60,
  "working_dir": "/workspace",
  "env": {
    "API_KEY": "sk-test-123"
  }
}
Parameters:
ParameterTypeRequiredDescription
codestringYesCode to execute
languagestringNoLanguage: python, javascript, bash, go (default: python)
timeoutintegerNoExecution timeout in seconds (default: 60)
working_dirstringNoWorking directory (default: /workspace)
envobjectNoEnvironment variables for this execution

Rich Output Endpoint

Use /execute/rich to capture rich outputs (plots, DataFrames, etc.):
{
  "code": "import matplotlib.pyplot as plt; plt.plot([1,2,3]); plt.savefig('/workspace/plot.png')",
  "language": "python",
  "timeout": 60
}

Response

Success (200 OK)

{
  "stdout": "Hello, World!\n",
  "stderr": "",
  "exit_code": 0,
  "execution_time": 0.123,
  "rich_outputs": [
    {
      "type": "image",
      "path": "/workspace/plot.png"
    }
  ]
}

Examples

cURL

# Basic execution
curl -X POST https://1761048129dsaqav4n.hopx.dev/execute \
  -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..." \
  -H "Content-Type: application/json" \
  -d '{
    "code": "print(\"Hello, World!\")",
    "language": "python"
  }'

# With rich outputs
curl -X POST https://1761048129dsaqav4n.hopx.dev/execute/rich \
  -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..." \
  -H "Content-Type: application/json" \
  -d '{
    "code": "import matplotlib.pyplot as plt; plt.plot([1,2,3]); plt.savefig(\"/workspace/plot.png\")",
    "language": "python"
  }'

Python

import os
import requests

# Get sandbox info (from creation response)
sandbox_public_host = "https://1761048129dsaqav4n.hopx.dev"
auth_token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."

headers = {
    "Authorization": f"Bearer {auth_token}",
    "Content-Type": "application/json"
}

# Basic execution
data = {
    "code": "print('Hello, World!')",
    "language": "python"
}

response = requests.post(
    f"{sandbox_public_host}/execute",
    headers=headers,
    json=data
)

if response.status_code == 200:
    result = response.json()
    print(f"Output: {result['stdout']}")
    print(f"Exit code: {result['exit_code']}")
    print(f"Execution time: {result['execution_time']}s")

# With rich outputs
data = {
    "code": """
import matplotlib.pyplot as plt
plt.plot([1, 2, 3])
plt.savefig('/workspace/plot.png')
""",
    "language": "python"
}

response = requests.post(
    f"{sandbox_public_host}/execute/rich",
    headers=headers,
    json=data
)

if response.status_code == 200:
    result = response.json()
    print(f"Rich outputs: {len(result.get('rich_outputs', []))}")
    for output in result.get('rich_outputs', []):
        print(f"  {output['type']}: {output['path']}")

JavaScript

// Get sandbox info (from creation response)
const sandboxPublicHost = 'https://1761048129dsaqav4n.hopx.dev';
const authToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...';

// Basic execution
const response = await fetch(`${sandboxPublicHost}/execute`, {
  method: 'POST',
  headers: {
    'Authorization': `Bearer ${authToken}`,
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    code: "print('Hello, World!')",
    language: 'python'
  })
});

if (response.ok) {
  const result = await response.json();
  console.log(`Output: ${result.stdout}`);
  console.log(`Exit code: ${result.exit_code}`);
  console.log(`Execution time: ${result.execution_time}s`);
}

// With rich outputs
const richResponse = await fetch(`${sandboxPublicHost}/execute/rich`, {
  method: 'POST',
  headers: {
    'Authorization': `Bearer ${authToken}`,
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    code: `
import matplotlib.pyplot as plt
plt.plot([1, 2, 3])
plt.savefig('/workspace/plot.png')
`,
    language: 'python'
  })
});

if (richResponse.ok) {
  const result = await richResponse.json();
  console.log(`Rich outputs: ${result.rich_outputs?.length || 0}`);
  result.rich_outputs?.forEach(output => {
    console.log(`  ${output.type}: ${output.path}`);
  });
}

Supported Languages

Python

{
  "code": "import pandas as pd; df = pd.DataFrame({'x': [1,2,3]}); print(df)",
  "language": "python"
}

JavaScript

{
  "code": "console.log('Hello from Node.js!')",
  "language": "javascript"
}

Bash

{
  "code": "echo 'Hello from Bash!' && ls -la",
  "language": "bash"
}

Go

{
  "code": "package main\nimport \"fmt\"\nfunc main() { fmt.Println(\"Hello from Go!\") }",
  "language": "go"
}