mqtt_client_authentication_api

MQTT Client Authentication API

This guide describes how third-party software, such as a building management system, a provisioning tool or a cloud service, can change, check and restore the credentials the Thinknx server uses to connect to an MQTT broker. No project re-upload and no server restart are needed.

Related documentation: MQTT, for configuring the MQTT client gateway, Virtual Inputs and Virtual Outputs in Thinknx Configurator. See also Integration Kit (API).

The MQTT Client Authentication API is available starting from Thinknx server firmware version 1.1.10.34.

The Thinknx server can act as an MQTT client: it connects to an external MQTT broker and exchanges values with it through the Virtual Inputs and Virtual Outputs defined in the project. The broker address and the credentials normally come from the project created with Thinknx Configurator.

This API changes only the MQTT authentication settings on a running server, leaving the rest of the configuration as it is. Typical uses:

  • rotating the broker password regularly, without reloading the project;
  • giving each installation its own broker account during commissioning;
  • switching a server between anonymous and authenticated access.
Endpoint Method Purpose
mqttclient/getauth GET or POST Reads the current authentication status and whether the client is connected.
mqttclient/setauth POST Stores new credentials (an override) and reconnects the client with them.
mqttclient/clearauth POST Deletes the override and goes back to the credentials in the project.

Credentials set through the API are saved on the server as an override. The override takes priority over the project settings and stays active:

  • Project: the credentials from the configurator project are loaded at startup.
  • Override: if an override exists, its credentials replace the project ones.
  • Persistent: the override survives server restarts and new project uploads.
  • Removal: the override only goes away when clearauth is called.

Only authentication is affected. The API changes only whether credentials are used, and the username and password. The broker address, port, TLS certificates, client ID, Virtual Inputs, Virtual Outputs and every other project setting stay as defined in the project.

  • Thinknx server firmware 1.1.10.34 or later.
  • The Integration Kit API is enabled (licensed) on the server. If it is not, the API is not available and the server log shows “Integration Kit API - License needed!”.
  • The project contains at least one MQTT Client gateway. The API changes existing MQTT clients; it cannot create new ones.
  • An enabled server user, used for HTTP authentication. All calls need it.
  • The password of the service user of the server. setauth and clearauth need it as an extra check.
  • The calling system must reach the Thinknx server directly on its HTTPS web port. The API is not available through the Thinknx cloud.

All endpoints are relative to the Integration Kit base path, over HTTPS:

https://<server-address>/api/V1/<endpoint>?auth=basic

Example: https://192.168.1.50/api/V1/mqttclient/getauth?auth=basic&index=1

The API is served only on this direct path: it is not available on action.cgi, and not through the Thinknx cloud (a call arriving from the cloud is answered with HTTP 403 and {"namespace": "<endpoint>", "error": "not available through the cloud"}).

The server certificate is usually self-signed: install it in the trust store of the calling system, or pin it in the client (for example curl –cacert server.pem, or Python requests with verify=“server.pem”).

Every request must be authenticated with a Thinknx server user, using HTTP Basic. Add auth=basic to the URL and give the user and password to the HTTP client, for example in the URL itself:

https://apiuser:apipassword@192.168.1.50/api/V1/<endpoint>?auth=basic
  • With auth=basic, a request without credentials gets HTTP 401 with WWW-Authenticate: Basic realm=“Thinknx Server”. Clients that wait for this challenge (browsers, user:password@ URLs, most HTTP libraries) then send the credentials and repeat the request.
  • A client can also send the Authorization: Basic header with every request, without waiting for the challenge (for example curl -u, or Python requests with auth=(user, password)). This works with or without auth=basic.
  • HTTP Digest is not supported, and no session is kept between requests.

Without auth=basic there is no challenge. A request without credentials and without auth=basic is redirected to the login page (HTTP 303 to /login.html). Wrong user or password gives HTTP 401.

