Skip to main content

Overview

The Code Execution API runs code in ephemeral sandboxes:
  • Stateless - Each execution is isolated
  • Fast - Optimized for quick execution
  • Multi-runtime - Python, Node.js, Go, Rust, and more
  • Isolated - Each execution runs in its own sandbox

Quick Start

const result = await rt.execute({
  runtime: 'python',
  code: 'print(sum([1, 2, 3, 4, 5]))',
});

console.log(result.stdout);    // "15\n"
console.log(result.exitCode);  // 0
console.log(result.executionTime); // 45 (ms)

Supported Runtimes

RuntimeIdentifierVersion
Pythonpython3.12
Node.jsnode22
Bunbunlatest
Denodenolatest
Gogo1.23
Rustrust1.80

Examples

Python

const result = await rt.execute({
  runtime: 'python',
  code: `
import numpy as np

arr = np.array([1, 2, 3, 4, 5])
print(f"Sum: {arr.sum()}")
print(f"Mean: {arr.mean()}")
print(f"Std: {arr.std():.2f}")
  `,
});
// Sum: 15
// Mean: 3.0
// Std: 1.41

Node.js

const result = await rt.execute({
  runtime: 'node',
  code: `
const numbers = Array.from({ length: 10 }, (_, i) => i * i);
console.log('Squares:', numbers);
console.log('Sum:', numbers.reduce((a, b) => a + b, 0));
  `,
});
// Squares: [ 0, 1, 4, 9, 16, 25, 36, 49, 64, 81 ]
// Sum: 285

Go

const result = await rt.execute({
  runtime: 'go',
  code: `
package main

import "fmt"

func main() {
    sum := 0
    for i := 1; i <= 5; i++ {
        sum += i
    }
    fmt.Printf("Sum: %d\\n", sum)
}
  `,
});
// Sum: 15

Rust

const result = await rt.execute({
  runtime: 'rust',
  code: `
fn main() {
    let numbers: Vec<i32> = (1..=5).collect();
    let sum: i32 = numbers.iter().sum();
    println!("Numbers: {:?}", numbers);
    println!("Sum: {}", sum);
}
  `,
});
// Numbers: [1, 2, 3, 4, 5]
// Sum: 15

Multiple Files

Execute with multiple files:
const result = await rt.execute({
  runtime: 'python',
  files: {
    'main.py': `
import helper
print(helper.greet("World"))
    `,
    'helper.py': `
def greet(name):
    return f"Hello, {name}!"
    `,
  },
  entrypoint: 'main.py',
});
// Hello, World!

Timeouts

Set execution timeouts:
const result = await rt.execute({
  runtime: 'python',
  code: 'import time; time.sleep(60)',
  timeout: 5000, // 5 seconds
});

if (result.timedOut) {
  console.log('Execution timed out');
}

Stdin Input

Provide input to the program:
const result = await rt.execute({
  runtime: 'python',
  code: `
name = input("What's your name? ")
print(f"Hello, {name}!")
  `,
  stdin: 'Alice\n',
});
// What's your name? Hello, Alice!

Error Handling

const result = await rt.execute({
  runtime: 'python',
  code: 'raise ValueError("Something went wrong")',
});

console.log(result.exitCode); // 1
console.log(result.stderr);   // Traceback...ValueError: Something went wrong

CLI Usage

# Quick execute
runtools exec --runtime python "print(1 + 1)"
# 2

# Execute file
runtools exec --runtime python ./script.py

# Execute with multiple files
runtools exec --runtime python --entry main.py ./src/

API Reference

Request

interface ExecuteRequest {
  runtime: 'python' | 'node' | 'bun' | 'deno' | 'go' | 'rust';
  code?: string;           // Code to execute
  files?: Record<string, string>; // Multiple files
  entrypoint?: string;     // Entry file (if files provided)
  stdin?: string;          // Stdin input
  timeout?: number;        // Timeout in ms (default: 30000)
  env?: Record<string, string>; // Environment variables
}

Response

interface ExecuteResult {
  stdout: string;          // Standard output
  stderr: string;          // Standard error
  exitCode: number;        // Exit code (0 = success)
  executionTime: number;   // Execution time in ms
  timedOut: boolean;       // Whether execution timed out
}

Use Cases

Code Evaluation

Evaluate user-submitted code safely in isolated environments

AI Code Generation

Run code generated by AI models to verify correctness

Notebooks

Build Jupyter-like notebook experiences

Education

Run student code submissions for grading

Pricing

TierExecutions/MonthPrice
Free1,000$0
Pro50,000$29/mo
Team500,000$99/mo
EnterpriseUnlimitedCustom

Best Practices

Don’t let code run forever. Set timeouts based on expected execution time.
Check exitCode and stderr to handle execution failures.
For anything beyond a few lines, use the files parameter for better organization.
If you need persistent state or multiple executions, use a sandbox instead.