# Script flockvm from the shell

> Shell recipes for the flockvm CLI: create several VMs in a loop, wait for them, run commands, load a .env into an environment, clean up, and run in CI.

- URL: https://flockvm.com/docs/cli/scripting
- Section: CLI
- Last updated: 2026-11-01

The CLI is made for scripts: `--json` prints the API's JSON for `jq`, errors go to standard error, and the exit code says what happened (see [Exit codes](https://flockvm.com/docs/cli/overview#exit-codes)). The recipes below use `bash` and `jq`.

## Authenticate a script

In a script or a CI job, give the key in `FLOCKVM_API_KEY` instead of running `flockvm login`. It wins over a saved key.

```bash
export FLOCKVM_API_KEY="$(cat ~/.config/flockvm-ci.key)"
flockvm whoami
```

Create a key for each script or job, with an expiry, in [Settings → API keys](https://flockvm.com/dashboard/settings/api-keys). It acts with its user's role, within its own scope: a key that only creates and deletes VMs can belong to a member, with the **Create and use VMs** preset (it deletes only the VMs it created). See [API keys and scopes](https://flockvm.com/docs/api/api-keys).

## Create several VMs

One VM per attempt or per task, with a shared prefix so the batch is easy to find and clean up:

```bash title="create-vms.sh"
#!/bin/bash
set -uo pipefail
for i in 1 2 3 4 5; do
  name="fix-auth-$i"
  if flockvm vm create "$name" --size small --json > /dev/null; then
    echo "created $name"
  else
    echo "skipped $name" >&2
  fi
done
```

Each `vm create` is checked like the dashboard's **New VM**: billing, your role, the environment. When the host has no room left for the size, that VM is refused: the CLI prints the host's reason followed by `(409 capacity, size)`, exits with `1`, and the loop goes on with the next one.

How many VMs fit depends on the host and the sizes already running: see [Limits](https://flockvm.com/docs/vms/limits). `flockvm regions` shows the sizes the region has room for right now.

To create them in parallel and return once all are running, start each `vm create --wait` in the background:

```bash
for i in 1 2 3; do
  flockvm vm create "fix-auth-$i" --size small --wait --json > /dev/null &
done
wait
```

## List the batch

`--json` and `jq` give you each VM's name and SSH port, in the `vms.txt` format the [parallel agents guide](https://flockvm.com/docs/agents/parallel-agents) loops over:

```bash
flockvm vm list --json \
  | jq -r '.vms[] | select(.name | startswith("fix-auth-")) | "\(.name) \(.ssh.port)"' \
  | sort > vms.txt
```

```text title="vms.txt"
fix-auth-1 22001
fix-auth-2 22002
fix-auth-3 22003
```

For the full SSH command of one VM:

```bash
flockvm vm get fix-auth-1 --json | jq -r .ssh.command
```

## Run a command in every VM

`flockvm vm ssh` passes what follows `--` to `ssh`, so a remote command runs as on any SSH host. `-n` keeps `ssh` from reading the loop's input:

```bash
while read -r name port; do
  echo "== $name"
  flockvm vm ssh "$name" -- -n 'cd ~/repo && git log -1 --oneline'
done < vms.txt
```

## Pause or delete a batch

```bash title="Pause every VM of the batch"
flockvm vm list --json \
  | jq -r '.vms[] | select(.name | startswith("fix-auth-")) | .name' \
  | while read -r name; do flockvm vm pause "$name"; done
```

```bash title="Delete every VM of the batch"
flockvm vm list --json \
  | jq -r '.vms[] | select(.name | startswith("fix-auth-")) | .id' \
  | while read -r id; do flockvm vm delete "$id" --yes; done
```

`vm delete` needs `--yes` when nobody can answer its question. Deleting a VM destroys its disk: push what you want to keep first.

## Load a .env into an environment

`--from-dotenv` sets every variable of a `.env` file in an [environment](https://flockvm.com/docs/vms/environments). It reads `KEY=value`, `export KEY=value`, comments and quoted values. Changing an environment takes an owner's or an admin's key.

```bash
flockvm env create staging
flockvm env set --from-dotenv .env.staging --env staging
flockvm env get staging
```

To put the file itself in every new VM, for example at `~/app/.env`:

```bash
flockvm env file add app/.env ./.env.staging --env staging
```

A secret from a password manager goes through standard input, so it never appears in your shell history or in `ps`:

```bash
op read "op://Engineering/Stripe/test-key" | flockvm env set STRIPE_KEY=- --env staging
```

Then create VMs with it: `flockvm vm create web-1 --env staging`.

## Run in CI

The CLI runs anywhere Node.js 20 runs. In a GitHub Actions job, keep the key in a repository secret and run it with `npx`:

```yaml title=".github/workflows/preview.yml"
jobs:
  preview:
    runs-on: ubuntu-latest
    env:
      FLOCKVM_API_KEY: ${{ secrets.FLOCKVM_API_KEY }}
    steps:
      - run: npx flockvm vm create "pr-${{ github.event.number }}" --size small --wait
      - run: npx flockvm vm get "pr-${{ github.event.number }}" --json
```

The VM gets the first SSH key saved in the key's account; if there is none, and no `~/.ssh/id_ed25519.pub` on the runner, add `--ssh-key` with a `.pub` file. This creates a flockvm VM from a job. To run the job itself in a flockvm VM, use the `runs-on: flockvm` labels: see [GitHub Actions](https://flockvm.com/docs/ci/overview).

## Check the exit code

| Code | In a script                                                                                   |
| ---- | --------------------------------------------------------------------------------------------- |
| `0`  | Go on.                                                                                        |
| `1`  | The API refused or something failed: the message is on standard error, as JSON with `--json`. |
| `2`  | The command line is wrong, or a confirmation is needed: add `--yes`.                          |

With `--json`, the error's `code` (`forbidden`, `capacity`, `conflict`…) tells the cases apart:

```bash
if ! out=$(flockvm vm create web-1 --json 2>&1); then
  code=$(echo "$out" | jq -r .error.code)
  [ "$code" = "capacity" ] && flockvm vm create web-1 --size small --json
fi
```

The codes are listed in [API overview](https://flockvm.com/docs/api/overview#errors).
