Skip to content
Dashboard

Run untrusted code with Vercel Sandbox

, Laurens Duijvesteijn, , , Ali Smesseim, Fabio Benedetti,
import { Sandbox } from "@vercel/sandbox";
import { generateText } from 'ai';
const result = await generateText({
model: "anthropic/claude-4-sonnet-20250514",
prompt: `Write a Node.js script that prints a Haiku poem to stdout.`,
system: `
You are a developer that responds with the content of a single Node.js script.
You must include only the code without any markdown, nothing else.
Just include Javascript code and no characters before or after the code.
`,
});
const sandbox = await Sandbox.create();
await sandbox.writeFiles([
{ path: "script.js", stream: Buffer.from(result.text) },
]);
await sandbox.runCommand({
cmd: "node",
args: ["script.js"],
stdout: process.stdout,
stderr: process.stderr,
});

An example of using Vercel Sandbox to run generated code.

Ready to deploy?