# CLI-profielen, updates en wachten

Drie dingen die helpen zodra je het [opdrachtregelprogramma](/docs/cli) dagelijks of in scripts gebruikt. Bij elk staat ook hoe je hetzelfde in code doet met de [SDK's](/docs/sdks) of de API.

## Meerdere accounts: profielen

`apidly login --profile NAME` slaat een sleutel op onder een naam, naast je standaardsleutel. Kies een profiel voor één commando met `--profile NAME` of de omgevingsvariabele `APIDLY_PROFILE`, of maak het de standaard met `apidly config use NAME`. `apidly config profiles` toont de profielen en `apidly config profiles rm NAME` verwijdert er een. Als `APIDLY_API_KEY` is ingesteld, gaat die voor elk opgeslagen profiel.

```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 kent geen profielen: elke sleutel is een eigen client. Bewaar elke sleutel in een eigen omgevingsvariabele, nooit in de 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"])
```

## De CLI bijwerken

`apidly update` zoekt een nieuwere versie, downloadt die van deze site, controleert de checksum en installeert hem met npm nadat je het bevestigt. `--check` meldt alleen, met exitcode `3` als er een nieuwere versie is, handig in een CI-stap; `--print` toont de commando's in plaats van ze uit te voeren. De huidige versie en de checksum van elk bestand staan ook 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 een script op een server wachten

`apidly servers wait ID` stopt met exitcode `0` zodra de server draait, of `1` als hij daar niet kan komen, bijvoorbeeld omdat het starten mislukte. `--for stopped` of `--for destroyed` wacht op die toestanden, en `--timeout` geeft het op na zoveel seconden (standaard 600). `apidly deploy --wait` en `apidly stop --wait` wachten in hetzelfde commando. Ctrl-C beëindigt het wachten met exitcode `130` en laat de server zoals hij is. Met de API lees je om de paar seconden de `status` van de server.

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