# Run code

Start a [sandbox](/docs/sandboxes.md) 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](/docs/authentication/api-keys.md).

## Run a command

**Code**

1. Install the SDK and set your key and the ID of the project to bill ([Find your IDs](/docs/authentication/api-keys.md#find-your-ids)):

```bash
npm install veryfront
export 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:

```js title="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

```playground
step: Install the SDK | npm install veryfront
step: Create the script | printf '%s\n' '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();' > run.mjs
check: node run.mjs
expect: Hello from a sandbox
```

**API**

1. Set `PROJECT` to the ID of the project to bill ([Find your IDs](/docs/authentication/api-keys.md#find-your-ids)): `export PROJECT=<id>`.
2. Create the sandbox. `jq` keeps its `id` as `SANDBOX_ID`:

```bash
export SANDBOX_ID=$(curl -s -X POST https://api.veryfront.com/sandboxes \
  -H "Authorization: Bearer $VERYFRONT_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "access_scope": "project", "project_reference": "'$PROJECT'", "ttl_mode": "duration", "ttl_hours": 1 }' | jq -r .id)
```

3. Write a file:

```bash
curl -X POST https://api.veryfront.com/sandboxes/$SANDBOX_ID/files \
  -H "Authorization: Bearer $VERYFRONT_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "files": [{ "path": "greet.mjs", "content": "console.log(\"Hello from a sandbox\");" }] }'
```

4. Run it. You should now see `stdout` read "Hello from a sandbox":

```bash
curl -X POST https://api.veryfront.com/sandboxes/$SANDBOX_ID/commands/run \
  -H "Authorization: Bearer $VERYFRONT_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "command": "node greet.mjs" }'
```

5. Delete the sandbox:

```bash
curl -X DELETE https://api.veryfront.com/sandboxes/$SANDBOX_ID \
  -H "Authorization: Bearer $VERYFRONT_API_KEY"
```

### Try it in a sandbox

```playground
step: Set PROJECT | export PROJECT=$VERYFRONT_PROJECT_REFERENCE
step: Create the sandbox | export SANDBOX_ID=$(curl -s -X POST https://api.veryfront.com/sandboxes -H "Authorization: Bearer $VERYFRONT_API_KEY" -H "Content-Type: application/json" -d '{ "access_scope": "project", "project_reference": "'$PROJECT'", "ttl_mode": "duration", "ttl_hours": 1 }' | jq -r .id)
step: Write a file | curl -X POST https://api.veryfront.com/sandboxes/$SANDBOX_ID/files -H "Authorization: Bearer $VERYFRONT_API_KEY" -H "Content-Type: application/json" -d '{ "files": [{ "path": "greet.mjs", "content": "console.log(\"Hello from a sandbox\");" }] }'
check: curl -X POST https://api.veryfront.com/sandboxes/$SANDBOX_ID/commands/run -H "Authorization: Bearer $VERYFRONT_API_KEY" -H "Content-Type: application/json" -d '{ "command": "node greet.mjs" }'
expect: Hello from a sandbox
```

## 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](/docs/sandboxes/project.md#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.

## With MCP

1. Call `veryfront__create_sandbox` with `"access_scope": "project"`, `"project_reference": "my-project"`, `"ttl_mode": "duration"` and `"ttl_hours": 1`.
2. Call `veryfront__write_sandbox_files` with the returned `id` as `sandbox_id` and `"files": [{ "path": "greet.mjs", "content": "console.log(\"Hello from a sandbox\");" }]`.
3. Call `veryfront__run_sandbox_command` with the same `sandbox_id` and `"command": "node greet.mjs"`. It worked when `stdout` is `Hello from a sandbox`.
4. Call `veryfront__delete_sandbox` with the same `sandbox_id` and `"confirm": true`.

## Next steps

* [Open a sandbox shell](/docs/sandboxes/project.md)
* [Debug a run](/docs/observability/debug.md)
