# CLI-Profile, Updates und Warten

Drei Dinge, die helfen, sobald du das [Kommandozeilenwerkzeug](/docs/cli) täglich oder in Skripten nutzt. Jedes zeigt auch, wie du dasselbe im Code mit den [SDKs](/docs/sdks) oder der API erledigst.

## Mehrere Konten: Profile

`apidly login --profile NAME` speichert einen Schlüssel unter einem Namen, neben deinem Standardschlüssel. Wähle ein Profil für einen Befehl mit `--profile NAME` oder der Umgebungsvariable `APIDLY_PROFILE`, oder mach es mit `apidly config use NAME` zum Standard. `apidly config profiles` listet die Profile auf, `apidly config profiles rm NAME` entfernt eines. Ist `APIDLY_API_KEY` gesetzt, gilt er vor jedem gespeicherten Profil.

```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
```

Im Code gibt es keine Profile: Jeder Schlüssel ist ein eigener Client. Lege jeden Schlüssel in einer eigenen Umgebungsvariable ab, nie im 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"])
```

## CLI aktualisieren

`apidly update` sucht nach einer neueren Version, lädt sie von dieser Website, prüft ihre Prüfsumme und installiert sie nach Rückfrage mit npm. `--check` meldet nur, mit Exit-Code `3`, wenn es eine neuere Version gibt, passend für einen CI-Schritt; `--print` gibt die Befehle aus, statt sie auszuführen. Die aktuelle Version und die Prüfsumme jeder Datei stehen auch 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"])
```

## In einem Skript auf einen Server warten

`apidly servers wait ID` endet mit Exit-Code `0`, sobald der Server läuft, oder mit `1`, wenn er nicht dorthin kommt, zum Beispiel weil der Start fehlschlug. `--for stopped` oder `--for destroyed` wartet auf diese Zustände, und `--timeout` gibt nach so vielen Sekunden auf (standardmäßig 600). `apidly deploy --wait` und `apidly stop --wait` warten im selben Befehl. Strg-C beendet das Warten mit Exit-Code `130` und lässt den Server, wie er ist. Mit der API liest du alle paar Sekunden den `status` des Servers.

```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)
```
