Documentation

Examples

Generated code execution and preview servers in Syva sandboxes.

End-to-end guides

Run generated code

Write source, execute it, return stdout and stderr.

TypeScript
import { Sandbox } from "@syva/sdk";

export async function runGeneratedCode(sourceCode: string) {
  const sandbox = await Sandbox.create({
    image: "python-3.12:base",
    cpuCores: 2,
    memoryMb: 2048,
  });

  try {
    await sandbox.fs.writeFile("/workspace/task.py", sourceCode);
    const result = await sandbox.runCommand("python task.py", { cwd: "/workspace" });
    return { stdout: result.stdout, stderr: result.stderr };
  } finally {
    await sandbox.stop();
  }
}

Stream a long-running job

Start a detached command, stream output, inspect final status.

TypeScript
const command = await sandbox.process.start("npm test", {
  cwd: "/workspace",
});

for await (const event of command.logs()) {
  if (event.event === "stdout" || event.event === "stderr") {
    process.stdout.write(String(event.data.data));
  }
}

const output = await command.output();
console.log(output.state, output.exitCode);

Expose a web preview

Declare HTTP ports at create time. Use sandbox.domain(port) after the server starts.

TypeScript
import { Sandbox } from "@syva/sdk";

const sandbox = await Sandbox.create({
  image: "node-22:base",
  ports: [{ containerPort: 5173, protocol: "http" }],
});

await sandbox.fs.writeFile("/workspace/server.mjs", `
import http from "node:http";

http.createServer((request, response) => {
  response.end("<h1>Hello from Syva</h1>");
}).listen(5173, "0.0.0.0");
`);

const devServer = await sandbox.process.start("node /workspace/server.mjs");
console.log(sandbox.domain(5173));

// Later:
await devServer.stop();
await sandbox.stop();

Collect artifacts

Create outputs, list produced files, remove temporary paths.

TypeScript
await sandbox.mkDir("results");
await sandbox.writeFile("results/output.txt", "done\n");

const entries = await sandbox.fs.list("/workspace/results");
console.log(entries.map((entry) => entry.name));

const info = await sandbox.fs.stat("/workspace/results/output.txt");
console.log(info.type, info.size);

await sandbox.fs.rm("/workspace/results", { recursive: true });