Repeated failures block the caller. The server counts failed logins per IP address and can temporarily block an address after repeated failures. Check the credentials before automating calls, and do not retry failed authentications in a tight loop.

  • Write operations (setauth, clearauth) only accept HTTP POST with a JSON object as the request body, sent as application/json or text/plain; charset=utf-8, encoded in UTF-8. Write operations sent with any other method are rejected with HTTP 405, and a POST with parameters in the URL is rejected with HTTP 400 (only auth=basic is allowed there), so credentials never end up in URLs or proxy logs.
  • Read operations (getauth) accept either GET with the index in the query string, or POST with a JSON body (the body can be left empty).
  • Field names are case-sensitive, and each field must have the JSON type shown in the parameter tables: use_credentials is a boolean, index is a number, the other fields are strings.
  • Values need no encoding beyond normal JSON: a password such as N3w+Secret&1 is sent exactly as it is. This is the same body format used by the Access Control API.

Every response body is a JSON object whose first field, namespace, is the name of the endpoint, as in all the Integration Kit APIs. Success is reported with HTTP status 200. Errors use the HTTP status codes listed in the error reference and contain an error field with a description.

A project can contain more than one MQTT client, each with its own broker. Every call takes an optional index parameter that says which client to work on:

Situation Result
index given The MQTT client with that index is used. If none matches, the call fails with HTTP 404.
index omitted, one MQTT client in the project That client is used.
index omitted, several MQTT clients in the project The call fails with HTTP 400 (index required).
No MQTT client in the project The call fails with HTTP 404.

Finding the index. The index is the one the MQTT Client gateway has in the configurator project. On a server with one MQTT client, call mqttclient/getauth without parameters: the index field of the response shows it.

Reads the current status. Read only.

Returns the authentication settings the MQTT client is currently using, whether they come from an override, and whether the client is connected to the broker. The password is never returned.

Name Required Description
index No Number. Index of the MQTT client. With GET it goes in the query string, with POST in the JSON body.
curl --cacert server.pem -u apiuser:apipassword "https://192.168.1.50/api/V1/mqttclient/getauth?index=1"
{"namespace" : "mqttclient/getauth", "index" : 1, "use_credentials" : true,
 "username" : "plant-042", "override_active" : true, "connected" : true}

Sets new credentials. Writes the override and reconnects the client.

Saves new authentication settings as a persistent override and reconnects the MQTT client with them. Calling setauth again replaces the previous override.

Name Required Description
service_password Yes String. Password of the service user of the server.
use_credentials Yes Boolean. true: connect to the broker with username and password. false: connect without credentials (anonymous).
username When use_credentials is true String. Broker username. Ignored when use_credentials is false.
password No String. Broker password. Ignored when use_credentials is false. Leave it out if the broker account has no password.
index No Number. Index of the MQTT client.
curl --cacert server.pem -u apiuser:apipassword -X POST "https://192.168.1.50/api/V1/mqttclient/setauth" \
     -H "Content-Type: application/json" \
     --data '{"service_password": "MyServicePwd", "use_credentials": true,
              "username": "plant-042", "password": "N3w+Secret!", "index": 1}'
{"namespace" : "mqttclient/setauth", "index" : 1, "use_credentials" : true,
 "username" : "plant-042", "override_active" : true, "connected" : false}

connected is false right after the call. The response is sent as soon as the new credentials have been saved. The client then disconnects and reconnects in the background, usually within a few seconds. Call getauth afterwards to check that connected becomes true. If it stays false, the broker probably rejected the new credentials. See the troubleshooting section.

use_credentials false is still an override. Sending “use_credentials”: false stores an override that forces anonymous access, even if the project defines credentials. To go back to the project settings, use clearauth instead.

Restores the project credentials. Removes the override and reconnects the client.

Deletes the override and reconnects the MQTT client with the credentials in the project. This is the only way to remove an override. Calling it when no override exists is harmless: the client simply reconnects with the project settings.

