# API checks

> Run up to 5 HTTP requests in order, pass values from one response to the next, and get alerted when any step fails.

Some things can't be tested with one request. To check that users can log in and load their account, you need to log in first, take the token from the response, and use it in the next request. An API check does exactly that.

## How it works [#how-it-works]

- An API check has 1 to 5 steps. Opush runs them in order on each check.
- Each step is one HTTP request with its own checks.
- A step can save values from its JSON response. Later steps use them with `{{name}}`.
- If any step fails, Opush stops there. The check counts as failed.
- The response time is the total for all steps.

API checks follow the same rules as other monitors: two failures in a row plus a second-location check before they're down. See [Monitoring](/docs/monitors).

## Add an API check [#add-an-api-check]

In the app, open **Following**, tap **Add**, choose **Monitor**, then **Website**, and pick **API check**. In the web console, go to **Monitors**, click **Add monitor**, and choose **API check**.

## Step settings [#step-settings]

<TypeTable
  type={{
    "Method": {
      type: "GET | HEAD | POST | PUT | PATCH | DELETE | OPTIONS",
      description: "Default GET.",
    },
    "URL": {
      type: "http or https",
      description: "Required. Can contain {{name}}.",
    },
    "Headers": {
      type: "up to 10",
      description: "Values can contain {{name}}. Secret-looking values are hidden after saving, like on website monitors.",
    },
    "Body": {
      type: "up to 10,000 characters",
      description: "Can contain {{name}}. Not sent with GET or HEAD.",
    },
    "Expected status": {
      type: "codes or ranges",
      description: "Like 200 or 200-299,301. Empty means any status below 400.",
    },
    "Must contain": {
      type: "up to 200 characters",
      description: "Fail the step when the response doesn't have this text.",
    },
    "Must not contain": {
      type: "up to 200 characters",
      description: "Fail the step when the response has this text.",
    },
    "Save values from the JSON response": {
      type: "up to 5 per step",
      description: "A name and a path, like token = data.token.",
    },
  }}
/>

Each request waits up to 10 seconds. Redirects work like on [website monitors](/docs/monitors/website): followed, unless you expect a `3xx` status.

## Save and reuse values [#save-and-reuse-values]

Give each value a name and a path into the JSON response.

| Response | Path | Value |
|---|---|---|
| `{"data":{"token":"abc"}}` | `data.token` | `abc` |
| `{"items":[{"id":42}]}` | `items[0].id` or `items.0.id` | `42` |
| `{"ok":true}` | `ok` | `true` |

- Paths use dots for keys and `[n]` (or `.n`) for list items. A leading `$.` is allowed.
- The value must be text, a number, or `true`/`false`. An object, a list, or an empty value doesn't count.
- Use short names with lowercase letters and digits, like `token` or `userid`. These work everywhere, including the iPhone app.

Write `{{name}}` in a later step's URL, header values, or body. Opush replaces it with the saved value.

If the response isn't JSON, or a path finds nothing, the step fails with **Couldn't read the JSON value**.

## Example: log in, then load the account [#example-log-in-then-load-the-account]

**Step 1: log in**

| Setting | Value |
|---|---|
| Method | `POST` |
| URL | `https://api.example.com/login` |
| Headers | `Content-Type: application/json` |
| Body | `{"email":"monitor@example.com","password":"…"}` |
| Expected status | `200` |
| Save values | `token` = `data.token` |

**Step 2: load the account**

| Setting | Value |
|---|---|
| Method | `GET` |
| URL | `https://api.example.com/me` |
| Headers | `Authorization: Bearer {{token}}` |
| Expected status | `200` |
| Must contain | `monitor@example.com` |

<Callout type="warn" title="Use a test account">
Request bodies are stored as you type them and aren't hidden. Create a separate account with no real data or permissions for monitoring.
</Callout>

## Example: a value in the URL [#example-a-value-in-the-url]

This checks that the latest release of a GitHub repo can be loaded:

| Step | URL | Save values |
|---|---|---|
| 1 | `https://api.github.com/repos/OWNER/REPO` | `name` = `full_name` |
| 2 | `https://api.github.com/repos/{{name}}/releases/latest` | — |

## The same check as JSON [#the-same-check-as-json]

If you work with the API, steps look like this:

```json
{
  "type": "api",
  "name": "Login flow",
  "interval": 5,
  "steps": [
    {
      "method": "POST",
      "url": "https://api.example.com/login",
      "headers": [{ "name": "Content-Type", "value": "application/json" }],
      "body": "{\"email\":\"monitor@example.com\",\"password\":\"…\"}",
      "expect_status": "200",
      "extract": { "token": "data.token" }
    },
    {
      "url": "https://api.example.com/me",
      "headers": [{ "name": "Authorization", "value": "Bearer {{token}}" }],
      "expect_status": "200",
      "contains": "monitor@example.com"
    }
  ]
}
```

## When a step fails [#when-a-step-fails]

The push says which step failed and why:

```text
Login flow
Down
Step 2 · HTTP 401 · confirmed from Western Europe
```

In the app and the web console, the monitor shows the failed step too.

## Edit steps [#edit-steps]

You can add, remove, and change steps anytime. Saving replaces the whole list of steps. API checks don't get domain and certificate expiry reminders. Add a [website monitor](/docs/monitors/website) for that.
