Skip to main content

Overview

The SDK provides rt.execute() for running code in sandboxed environments and rt.runtimes() for listing available languages. Sessions allow you to persist state across multiple requests.

Execute Code

import { RunTools } from '@runtools/sdk';

const rt = new RunTools({ apiKey: process.env.RUNTOOLS_API_KEY });

// Simple execution
const result = await rt.execute({
  language: 'python',
  code: 'print("Hello from RunTools!")',
});

console.log(result.run.stdout);  // "Hello from RunTools!\n"
console.log(result.run.code);    // 0
console.log(result.run.time_ms); // 22

Options

const result = await rt.execute({
  // Required
  language: 'python',      // Language identifier

  // Code input (one of these is required)
  code: 'print(42)',       // Shorthand for single file
  files: [                 // Multi-file support
    { name: 'main.py', content: 'from utils import add\nprint(add(1, 2))' },
    { name: 'utils.py', content: 'def add(a, b): return a + b' },
  ],

  // Optional
  stdin: 'input data',     // Standard input
  sessionId: 'abc-123',   // Reuse a persistent sandbox
  version: '*',            // Version selector (default: latest)
  runTimeout: 300000,      // Run timeout in ms (default: 300000 = 5 min)
  compileTimeout: 60000,   // Compile timeout in ms (default: 60000 = 1 min)
});

Response

interface ExecuteResult {
  language: string;   // e.g. "python"
  version: string;    // e.g. "3.12.3"
  session_id: string; // Pass back to reuse sandbox
  compile?: {         // Only for compiled languages
    stdout: string;
    stderr: string;
    code: number;
    time_ms: number;
    memory_kb: number;
  };
  run: {
    stdout: string;   // Program output
    stderr: string;   // Error output
    code: number;     // Exit code (0 = success)
    signal: string | null;
    time_ms: number;  // Execution time
    memory_kb: number;
  };
}

Sessions

Sessions let you persist files and state across multiple execution requests:
// Step 1: Create a variable
const r1 = await rt.execute({
  language: 'python',
  code: 'open("data.txt", "w").write("hello world")',
});

// Step 2: Read it back (files persist!)
const r2 = await rt.execute({
  language: 'python',
  sessionId: r1.session_id,
  code: 'print(open("data.txt").read())',
});

console.log(r2.run.stdout); // "hello world"
Sessions are automatically cleaned up after 10 minutes of inactivity. Without sessionId, each execution runs in an ephemeral sandbox that is destroyed immediately.

List Runtimes

const runtimes = await rt.runtimes();
// [
//   { language: "python", version: "3.12.3", aliases: ["py", "python3"] },
//   { language: "javascript", version: "22.x.x", aliases: ["js", "node"] },
//   ...
// ]

Supported Languages

LanguageIDCompiled
PythonpythonNo
JavaScriptjavascriptNo
TypeScripttypescriptNo
BashbashNo
CcYes
C++c++Yes
GogoYes
RubyrubyNo
RustrustYes
JavajavaYes
C#csharpNo

Examples

Session Persistence

// Multi-step computation with persistent state
const step1 = await rt.execute({
  language: 'python',
  code: `
import json
data = {"users": ["alice", "bob"], "count": 2}
json.dump(data, open("state.json", "w"))
print("State saved")
`,
});

const step2 = await rt.execute({
  language: 'python',
  sessionId: step1.session_id,
  code: `
import json
data = json.load(open("state.json"))
data["users"].append("charlie")
data["count"] += 1
print(f"Users: {data['users']}, Count: {data['count']}")
`,
});
// stdout: "Users: ['alice', 'bob', 'charlie'], Count: 3"

Compiled Language (C)

const result = await rt.execute({
  language: 'c',
  code: `#include <stdio.h>
int main() {
    printf("Hello from C!\\n");
    return 0;
}`,
});

// result.compile contains compilation output
// result.run contains execution output
console.log(result.compile?.time_ms); // Compile time
console.log(result.run.stdout);       // "Hello from C!\n"

With stdin

const result = await rt.execute({
  language: 'python',
  code: 'name = input()\nprint(f"Hello, {name}!")',
  stdin: 'World',
});

console.log(result.run.stdout); // "Hello, World!\n"

Error Handling

const result = await rt.execute({
  language: 'python',
  code: 'print(undefined_variable)',
});

if (result.run.code !== 0) {
  console.error('Execution failed:', result.run.stdout);
  // "NameError: name 'undefined_variable' is not defined"
}