# Service settings

> How to read and change the eKasa settings through the HTTP API, how passwords and other secrets behave, and how version 2 differs from version 1.

Source: https://ekasa.ninedigit.sk/en/docs/rest-api/settings

You can read and change the eKasa settings (login, CORS, printers, extensions, hosting and more) from your software
just like in the web interface. The merchant then does not have to set them up by hand.

This article applies to the HTTP API. The .NET library is set up differently, see
[Configuration](https://ekasa.ninedigit.sk/en/docs/dotnet/configuration).

From version 9.1.0, the settings are also available in **version 2** (`/api/v2/settings`). In it, the `GET` response
can be sent back unchanged, single values can be changed without sending the whole settings, and a stored password
or key can be removed. Connect a new integration to version 2. Version 1 behaves as before; the differences are in
the [comparison below](#comparison-with-version-1).

## Overview

| Request | What it does | Version 1 | Version 2 |
|---|---|---|---|
| `GET …/settings` | returns the current settings | yes | yes, the same response |
| `PUT …/settings` | replaces the whole settings | yes | yes |
| `PATCH …/settings` | changes only the values sent | no | yes |

The settings fields and responses are described in the
[HTTP API reference](https://ekasa.ninedigit.sk/en/docs/rest-api/reference#tag/settings/GET/api/v2/settings).

## Passwords and other secrets

eKasa stores secrets but does not return them in responses. Instead of the value there is `null`, and next to it
a flag telling whether the secret is stored:

| Secret | Flag |
|---|---|
| `authentication.users[].password` | `authentication.users[].hasPassword` |
| `printers.email.credentials.password` | `printers.email.credentials.hasPassword` |
| `hosting.https.certificates.pfx` | `hosting.https.certificates.hasPfx` |
| `hosting.https.certificates.password` | `hosting.https.certificates.hasPassword` |
| `extensions.Expose.privateKey` | `extensions.Expose.hasPrivateKey` |
| `extensions.Expose.accessToken` | `extensions.Expose.hasAccessToken` |

Part of the `GET {server_address}/api/v2/settings` response when an SMTP password is stored:

```json
{
  "printers": {
    "email": {
      "credentials": {
        "userName": "edoklad@ninedigit.sk",
        "password": null,
        "hasPassword": true
      }
    }
  }
}
```

## Saving the whole settings

`PUT` **replaces the whole settings**. Sections and values you do not send get their defaults
(`authentication.requireHttps` is on by default when HTTPS is on). A body with a single
section would, for example, switch the login off and remove the users. The secrets `GET` does not return are kept
according to their flag (see below), but only in the sections you send. A section left out gets its default along
with its secrets: without `extensions.Expose` the Expose settings are removed with their key and token, without
`hosting` the HTTPS certificate. The SMTP server address `printers.email.host` and the sender address
`printers.email.addressFrom` are required: they have no meaningful default, so settings without them (or without the
`printers.email` section) are refused. Version 1 also keeps the values an older client may not know (see the
[comparison](#comparison-with-version-1)).

The procedure is therefore always the same: load the settings with `GET`, change what you need and send them back
as a whole.

```http
PUT {server_address}/api/v2/settings
Content-Type: application/json
```

The response contains the saved settings in the same form as `GET`.

## Secrets in version 2

In version 2 the flag means the same as in the `GET` response: whether the secret is stored. By sending the flag you
keep or change that state:

| Value | `has…` flag | Result |
|---|---|---|
| a new value | `true` or not sent | the new value is stored |
| a new value | `false` | refused (`400`), the flag removes the value |
| `null` | `true` or not sent | the stored value is kept (if none is stored, nothing happens) |
| `null` | `false` | the stored value is removed (if none is stored, nothing happens) |
| `""` or only spaces | any | refused (`400`) |

The `GET` response can therefore be sent back unchanged and nothing changes. The flag takes precedence: if you add a
new password after `GET` that was not stored yet (`GET` returned `false`), change the flag to `true` or leave it out.
With `false` the request is refused and nothing is saved.

A new SMTP password:

```json
{
  "printers": {
    "email": {
      "credentials": {
        "userName": "edoklad@ninedigit.sk",
        "password": "Password123!"
      }
    }
  }
}
```

Removing the access token of Expose switched off (the other Expose settings stay as they were in `GET`):

```jsonc
{
  "extensions": {
    "Expose": {
      /* … the other Expose settings from GET */
      "hasAccessToken": false
    }
  }
}
```

Some combinations would leave the settings invalid, so eKasa refuses them:

- **The SMTP server and the sender address are required** (`printers.email.host`, `printers.email.addressFrom`), even
  if you do not use the e-mail printer. A new installation has the placeholders `smtp.example.com` and
  `mail@example.com` in them, which `GET` returns and which can be sent back.
- **A user must have a password.** A user's password cannot be removed (`hasPassword: false`). Remove the user by
  leaving them out of the `authentication.users` list. A new user must have a password.
- **An SMTP password needs a user name.** A user name without a password is stored, `GET` returns
  `hasPassword: false` for it, and with the sign-in switched on (`printers.email.authenticate`) eKasa signs in
  with an empty password. Remove the credentials by sending `userName: null` and `hasPassword: false`. With the
  sign-in switched on and no credentials, eKasa sends without signing in.
- **A certificate** that cannot be loaded without a password cannot be saved without one.
- **A certificate password needs a certificate.** Remove the certificate along with its password (`hasPfx: false`
  and `hasPassword: false`); `hasPfx: false` alone while a password is stored is refused.
- **An extension switched on must have everything it needs.** While Expose is on, its key and token cannot be
  removed, and Expose cannot be switched on without them. The extension settings are checked only while it is on:
  for example the format of the token of Expose switched off is checked only when it is switched on.

An extension switched on is checked at every save, also when the save does not change it. If an older version saved
it without these checks (for example Expose without its token), the save is refused until you fix the extension or
switch it off. Version 1 checks, as 9.0.7 did, only an extension the save changes.

## Changing single values

`PATCH` (version 2 only) changes only what you send. The rest of the settings stays saved, so you do not have to call
`GET` first. The body is a [JSON Merge Patch (RFC 7396)](https://www.rfc-editor.org/rfc/rfc7396):

```http
PATCH {server_address}/api/v2/settings
Content-Type: application/merge-patch+json
```

```json
{ "dateTimeProvider": { "validateTime": false } }
```

eKasa also accepts `Content-Type: application/json`. Merge rules:

- objects are merged, arrays (`cors.allowOrigins`, `authentication.users`) are replaced as a whole,
- `null` resets a value to its default, a whole section as well (for example `"hosting": null`); the required
  `printers.email.host` and `printers.email.addressFrom` have no default, so their `null` is refused, as leaving
  them out of `PUT` is,
- a secret the patch does not mention stays stored,
- a secret without its flag follows its value like any other value: a new value sets it, `null` resets it to its
  default, that is removes it; you do not have to know whether one is stored,
- a flag the patch has applies as in `PUT`: `"has…": false` removes the secret, with a new value it is refused,
  `null` with `"has…": true` keeps it,
- the patch replaces an array as a whole, so the passwords in `authentication.users` follow the rules of `PUT`: a
  user without a password or with `null` keeps theirs,
- names are case-insensitive.

Removing the SMTP password (the user name stays):

```json
{ "printers": { "email": { "credentials": { "password": null } } } }
```

Adding a user: the array is replaced as a whole, so send the existing users too. Without a password they keep theirs:

```json
{
  "authentication": {
    "users": [
      { "userName": "admin" },
      { "userName": "cashier", "password": "Password123!" }
    ]
  }
}
```

A value of a wrong type and an invalid value are refused, and nothing is saved. A name the settings do not have is
ignored, as in `PUT`: a newer client still saves on an older eKasa version what that version knows. The response
contains the saved settings; check in it that the change applied (for example after a typo in a name).

## When saving fails

`PUT` and `PATCH` refuse invalid settings with status `400` and code
[`-900`](https://ekasa.ninedigit.sk/en/docs/rest-api/error-codes#-900-validationerror). Nothing is saved. The `errors` field holds an error for
each wrong value:

```json
{
  "title": "Validácia zlyhala",
  "status": 400,
  "code": -900,
  "errors": {
    "Authentication.Users[0].Password": [
      "Používateľ musí mať heslo, nedá sa odstrániť. Používateľa odstránite tak, že ho vynecháte zo zoznamu."
    ]
  }
}
```

The messages are in Slovak and their wording may change between versions. Recognize the error by code `-900` and
the value name in `errors`.

## Comparison with version 1

`GET` returns the same in both versions. What differs is the meaning of the `has…` flags when saving: in version 1
the flag does not say whether the secret is stored, but **whether you are sending it**.

| Value | `has…` flag | Version 1 | Version 2 |
|---|---|---|---|
| a new value | `true` or not sent | stored | stored |
| a new value | `false` | refused (`400`) | refused (`400`) |
| `null` | not sent | kept | kept |
| `null` | `true` | refused (`400`) | kept |
| `null` | `false` | kept | **removed** |

The certificate (`hosting.https.certificates`) does not follow these rules in version 1, because it came to the
settings only in version 9.1.0: only its value decides (`null` keeps it, a new value replaces it).

Other rules of version 1 stay as they were in version 9.0.7:

- An SMTP server address (`printers.email.host`) or sender address (`printers.email.addressFrom`) not sent, or the
  whole section not sent, gets the placeholder `smtp.example.com` or `mail@example.com`. Version 2 requires them.
- An empty string `""` without a flag: the user password stays stored, the SMTP password is removed and the user
  name stays. Version 2 always refuses `""`.
- The flags of the secrets of an extension switched **off** are not checked. A flag contradicting the value keeps
  the stored secret. `""` without a flag or with `true` removes it.

In practice this means:

| | Version 1 | Version 2 |
|---|---|---|
| send the `GET` response back unchanged | refused if passwords or keys are stored | yes |
| remove one password, the certificate or an Expose key | no | yes, `has…: false` |
| change one value without the whole settings (`PATCH`) | no | yes |
| a missing `hosting` section, `authentication.requireHttps` or `hosting.enableMdns` | kept (an older client may not know them) | the default, as every value not sent |

In version 1 the `GET` response therefore has to be adjusted before sending: set all `has…` flags to `false`, and to
`true` only for a secret whose new value you send. The procedure is described in
[Migration to version 8](https://ekasa.ninedigit.sk/en/docs/migration/v8#5-changes-in-apiv1settings).

### Moving from version 1 to version 2

1. Change the address from `/api/v1/settings` to `/api/v2/settings`.
2. After `GET`, no longer rewrite the `has…` flags to `false`. In version 2, `false` would remove the stored secret.
3. Send a new value with the flag `true` or without it, as before.
4. Remove a secret by sending `null` instead of the value and the flag `false`.
5. Send the whole settings from `GET`, the `hosting` section included. In version 2 a missing `hosting` section,
   `authentication.requireHttps` or `hosting.enableMdns` gets its default.

- [HTTP API reference](https://ekasa.ninedigit.sk/en/docs/rest-api/reference#tag/settings/PATCH/api/v2/settings): All settings fields and responses.
- [Connection and security](https://ekasa.ninedigit.sk/en/docs/security): Login, CORS and HTTPS, which are set here.
- [Error codes](https://ekasa.ninedigit.sk/en/docs/rest-api/error-codes#-900-validationerror): Code -900 and other HTTP API errors.
