Quickstart

Run your first script — execute Python, JavaScript, and Go code with stdin/stdout handling.

Install the SDK

npm install @usetransactional/node

Run a Python Script

import Transactional from '@usetransactional/node';
 
const tx = new Transactional({
  apiKey: process.env.TRANSACTIONAL_API_KEY,
});
 
const result = await tx.code.run({
  language: 'python',
  version: '3.12',
  code: `
import math
 
numbers = [1, 4, 9, 16, 25]
roots = [math.sqrt(n) for n in numbers]
 
for n, r in zip(numbers, roots):
    print(f"sqrt({n}) = {r}")
  `,
});
 
console.log(result.stdout);
// sqrt(1) = 1.0
// sqrt(4) = 2.0
// sqrt(9) = 3.0
// sqrt(16) = 4.0
// sqrt(25) = 5.0
 
console.log('Exit code:', result.exitCode); // 0
console.log('Execution time:', result.executionTime, 'ms');

Run JavaScript

const result = await tx.code.run({
  language: 'javascript',
  code: `
    const data = [
      { name: 'Alice', score: 95 },
      { name: 'Bob', score: 87 },
      { name: 'Carol', score: 92 },
    ];
 
    const avg = data.reduce((sum, d) => sum + d.score, 0) / data.length;
    const top = data.sort((a, b) => b.score - a.score)[0];
 
    console.log(JSON.stringify({ average: avg.toFixed(1), topScorer: top.name }));
  `,
});
 
const parsed = JSON.parse(result.stdout);
console.log('Average:', parsed.average);   // "91.3"
console.log('Top scorer:', parsed.topScorer); // "Alice"

Run Go

const result = await tx.code.run({
  language: 'go',
  code: `
package main
 
import (
    "fmt"
    "strings"
)
 
func main() {
    words := []string{"hello", "from", "go"}
    fmt.Println(strings.Join(words, " "))
    fmt.Println("Version: Go 1.22")
}
  `,
});
 
console.log(result.stdout);
// hello from go
// Version: Go 1.22

Pass Stdin

Provide input data via stdin:

const result = await tx.code.run({
  language: 'python',
  code: `
import sys
import json
 
data = json.load(sys.stdin)
total = sum(item['amount'] for item in data['transactions'])
print(f"Total: ${total:.2f}")
  `,
  stdin: JSON.stringify({
    transactions: [
      { amount: 29.99 },
      { amount: 49.50 },
      { amount: 12.00 },
    ],
  }),
});
 
console.log(result.stdout); // Total: $91.49

Pass Command-Line Arguments

const result = await tx.code.run({
  language: 'python',
  code: `
import sys
name = sys.argv[1]
count = int(sys.argv[2])
print(f"Hello {name}! " * count)
  `,
  args: ['World', '3'],
});
 
console.log(result.stdout); // Hello World! Hello World! Hello World!

Set Resource Limits

Configure execution constraints:

const result = await tx.code.run({
  language: 'python',
  code: 'print(sum(range(10_000_000)))',
  limits: {
    timeout: 5000,   // 5 seconds max
    memory: 256,     // 256 MB max
  },
});

Handle Errors

const result = await tx.code.run({
  language: 'python',
  code: 'print(1 / 0)',
});
 
if (result.exitCode !== 0) {
  console.error('Script failed:');
  console.error(result.stderr);
  // ZeroDivisionError: division by zero
} else {
  console.log(result.stdout);
}

Run TypeScript

TypeScript is executed directly without a separate compile step:

const result = await tx.code.run({
  language: 'typescript',
  code: `
interface User {
  name: string;
  age: number;
}
 
const users: User[] = [
  { name: 'Alice', age: 30 },
  { name: 'Bob', age: 25 },
];
 
const names: string[] = users.map(u => u.name);
console.log(names.join(', '));
  `,
});
 
console.log(result.stdout); // Alice, Bob

Next Steps