# API quickstart

> From a new API token to a tested HTTP check and its first results, with curl and jq.

This creates one HTTP check through the REST API, tests it from several countries, and reads its results. You need `curl`, `jq` and an account.

## 1. A token

In the dashboard, open **Settings → API tokens → Create token**. Give it the scopes `checks:read`, `checks:write`, `nodes:read` and `incidents:read`, and keep the value in the environment:

```sh
export MONITORLY_TOKEN=mly_…
export API=https://app.monitorly.cloud/api/v1
```

The value is shown once. Never put it in a file you commit ([Authentication](https://docs.monitorly.cloud/api/authentication.md)).

## 2. Workspace, project and nodes

```sh title="Your workspaces"
curl -fsS "$API/workspaces" -H "Authorization: Bearer $MONITORLY_TOKEN" | jq '.[] | {id, name}'
```

```sh title="Projects of the first workspace"
WORKSPACE=$(curl -fsS "$API/workspaces" -H "Authorization: Bearer $MONITORLY_TOKEN" | jq -r '.[0].id')
curl -fsS "$API/workspaces/$WORKSPACE/projects" -H "Authorization: Bearer $MONITORLY_TOKEN" | jq '.[] | {id, name}'
PROJECT=$(curl -fsS "$API/workspaces/$WORKSPACE/projects" -H "Authorization: Bearer $MONITORLY_TOKEN" | jq -r '.[0].id')
```

A check runs on the nodes you choose. List them with their location and health, and pick two or more in different countries, so that one failing location cannot open an incident on its own ([How DOWN is decided](https://docs.monitorly.cloud/concepts/quorum.md)):

```sh title="Nodes"
curl -fsS "$API/workspaces/$WORKSPACE/nodes" -H "Authorization: Bearer $MONITORLY_TOKEN" \
  | jq '.[] | {id, name, countryCode, health: .health.status}'
NODES=$(curl -fsS "$API/workspaces/$WORKSPACE/nodes" -H "Authorization: Bearer $MONITORLY_TOKEN" \
  | jq -c '[.[] | select(.health.status == "online") | .id][:3]')
```

## 3. Test before saving

A test runs the check once on each of its nodes and saves nothing:

```sh title="check.json"
cat > check.json <<EOF
{
  "name": "payments-health",
  "intervalSecs": 300,
  "nodes": $NODES,
  "kind": {
    "type": "http",
    "method": "GET",
    "url": "https://api.acme.io/healthz",
    "maxRedirects": 0,
    "assertions": [{ "target": "status", "op": "eq", "value": 200 }]
  }
}
EOF
```

Five minutes is an interval every plan allows; the shortest is on [Plans and limits](https://docs.monitorly.cloud/concepts/plans.md).

```sh title="Start the test, then read its results"
TEST=$(jq '{check: .}' check.json | curl -fsS "$API/projects/$PROJECT/check-tests" \
  -H "Authorization: Bearer $MONITORLY_TOKEN" -H "Content-Type: application/json" --data @- | jq -r '.id')
sleep 15
curl -fsS "$API/check-tests/$TEST" -H "Authorization: Bearer $MONITORLY_TOKEN" \
  | jq '{done, results: [.results[] | {nodeId, status, outcome, statusCode, ms: .timings.totalMs}]}'
```

`done` turns `true` when every node has answered or its deadline passed: the check's timeout plus 5 seconds.

## 4. Create the check

```sh title="Create it, safe to retry"
CHECK=$(curl -fsS "$API/projects/$PROJECT/checks" \
  -H "Authorization: Bearer $MONITORLY_TOKEN" -H "Content-Type: application/json" \
  -H "Idempotency-Key: payments-health-1" --data @check.json | jq -r '.check.id')
```

A retry with the same `Idempotency-Key` returns the first answer and creates nothing again ([Idempotency](https://docs.monitorly.cloud/api/idempotency.md)).

## 5. Read its results

```sh title="Status, uptime and each location"
curl -fsS "$API/checks/$CHECK" -H "Authorization: Bearer $MONITORLY_TOKEN" \
  | jq '{status, uptime30dPercent, stations: [.stations[] | {nodeName, outcome, totalMs}]}'
```

```sh title="The latest runs"
curl -fsS "$API/checks/$CHECK/runs?limit=10" -H "Authorization: Bearer $MONITORLY_TOKEN" \
  | jq '.items[] | {slotAt, nodeId, outcome, statusCode, totalMs}'
```

```sh title="Incidents of the project"
curl -fsS "$API/projects/$PROJECT/incidents?state=open" -H "Authorization: Bearer $MONITORLY_TOKEN" \
  | jq '.items[] | {checkName, openedAt, ackedAt}'
```

`status` is absent until the check is first evaluated, then `PENDING` until its first decision. Alerts go to the project's alert channels: add one with [Add an alert channel](https://docs.monitorly.cloud/api/reference/operations/create-project-alert-channel.md) or in the dashboard ([Alert channels](https://docs.monitorly.cloud/alerts.md)).

## When something fails

Errors are `application/problem+json`. Branch on `type`, not on the text:

```json
{ "type": "token.scope_missing", "title": "Forbidden", "status": 403, "detail": "this token needs the scope checks:write", "errors": [] }
```

Field errors point at the field in `errors[].pointer`, for example `/kind/url`. Every code is on [Errors](https://docs.monitorly.cloud/api/errors.md), and every operation with its parameters, scopes and examples is in the [REST reference](https://docs.monitorly.cloud/api/reference.md).