Name Required Description
service_password Yes String. Password of the service user of the server.
index No Number. Index of the MQTT client.
curl --cacert server.pem -u apiuser:apipassword -X POST "https://192.168.1.50/api/V1/mqttclient/clearauth" \
     -H "Content-Type: application/json" \
     --data '{"service_password": "MyServicePwd", "index": 1}'
{"namespace" : "mqttclient/clearauth", "index" : 1, "use_credentials" : true,
 "username" : "project-user", "override_active" : false, "connected" : false}

All three endpoints return the same status object when they succeed:

Field Type Description
namespace string Name of the endpoint that produced the response.
index number Index of the MQTT client.
use_credentials boolean true if the client authenticates with username and password, false if it connects anonymously.
username string Username currently in use. Empty when use_credentials is false.
override_active boolean true if the settings come from an API override, false if they come from the project.
connected boolean true if the client is connected to the broker right now.

When a call fails, the response contains namespace and error:

{"namespace" : "mqttclient/setauth", "error" : "wrong password"}
HTTP error Cause and solution
400 the body must be a JSON object The POST body is empty or is not a JSON object.
400 service_password missing The service_password field was not sent, or is not a string.
400 use_credentials must be true or false The field is missing or is not a JSON boolean (1, “true” and null are refused).
400 username is required when use_credentials is true Send a non-empty username, or set use_credentials to false.
400 username and password must be strings username or password has another JSON type. Send them as strings.
400 index must be a number The index field of the body is a string or another type. Send it as a JSON number.
400 multiple mqtt clients configured: index required The project has several MQTT clients. Add the index parameter.
400 parameters must be sent in the request body A setauth or clearauth request has parameters in the URL other than auth=basic. Send them only in the POST body.
303 (redirect to the login page, no JSON body) No credentials were sent and the URL has no auth=basic. Add auth=basic to the URL, or send the Authorization: Basic header with every request.
401 (no JSON body) Wrong server user name or password.
403 wrong password The service_password is wrong.
403 not available through the cloud The call was sent through the Thinknx cloud. Call the server directly.
404 mqtt client with index N not found No MQTT client has that index. Check the project.
404 no mqtt client configured The loaded project has no MQTT client.
405 POST required setauth and clearauth only accept POST.
500 cannot write override file: … The server could not save the override (for example, the storage is full or read-only). Nothing has changed: the previous settings are still active.
500 cannot delete override file: … The server could not remove the override. The override is still active.
  1. The server checks the request and saves the new settings, writing them safely so that an interruption cannot leave a corrupted override behind.
  2. The response is returned.
  3. The MQTT client closes its current connection to the broker.
  4. A new connection is opened with the new settings, usually within a few seconds, and the client subscribes again to all the topics of the Virtual Inputs.

While the client is reconnecting, Virtual Output values due to be published are skipped; they are not queued. Outputs set to publish on a fixed interval resume at their next interval. Messages the broker sends during the reconnection follow the broker's own session and retain rules.

Event Effect on the override
Server or service restart Kept. Loaded right after the project.
New project uploaded from the configurator Kept. Still takes priority over the credentials in the new project.
setauth called again Replaced by the new values.
clearauth called Removed. The project credentials are used again.

Each MQTT client has its own override, so changing one client does not affect the others.

import time, requests
 
BASE = "https://192.168.1.50/api/V1/"
AUTH = ("apiuser", "apipassword")           # Thinknx server user
SERVICE_PWD = "MyServicePwd"
 
# 1. Set the new broker credentials (json= sends a JSON body)
r = requests.post(BASE + "mqttclient/setauth", auth=AUTH, timeout=10,
                  verify="server.pem", json={
    "service_password": SERVICE_PWD,
    "use_credentials": True,
    "username": "plant-042",
    "password": "N3w+Secret!",
    "index": 1,
})
if r.status_code != 200:
    raise SystemExit(r.json().get("error"))
 
# 2. Wait for the client to reconnect
for _ in range(15):
    time.sleep(2)
    status = requests.get(BASE + "mqttclient/getauth", auth=AUTH,
                          params={"index": 1}, timeout=10,
                          verify="server.pem").json()
    if status["connected"]:
        print("Connected with", status["username"])
        break
