# Getting started with the API

## 1. Turn it on

The API is off on a new server. In the WebInterface, open **Preferences, API** and tick **Turn the API on**, then save.

On the same panel:

- **Addresses that may call it**: empty lets every address in. Admin operations also obey the Admin IPs of the Banning panel.
- **Accept a user name and password**, **Accept a browser session**: the credentials besides tokens (see [Credentials](API-Authentication.md)).
- **Requests a minute**: per credential, 600 by default.
- **Answer the health checks**: `/WebInterface/api/healthz` and `/readyz` for a load balancer. They answer without a login.

Every HTTP and HTTPS port answers the API while it is on. To keep one port out of it, clear **Answer the API** for that port on the **IP / Servers** panel (HTTP / HTTPS Options).

> [!NOTE]
> While the API is off, every API address answers 404, even for an administrator. The WebInterface works the same either way.

## 2. A token

A script or a tool should use an access token, not a password. A token carries only what you give it, ends when you say, and shows in the audit log under its label.

In the **User Manager**, pick the user the token works for (often your own administrator), open **Access**, and under **Access tokens** click **Issue token**:

| Field | What to put |
|---|---|
| Label | What uses it: "nightly user report", "Claude on the admin laptop". |
| Lifetime in minutes | How long it lives. 0 never expires. |
| Grants, one per line | What it may do: a permission, then where. `users.user.view\|users=conn:MainUsers` reads every user of MainUsers. You can only give rights you hold. |
| Read-only | Yes: it can read, never change. |
| Folders, file privileges, protocols | For file work: which of your folders, which privileges, over which protocols. |
| Addresses it may come from | Empty: any. |

The token is shown once, as `cft_<id>.<secret>`. Keep it like a password. A token can be revoked from the same list.

A token can also be issued through the API, by a login that holds the rights it carries:

```bash
curl -u admin -H "Content-Type: application/json" \
  -d '{"label": "user report", "minutes": 1440, "read_only": true,
       "grants": ["users.user.view|users=conn:MainUsers", "users.user.list|users=conn:MainUsers"]}' \
  https://server.example.com/WebInterface/api/v1/tokens
```

## 3. The first calls

```bash
export TOKEN='cft_...'
API=https://server.example.com/WebInterface/api/v1
```

**What may this token do?**

```bash
curl -H "Authorization: Bearer $TOKEN" $API/whoami
```

**The users of a group**, a page at a time:

```bash
curl -H "Authorization: Bearer $TOKEN" "$API/users/MainUsers?limit=50"
```

The answer is `{"items": [...], "next_cursor": "..."}`. Send `cursor=<next_cursor>` for the next page; `next_cursor` is `null` on the last one.

**One user:**

```bash
curl -H "Authorization: Bearer $TOKEN" $API/users/MainUsers/bob
```

**A change, first as a dry run.** `PATCH` changes only the fields sent; `?dry_run=true` checks everything and changes nothing, and answers what would change:

```bash
curl -X PATCH -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \
  -d '{"settings": {"email": "bob@example.com"}}' \
  "$API/users/MainUsers/bob?dry_run=true"
```

Without `dry_run` it is made, and the answer is the user as it is now, with the list of `changes`.

> [!TIP]
> Every operation in this reference has a **Try it with curl** box: fill in your server and the values, and copy the command. The token stays in your shell as `$TOKEN`; nothing is sent from this page.

## Next

- [Conventions](API-Conventions.md) every operation follows.
- [Errors](API-Errors.md): what a refusal looks like.
- The reference: the menu on the left, or [all of it on one page](API-Reference.md).
