# Keep an eye on spending from scripts

The console shows your balance and usage, but a script can watch them too. This guide shows a balance check that fails with its own exit code when money runs low, a live view for a terminal, and one server's charges day by day. You need the `apidly` command (see [Use the CLI and API from scratch](/docs/tutorial-cli-and-api)). A key with **Read only** is enough for everything here, and it cannot start or stop anything.

## 1. Check the balance once

```bash
apidly balance
apidly balance --below 20
```

The first command shows **Balance**, **Burn rate** (what your running servers cost per hour), **Runway**, **Month to date** and **State**. The second adds a line **Below $20.00** with `yes` or `no`, and the command's exit code tells a script the answer:

| Exit code | Meaning |
|---|---|
| `0` | The balance is at or above the amount. |
| `3` | The balance is below the amount. |
| `1` | The check failed, for example a network error or a rejected key. |

The amount is in US dollars and must be 0 or more. With `--json` the answer also carries `"below": true` or `false`.

## 2. Watch the balance in a terminal

```bash
apidly balance --watch
apidly balance --watch --interval 300 --below 20
```

`--watch` reads the balance again every 60 seconds until you press Ctrl-C. `--interval` sets the seconds between readings, from 10 to 3600. With `--below`, the watch ends at the first reading below the amount, with exit code `3`, so you can chain a command after it. Each reading is one read call; nothing is changed.

## 3. See one server's charges per day

```bash
apidly usage --server ins_ID --by-day
apidly usage --server ins_ID --by-day --from 2026-11-01 --to 2026-11-15
```

You get one row per UTC day with **COMPUTE**, **STORAGE** and **TOTAL**, and a **Total** line under the table. Without `--from` and `--to` the range is the current UTC month; `--to` is exclusive, and a range can be at most 366 days. Only that server's compute and disk are counted: volume charges belong to no server and are left out. Without `--server`, `apidly usage --by-day` shows all servers together. In the console, the server's **Usage** tab shows the same.

## 4. A cron job that warns you

Save this as `check-balance.sh` and make it executable with `chmod +x check-balance.sh`:

```bash
#!/bin/sh
apidly balance --below 20 >/dev/null
status=$?
if [ "$status" -eq 3 ]; then
  echo "Apidly balance is below 20 USD" | mail -s "Low balance" you@example.com
elif [ "$status" -ne 0 ]; then
  echo "Balance check failed with exit code $status" | mail -s "Balance check failed" you@example.com
fi
```

Then run `crontab -e` and add a line that runs it every hour:

```text
0 * * * * APIDLY_API_KEY=your-read-only-key /path/to/check-balance.sh
```

Replace `mail` with whatever your machine uses to send a message, for example a chat webhook with `curl`.

## 5. A CI step that stops before spending

In CI, a step fails when its command exits with anything but `0`. Put this before a step that starts servers, with the key stored as a secret of your CI system:

```bash
apidly balance --below 10
```

If the balance is below 10 USD the step fails with exit code `3`, and the job stops before it starts a server.

## What it costs and how to stop spending

These commands only read, so they cost nothing. To stop the spending they warn you about, open **Servers** or run `apidly servers`, then stop what you will use again (`apidly stop ins_ID`, the disk is still charged) and destroy the rest (`apidly destroy ins_ID`). The console also sends a low-balance email; see [Billing and balance](/docs/billing-and-balance).

## Next

- [Control costs: stop, auto-stop, budgets and auto top-up](/docs/tutorial-save-money)
- [Logs and usage](/docs/server-logs-and-usage)
