# Schalen, versies en logs van een endpoint vanuit code

Deze pagina doet vanuit een script wat in de console de kaart **Schalen en uitgaven**, de kaart **Versies** en de workerlogs van een endpoint doen. De voorbeelden gebruiken het endpoint-id `ep_ID`. Lezen kan met een `read`-sleutel; wijzigingen vragen een sleutel met de scope `full`. Een sleutel die aan één endpoint gebonden is, kan alleen dat endpoint aanroepen en dus niets hiervan. Serverloze endpoints staan misschien nog niet aan voor je account. Wat elke instelling betekent, staat in [Serverloze eindpunten](/docs/endpoints).

## Een schaalpreset toepassen

Toon de drie presets met wat elk per uur kost tegen de prijs van vandaag, zonder verkeer (`idleHourlyMicros`) en bij volle belasting (`maxHourlyMicros`); `current` markeert de preset waar het endpoint al aan voldoet. Pas er daarna een toe. Een preset wordt opgeslagen zoals elke andere wijziging, wordt dus ook een versie, en start nooit een geleidelijke update.

```bash
curl "https://apidly.com/api/v1/endpoints/ep_ID/presets" \
  -H "Authorization: Bearer $APIDLY_API_KEY"

curl -X POST "https://apidly.com/api/v1/endpoints/ep_ID/presets" \
  -H "Authorization: Bearer $APIDLY_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"preset":"scale_to_zero"}'

apidly endpoints presets ep_ID
apidly endpoints preset ep_ID scale_to_zero
```

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

const apidly = new Apidly({ apiKey: process.env.APIDLY_API_KEY });
const { presets } = await apidly.endpoints.presets("ep_ID");
for (const p of presets) {
  console.log(p.name, p.idleHourlyMicros, p.maxHourlyMicros, p.current);
}
await apidly.endpoints.applyPreset("ep_ID", "scale_to_zero");
```

```python
import os
from apidly import Client

client = Client(api_key=os.environ["APIDLY_API_KEY"])
for p in client.endpoints.presets("ep_ID")["presets"]:
    print(p["name"], p["idleHourlyMicros"], p["maxHourlyMicros"], p["current"])
client.endpoints.apply_preset("ep_ID", "scale_to_zero")
```

## Een maandelijkse bestedingslimiet instellen

De limiet is in Amerikaanse dollars per kalendermaand (UTC). Bij de limiet pauzeert het endpoint zichzelf, geeft zijn workers vrij en mailt de eigenaar. Met `resumeAtNewMonth` gaat het vanzelf verder als de volgende maand begint. Stuur `null` (in de CLI `none`) om de limiet te verwijderen.

```bash
curl -X PATCH "https://apidly.com/api/v1/endpoints/ep_ID" \
  -H "Authorization: Bearer $APIDLY_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"monthlySpendCapUsd":50,"resumeAtNewMonth":true}'

apidly endpoints update ep_ID --monthly-cap 50 --resume-at-new-month on
apidly endpoints update ep_ID --monthly-cap none
```

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

const apidly = new Apidly({ apiKey: process.env.APIDLY_API_KEY });
await apidly.endpoints.update("ep_ID", {
  monthlySpendCapUsd: 50,
  resumeAtNewMonth: true,
});
const { endpoint } = await apidly.endpoints.get("ep_ID");
console.log(endpoint.spendCap);
```

```python
import os
from apidly import Client

client = Client(api_key=os.environ["APIDLY_API_KEY"])
client.endpoints.update("ep_ID", {"monthlySpendCapUsd": 50, "resumeAtNewMonth": True})
print(client.endpoints.get("ep_ID")["endpoint"]["spendCap"])
```

## Versies tonen en herstellen

Elke opgeslagen wijziging is een genummerde versie, de nieuwste eerst, met omgevingsvariabelen alleen bij naam. Herstellen zet de instellingen van een versie terug als nieuwe versie; verandert dat de workerinstelling, dan start een geleidelijke update. De CLI vraagt het eerst, tenzij je `--yes` meegeeft.

```bash
curl "https://apidly.com/api/v1/endpoints/ep_ID/releases?limit=20" \
  -H "Authorization: Bearer $APIDLY_API_KEY"

curl -X POST "https://apidly.com/api/v1/endpoints/ep_ID/releases/VERSION/restore" \
  -H "Authorization: Bearer $APIDLY_API_KEY"

apidly endpoints releases ep_ID --limit 20
apidly endpoints restore ep_ID 3
```

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

const apidly = new Apidly({ apiKey: process.env.APIDLY_API_KEY });
const { releases } = await apidly.endpoints.releases("ep_ID", { limit: 20 });
console.log(releases.map((r) => r.version));
const { changed } = await apidly.endpoints.restoreRelease("ep_ID", 3);
console.log(changed);
```

```python
import os
from apidly import Client

client = Client(api_key=os.environ["APIDLY_API_KEY"])
for r in client.endpoints.releases("ep_ID", limit=20)["releases"]:
    print(r["version"], r["changes"])
print(client.endpoints.restore_release("ep_ID", 3)["changed"])
```

## Workerlogs lezen

De logs van het endpoint zijn de nieuwste regels van elke worker, tot 12 workers per aanroep, of van één worker met `worker`. Het log van een job is het log van de worker die hem uitvoerde; `available: false` met `reason` `not_dispatched` of `worker_gone` betekent dat er niets te lezen is. `tail` bepaalt het aantal regels, hoogstens 500. Geheime waarden zijn in elk log verborgen.

```bash
curl "https://apidly.com/api/v1/endpoints/ep_ID/logs?tail=100" \
  -H "Authorization: Bearer $APIDLY_API_KEY"

curl "https://apidly.com/api/v1/endpoints/ep_ID/logs?worker=ins_ID&tail=200" \
  -H "Authorization: Bearer $APIDLY_API_KEY"

curl "https://apidly.com/api/v1/endpoints/ep_ID/jobs/job_ID/logs?tail=200" \
  -H "Authorization: Bearer $APIDLY_API_KEY"

apidly endpoints logs ep_ID --tail 100
apidly endpoints logs ep_ID --worker ins_ID
apidly endpoints job-logs ep_ID job_ID
```

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

const apidly = new Apidly({ apiKey: process.env.APIDLY_API_KEY });
const { workers } = await apidly.endpoints.logs("ep_ID", { tail: 100 });
for (const w of workers) {
  console.log(`== ${w.serverId} (${w.status})\n${w.logs}`);
}
const { job } = await apidly.endpoints.jobLogs("ep_ID", "job_ID", { tail: 200 });
console.log(job.available ? job.logs : job.reason);
```

```python
import os
from apidly import Client

client = Client(api_key=os.environ["APIDLY_API_KEY"])
for w in client.endpoints.logs("ep_ID", tail=100, worker="ins_ID")["workers"]:
    print(w["serverId"], w["logs"])
job = client.endpoints.job_logs("ep_ID", "job_ID", tail=200)["job"]
print(job["logs"] if job["available"] else job["reason"])
```