else:
    print("Not connected: check the credentials on the broker")
$base    = "https://192.168.1.50/api/V1/"
$pair    = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes("apiuser:apipassword"))
$headers = @{ Authorization = "Basic $pair" }
 
# Set new credentials (the hashtable is converted to a JSON body)
$body = @{
    service_password = "MyServicePwd"
    use_credentials  = $true
    username         = "plant-042"
    password         = "N3w+Secret!"
    index            = 1
} | ConvertTo-Json
Invoke-RestMethod -Method Post -Uri ($base + "mqttclient/setauth") -Headers $headers `
    -ContentType "application/json; charset=utf-8" -Body ([Text.Encoding]::UTF8.GetBytes($body))
 
# Read the status
Invoke-RestMethod -Uri ($base + "mqttclient/getauth?index=1") -Headers $headers
 
# Restore the project credentials
$body = @{ service_password = "MyServicePwd"; index = 1 } | ConvertTo-Json
Invoke-RestMethod -Method Post -Uri ($base + "mqttclient/clearauth") -Headers $headers `
    -ContentType "application/json; charset=utf-8" -Body ([Text.Encoding]::UTF8.GetBytes($body))
POST /api/V1/mqttclient/setauth HTTP/1.1
Host: 192.168.1.50
Authorization: Basic YXBpdXNlcjphcGlwYXNzd29yZA==
Content-Type: application/json
Content-Length: 125

{"service_password": "MyServicePwd", "use_credentials": true, "username": "plant-042", "password": "N3w+Secret!", "index": 1}
  • Use HTTPS and verify the certificate. The HTTP Basic credentials, the service password and the broker password travel in every request. Trust or pin the server certificate instead of turning verification off, and do not expose the server directly to the internet.
  • Use a dedicated server user for API access, with a strong password, so it can be disabled without affecting other users.
  • Protect the service password. It allows administrative actions on the server. Store it in a secrets vault on the integration side, never in scripts kept in version control.
  • Do not log the request bodies of setauth in the integration system.
  • Restrict network access to the web port of the server to the systems that need it (firewall, VPN, management VLAN).
  • The broker password is kept on the internal storage of the Thinknx server, in the same way as the credentials in the project. Protect physical and administrative access to the server.
Symptom What to check
connected stays false after setauth The broker is rejecting the new credentials, or the account lacks the permissions it needs. Check the broker log and the topic permissions of the account. To go back to the working project settings, call clearauth.
connected is false even with the project credentials The problem is not the credentials: check the broker address, port, TLS settings and network connectivity in the project.
Every request is redirected to the login page (HTTP 303) The credentials are not sent with the request. Add auth=basic to the URL so the server asks for them, or send the Authorization: Basic header every time.
Every request returns HTTP 401 Wrong server user or password, or the IP address of the caller has been temporarily blocked after repeated failures. Wait, or ask the administrator to remove the block from the web access settings of the server.
The API does not answer or returns a web page Check the URL (/api/V1/ is case-sensitive), the firmware version and that the Integration Kit API is licensed.
HTTP 400 with the body must be a JSON object The request was sent as a form (key=value&…) or with an empty body. Send a JSON object.
New credentials are lost after a project upload They are not: the override survives uploads. Check override_active with getauth. If it is false, clearauth was called.
Endpoint Method Parameters Needs service password
mqttclient/getauth GET or POST index (optional) No
mqttclient/setauth POST service_password, use_credentials, username (if use_credentials), password (optional), index (optional) Yes
mqttclient/clearauth POST service_password, index (optional) Yes
URL:             https://<server>/api/V1/<endpoint>?auth=basic
Authentication:  Authorization: Basic base64(user:password)
Body (POST):     Content-Type: application/json
                 {"service_password": "...", "index": 1}

Typical rotation flow:
  setauth, wait 2 to 5 s, getauth
  connected = true: done. Otherwise: clearauth
  • mqtt_client_authentication_api.txt
  • Last modified: 2026/11/30 16:36
  • by francesco