# API and health endpoint monitoring

> Checks the JSON response of a health endpoint: status code and conditions on values through JSONPath, not only that the server answered.

A server can answer with code 200 and still report that its database is down. An API monitor reads the JSON response and verifies values in it, so it catches that kind of "healthy" outage too.

## What is checked

1. The checker sends a request to the health endpoint address.
2. It checks the status code. 200 is expected by default.
3. If conditions are set, the response has to be valid JSON (up to 1 MB). Each condition picks a value with JSONPath and compares it.
4. Conditions are evaluated in order and the first one that does not hold fails the check. You see the reason in the check detail.

## Monitor settings

The checker really reads all of this from the monitor configuration.

| Monitor settings | Where to set it |
|---|---|
| **Address** (`url`): The full URL of the health endpoint. | Dashboard and REST API |
| **Method** (`method`): GET (default), POST or HEAD. Any other method is sent as GET. | Dashboard and REST API |
| **Expected status code** (`expected_status`): The number the server has to return. 200 by default. | Dashboard and REST API |
| **Conditions on the response** (`assertions`): A list of conditions. Each has a jsonpath (where to find the value), an operator (how to compare it) and expected (to what). Operators: equals (alias eq), neq (alias not_equals), gt, gte, lt, lte, contains, exists. Only the first value found by the JSONPath is evaluated. The expected value is typed: gt, gte, lt and lte require a number, exists requires true or false (false means the value must not exist). | Dashboard and REST API |
| **Timeout** (`timeout_ms`): The longest time in milliseconds the checker waits for a response. Without a value the checker default applies. | Dashboard and REST API |

## Example: a response and the conditions on it

A health endpoint returns JSON. The monitor requires status to be "ok" and the number of database connections to be below 90. With the response below, the second condition would fail.

Health endpoint response

```
{
  "status": "ok",
  "db": {
    "connections": 97
  }
}
```

API monitor configuration through the REST API

```
{
  "name": "Health API",
  "type": "api_health",
  "interval_seconds": 300,
  "config": {
    "url": "https://api.example.com/health",
    "method": "GET",
    "expected_status": 200,
    "assertions": [
      { "jsonpath": "$.status", "operator": "equals", "expected": "ok" },
      { "jsonpath": "$.db.connections", "operator": "lt", "expected": 90 }
    ]
  }
}
```

## What counts as a failure

- The status code differs from the expected one.
- The response is not valid JSON although conditions are set.
- A JSONPath finds no value (except an exists condition expecting false) or the value does not meet the condition.
- The server does not answer within the timeout or cannot be reached.

## What happens on failure

- The monitor changes from UP to DOWN. If you use ignore blips, the failure first has to last for the time or the number of checks you set.
- An incident named after the monitor is created automatically. When a check passes again, the incident closes itself.
- Your alert rules send a message through the channels you set up: Slack, Discord, Telegram, email, SMS, push, webhook or ntfy.
- If the monitor is linked to a status page component, its state shows up there too.

## How it works with ignore blips

A short outage does not have to mean DOWN right away. For each monitor you can set state stabilization. Upstat decides it when it processes check results, not the checker.

- `consecutive_failures`: How many checks in a row must fail before the monitor is marked DOWN. For example 3 means three failures in a row.
- `min_down_seconds`: How long the failing has to last before the state changes to DOWN.
- `min_up_seconds`: How long everything has to be fine after an outage before the monitor goes back to UP. It protects against rapid flapping.

Health endpoints often report a degraded state for a moment during warm-up or when the database is overloaded. A condition that keeps failing for several checks in a row is a better signal than a single deviation.

[How monitoring works](https://upstat.sk/en/how-monitoring-works)

## Limits by plan

The shortest allowed interval between checks depends on the plan. The numbers are read straight from the plan settings.

| Plan | Shortest check interval |
|---|---|
| Hobby | 120 s |
| Starter | 60 s |
| Pro | 30 s |

## Questions about this type

### Which value does JSONPath compare if it finds several?

The first value found is used.

### Can custom headers, such as a token, be sent?

The API monitor does not read headers at the moment. For an endpoint that requires them, use a website monitor with custom headers.

## Other monitor types

- [Website](https://upstat.sk/en/monitors/website)
- [SSL certificate](https://upstat.sk/en/monitors/ssl)
- [TCP port](https://upstat.sk/en/monitors/port)
- [Heartbeat](https://upstat.sk/en/monitors/heartbeat)
- [Docker container](https://upstat.sk/en/monitors/docker)
- [Database](https://upstat.sk/en/monitors/database)
- [How monitoring works](https://upstat.sk/en/how-monitoring-works): The check cycle, outage confirmation and monitor states.
- [Alerting](https://upstat.sk/en/alerting): Rules, thresholds and alert channels.
