Run code

Start a sandbox from your code and send it files and commands, so generated or untrusted code runs away from your machine. It bills to a project.

Before you start

You need an API key. See Create an API key.

Run a command

  1. Install the SDK and set your key and the ID of the project to bill (Find your IDs):
    Shell
    npm install veryfrontexport VERYFRONT_API_KEY=<your key>export VERYFRONT_PROJECT_REFERENCE=<project ID>
  2. Create the script. ttlMode and ttlHours delete the sandbox after an hour even if the script stops early:
    run.mjs
    import { Sandbox } from "veryfront/sandbox"; const sandbox = await Sandbox.create({  projectReference: process.env.VERYFRONT_PROJECT_REFERENCE,  ttlMode: "duration",  ttlHours: 1,}); await sandbox.writeFiles([  { path: "greet.mjs", content: 'console.log("Hello from a sandbox");' },]); const result = await sandbox.runCommand("node greet.mjs");console.log(result.stdout);console.log(await sandbox.readFile("greet.mjs")); await sandbox.delete();
  3. Run node run.mjs. You should now see it print "Hello from a sandbox" and the file's contents.

Try it in a sandbox

  1. Install the SDK

  2. Create the script

  3. Check it

Try it out

Good to know

  • Each sandbox has its own address, such as https://3218467769.sandbox.veryfront.com.
  • Its files are temporary: they can go when it restarts or stops, unless it has a persistent workspace.
  • A heartbeat from your code keeps an idle sandbox alive.
  • ttl_mode picks when it goes: default once it idles, duration after ttl_hours (up to 24), or always_on until you delete it. See Sharing and cleanup.

Troubleshooting

If you see "project_id (or an environment_id that belongs to a project) is required", the sandbox has no project to bill. Set the project's ID, as step 1 does, and run it again.

Next steps