Skip to content

Sandbox quickstart

Start a sandbox, run code in it and stop it

View .md

This starts a machine, writes a Python file to it, runs it and stops the machine. It costs about a cent.

  1. Create a sandbox

    import Zavudev from "@zavudev/sdk"; const zavu = new Zavudev({ apiKey: process.env["ZAVUDEV_API_KEY"], // This is the default and can be omitted }); const { sandbox } = await zavu.sandboxes.create({ size: "default", timeoutSeconds: 1800, idempotencyKey: "build-1042", }); console.log(sandbox.id, sandbox.status); // "starting"

    The answer is 202 with the sandbox in starting. You do not need to wait for running: commands and file calls made while it starts wait for it.

    Retrying with the same idempotencyKey answers 409 with the sandbox the first request created, instead of starting and charging a second one.

  2. Write a file and run it

    await zavu.sandboxes.files.write(sandbox.id, { path: "main.py", content: "print(2 + 2)\n", }); const { result } = await zavu.sandboxes.commands.run(sandbox.id, { command: "python3 main.py", }); console.log(result?.exitCode, result?.stdout); // 0 "4\n"
    JSON
    { "result": { "exitCode": 0, "stdout": "4\n", "stderr": "", "stdoutTruncated": false, "stderrTruncated": false, "timedOut": false, "startedAt": "…", "finishedAt": "…" } }
  3. Stop it

    await zavu.sandboxes.stop(sandbox.id);

    Billing ends here. The disk is kept 14 days: resume it with zavu.sandboxes.resume(id), or delete it for good with zavu.sandboxes.delete(id).

Next