# Users

The users API of a self-hosted Expose server lists, creates, shows and deletes the users that may connect to the server. Each user has an auth token that the Expose client sends when it connects. The server only checks these tokens when `validate_auth_tokens` is enabled in the server configuration or on the settings page of the [admin interface](https://expose.dev/docs/server/server/admin-interface).

The API runs on the admin subdomain, `expose` by default, so the base URL is `https://expose.your-domain.com`. Every request needs HTTP basic authentication with one of the admin users from the configuration. See [Authorization](https://expose.dev/docs/server/api/authorization).

| Method | Endpoint | What it does |
|---|---|---|
| `GET` | `/api/users` | List and search users, 20 per page. |
| `POST` | `/api/users` | Create a user, or update the user with the same auth token. |
| `GET` | `/api/users/{id}` | Show one user by ID or auth token. |
| `DELETE` | `/api/users/{id}` | Delete a user by ID or auth token. |

## Retrieving all users

Send a GET request to `/api/users`:

```bash
curl -u username:password "https://expose.your-domain.com/api/users?search=miguel&page=1&perPage=20"
```

All query parameters are optional:

- `search` only returns users whose name contains the text.
- `page` is the page to return, starting at 1.
- `perPage` is the number of users per page. The default is 20.

The users are wrapped in a `paginated` object:

```json
{
    "paginated": {
        "total": 3,
        "users": [
            {
                "id": 9,
                "name": "miguel",
                "auth_token": "858fad3d-2163-4af6-8c8d-68e89f80cf8c",
                "can_specify_subdomains": 1,
                "can_specify_domains": 1,
                "can_share_tcp_ports": 0,
                "max_connections": 0,
                "created_at": "2020-06-04 19:31:26",
                "updated_at": null,
                "sites": [],
                "tcp_connections": []
            }
        ],
        "current_page": 1,
        "per_page": 20,
        "next_page": null,
        "previous_page": null
    }
}
```

`next_page` and `previous_page` are `null` when there is no next or previous page. `total` counts all users on the server, also when you search. Each user also lists the `sites` and `tcp_connections` it has open right now.

## Creating a new user

Send a POST request to `/api/users`:

```bash
curl -u username:password -X POST https://expose.your-domain.com/api/users \
  -H "Content-Type: application/json" \
  -d '{"name": "sebastian", "can_specify_subdomains": 1, "max_connections": 2}'
```

The endpoint accepts these fields:

- `name` is required.
- `auth_token` is optional. Without it, the server generates a UUID.
- `can_specify_subdomains` lets the user choose a subdomain with `--subdomain`. Send `1` or `0`.
- `can_specify_domains` lets the user share on a custom domain. Send `1` or `0`.
- `max_connections` is the number of connections the user can keep open at the same time. `0`, the default, means no limit.

Users created through the API can't share TCP ports: the server sets `can_share_tcp_ports` to `0`.

If you send an `auth_token` that already belongs to a user, the server updates that user's name and permissions instead of adding a new user.

The response contains the stored user:

```json
{
    "user": {
        "id": 8,
        "name": "sebastian",
        "auth_token": "360461ea-23b9-422e-bc76-7ca1b2ec8a91",
        "can_specify_subdomains": 1,
        "can_specify_domains": 0,
        "can_share_tcp_ports": 0,
        "max_connections": 2,
        "created_at": "2020-06-04 19:31:17",
        "updated_at": null
    }
}
```

Without a `name`, or with a `max_connections` that isn't a number, the server answers with status 401 and an `errors` object.

Give the user the `auth_token`. They save it in their client once with `expose token 360461ea-23b9-422e-bc76-7ca1b2ec8a91`.

## Retrieving a single user

Send a GET request to `/api/users/{id}`. Instead of the numeric ID you can pass the user's auth token. The response contains the `user` and the `subdomains` reserved for that user. If no user matches, the server answers with status 404.

## Deleting a user

Send a DELETE request to `/api/users/{id}`, with the numeric ID or the auth token:

```bash
curl -u username:password -X DELETE https://expose.your-domain.com/api/users/8
```

The server answers with `{"deleted": true}`, also when no user matched.

> **Note:** The user's currently active shared sites will not be disconnected automatically. Disconnect them with the [sites API](https://expose.dev/docs/server/api/sites) or in the admin interface.

If you keep users in your own database, the API reads and writes them through your [custom user repository](https://expose.dev/docs/server/extending-the-server/user-repository).
