The node template ships Node.js 20 LTS, npm, the typescript compiler,
and yarn. Pick it for TypeScript / Node scripts and any workflow that
needs to install npm packages at runtime.
What you’ll learn
- Picking
template="node" to skip a Node install
- Writing a
.ts file into the sandbox, compiling with tsc, running with node
Prerequisites
export DECLAW_API_KEY="your-api-key"
export DECLAW_DOMAIN="your-declaw-instance.example.com:8080"
Code
import textwrap
from declaw import Sandbox
SCRIPT = textwrap.dedent("""\
declare const process: { version: string };
type Order = { id: string; qty: number; price: number };
const orders: Order[] = [
{ id: "A-1", qty: 3, price: 19.99 },
{ id: "A-2", qty: 1, price: 49.50 },
{ id: "B-1", qty: 5, price: 4.25 },
];
const total = orders.reduce((s, o) => s + o.qty * o.price, 0);
const summary = {
orders: orders.length,
units: orders.reduce((s, o) => s + o.qty, 0),
total: Number(total.toFixed(2)),
node: process.version,
};
console.log(JSON.stringify(summary, null, 2));
""")
def main() -> None:
sbx = Sandbox.create(template="node", timeout=120)
try:
for tool in ("node", "npm", "tsc"):
r = sbx.commands.run(f"{tool} --version")
print(f"{tool}: {r.stdout.strip()}")
sbx.files.write("/tmp/orders.ts", SCRIPT)
r = sbx.commands.run("cd /tmp && tsc --target ES2020 orders.ts && node orders.js")
if r.exit_code != 0:
print("compile/run failed:", r.stderr)
return
print("\nscript output:")
print(r.stdout)
finally:
sbx.kill()
if __name__ == "__main__":
main()
import "dotenv/config";
import { Sandbox } from "@declaw/sdk";
const SCRIPT = `declare const process: { version: string };
type Order = { id: string; qty: number; price: number };
const orders: Order[] = [
{ id: "A-1", qty: 3, price: 19.99 },
{ id: "A-2", qty: 1, price: 49.50 },
{ id: "B-1", qty: 5, price: 4.25 },
];
const total = orders.reduce((s, o) => s + o.qty * o.price, 0);
const summary = {
orders: orders.length,
units: orders.reduce((s, o) => s + o.qty, 0),
total: Number(total.toFixed(2)),
node: process.version,
};
console.log(JSON.stringify(summary, null, 2));
`;
async function main(): Promise<void> {
const sbx = await Sandbox.create({ template: "node", timeout: 120 });
try {
for (const tool of ["node", "npm", "tsc"]) {
const r = await sbx.commands.run(`${tool} --version`);
console.log(`${tool}: ${r.stdout.trim()}`);
}
await sbx.files.write("/tmp/orders.ts", SCRIPT);
const r = await sbx.commands.run(
"cd /tmp && tsc --target ES2020 orders.ts && node orders.js",
);
if (r.exitCode !== 0) {
console.log("compile/run failed:", r.stderr);
return;
}
console.log("\nscript output:");
console.log(r.stdout);
} finally {
await sbx.kill();
}
}
main().catch(console.error);
Expected output
node: v20.20.2
npm: 10.8.2
tsc: Version 6.0.2
script output:
{
"orders": 3,
"units": 9,
"total": 130.72,
"node": "v20.20.2"
}
Need an actual TypeScript Declaw client inside the sandbox? npm install @declaw/sdk works at runtime against the node template — but for an
agent-in-sandbox pattern, the ai-agent template is usually a better
starting point.