# CLI profiles, updates and waiting

Three things that help once you use the [command-line tool](/docs/cli) every day or in scripts. Each one also shows how to do the same in code with the [SDKs](/docs/sdks) or the API.

## Several accounts: profiles

`apidly login --profile NAME` saves a key under a name, next to your default one. Choose a profile for one command with `--profile NAME` or the `APIDLY_PROFILE` environment variable, or make it the default with `apidly config use NAME`. `apidly config profiles` lists the profiles and `apidly config profiles rm NAME` removes one. When `APIDLY_API_KEY` is set, it wins over every saved profile.

```bash
apidly login --profile work
apidly config profiles
apidly servers --profile work
APIDLY_PROFILE=work apidly balance
apidly config use work
apidly config profiles rm work
```

Code has no profiles: each key is its own client. Keep each key in its own environment variable, never in the code.

```bash
curl "https://apidly.com/api/v1/me" \
  -H "Authorization: Bearer $APIDLY_WORK_KEY"
```

```js
import { Apidly } from "@apidly/sdk";

const personal = new Apidly({ apiKey: process.env.APIDLY_API_KEY });
const work = new Apidly({ apiKey: process.env.APIDLY_WORK_KEY });
console.log((await work.billing.balance()).balance);
console.log((await personal.billing.balance()).balance);
```

```python
import os
from apidly import Client

personal = Client(api_key=os.environ["APIDLY_API_KEY"])
work = Client(api_key=os.environ["APIDLY_WORK_KEY"])
print(work.billing.balance()["balance"])
print(personal.billing.balance()["balance"])
```

## Update the CLI

`apidly update` looks for a newer version, downloads it from this site, checks its checksum and installs it with npm after asking you. `--check` only reports, with exit code `3` when a newer version is out, which suits a CI step; `--print` prints the commands instead of running them. The current version and the checksum of every file are also in `latest.json`.

```bash
apidly --version
apidly update --check
apidly update --print
apidly update --yes

curl -fsS "https://apidly.com/downloads/latest.json" | jq -r '.version'
```

```js
const res = await fetch("https://apidly.com/downloads/latest.json");
const latest = await res.json();
const cli = latest.files.find((f) => f.kind === "cli");
console.log(latest.version, cli.name, cli.sha256);
```

```python
import json
import urllib.request

with urllib.request.urlopen("https://apidly.com/downloads/latest.json") as res:
    latest = json.load(res)
cli = next(f for f in latest["files"] if f["kind"] == "cli")
print(latest["version"], cli["name"], cli["sha256"])
```

## Wait for a server in a script

`apidly servers wait ID` returns with exit code `0` once the server is running, or `1` when it cannot get there, for example when the launch failed. `--for stopped` or `--for destroyed` waits for those states, and `--timeout` gives up after that many seconds (600 by default). `apidly deploy --wait` and `apidly stop --wait` wait in the same command. Ctrl-C ends the wait with exit code `130` and leaves the server as it is. With the API, read the server's `status` every few seconds.

```bash
apidly servers wait ins_ID
apidly servers wait ins_ID --for stopped --timeout 300
apidly deploy --plan rtx-4090-1x --template pytorch --yes --wait
apidly stop ins_ID --wait

until [ "$(curl -fsS "https://apidly.com/api/v1/servers/ins_ID" \
  -H "Authorization: Bearer $APIDLY_API_KEY" | jq -r '.server.status')" = running ]; do
  sleep 5
done
```

```js
import { Apidly } from "@apidly/sdk";

const apidly = new Apidly({ apiKey: process.env.APIDLY_API_KEY });
for (;;) {
  const { server } = await apidly.servers.get("ins_ID");
  if (server.status === "running") break;
  if (server.status === "error" || server.status === "destroyed") {
    throw new Error(server.statusMessage ?? server.status);
  }
  await new Promise((r) => setTimeout(r, 5000));
}
```

```python
import os
import time
from apidly import Client

client = Client(api_key=os.environ["APIDLY_API_KEY"])
while True:
    server = client.servers.get("ins_ID")["server"]
    if server["status"] == "running":
        break
    if server["status"] in ("error", "destroyed"):
        raise SystemExit(server.get("statusMessage") or server["status"])
    time.sleep(5)
```
