Stay tuned, we are relaunching the VAPIX library soon!

MQTT client API

Description

The MQTT client API provides you with examples and specifications that makes it possible to directly control the MQTT (Message Queuing Telemetry Transport) messaging on your Axis device. This includes configuring the device to act as an MQTT client, which can then be connected to an MQTT broker to handle message exchanges.

For more information about MQTT brokers and how to connect them to Axis devices, see MQTT.

Model

The API implements client.cgi as its communications interface and supports the following methods:

MethodDescription
configureClientConfigure the MQTT client.
activateClientActivate the MQTT client.
deactivateClientDeactivate the MQTT client.
getClientStatusReturn the client status.
getSupportedVersionsRetrieve API versions supported by the product.

Identification

API Discovery

id=mqtt-client

Common examples

MQTT client management

Use this example to exchange messages and data over MQTT. MQTT is based on two entities: the client and the broker. The client is able to send and receive messages, while the broker is responsible for routing said messages between different clients.

Configure the MQTT client

The first step to enable MQTT messaging is to configure the MQTT client, which includes all aspects such as the broker, security, protocol settings, last will testament, announcement, etc. To configure the client, a configureClient request should be sent.

1. Configure the MQTT client.

http://<servername>/axis-cgi/mqtt/client.cgi
JSON input parameters
{
  "apiVersion": "1.0",
  "context": "some context",
  "method": "configureClient",
  "params": {
    "server": {
      "protocol": "ssl",
      "host": "somebroker.somewhere.com",
      "port": 8883
    },

    "username": "user_1",
    "password": "password_1",
    "keepExistingPassword": false,
    "clientId": "unique_id_on_broker",
    "keepAliveInterval": 20,
    "connectTimeout": 30,
    "cleanSession": true,
    "autoReconnect": true,
    "deviceTopicPrefix": "some/topic/prefix",

    "lastWillTestament": {
      "useDefault": false,
      "topic": "Camera_1/ConnectionStatus",
      "message": "Connection Lost",
      "retain": true,
      "qos": 1
    },

    "connectMessage": {
      "useDefault": false,
      "topic": "Camera_1/ConnectionStatus",
      "message": "Connected",
      "retain": true,
      "qos": 1
    },

    "disconnectMessage": {
      "useDefault": false,
      "topic": "Camera_1/ConnectionStatus",
      "message": "Disconnected",
      "retain": true,
      "qos": 1
    },

    "ssl": {
      "validateServerCert": true
    }
  }
}

2. Parse the JSON response

Successful response
{
  "apiVersion": "1.0",
  "context": "some context",
  "method": "configureClient",
  "data": {
  }
}
Failed response
{
  "apiVersion": "1.0",
  "context": "some context",
  "method": "configureClient",
  "error": {
    "code": 1100,
    "message": "Internal Error"
  }
}

Activate the MQTT client

The client have two states: active and inactive. An active client is either trying to connect, or has already connected to a configured broker, while an inactive client does not have any effect, nor connection to the broker. To activate the client, an activateClient request should be sent. Please note that an active client remains active even after a reboot.

1. Activate the MQTT client.

http://<servername>/axis-cgi/mqtt/client.cgi
JSON input parameters
{
  "apiVersion": "1.0",
  "context": "some context",
  "method": "activateClient",
  "params": {
  }
}

2. Parse the JSON response.

Successful response
{
  "apiVersion": "1.0",
  "context": "come context",
  "method": "activateClient",
  "data": {
  }
}
Error response
{
  "apiVersion": "1.0",
  "context": "some context",
  "method": "activateClient",
  "error": {
    "code": 1100,
    "message": "Internal Error"
  }
}

Deactivate the MQTT client

An activated MQTT can be turned off by sending a deactivation request, which will terminate the connection with the broker.

1. Deactivate the MQTT client instance.

http://<servername>/axis-cgi/mqtt/client.cgi
JSON input parameters
{
  "apiVersion": "1.0",
  "context": "some context",
  "method": "deactivateClient",
  "params": {
  }
}

2. Parse the JSON response.

Successful response
{
  "apiVersion": "1.0",
  "context": "some context",
  "method": "deactivateClient",
  "data": {
  }
}
Error response
{
  "apiVersion": "1.0",
  "context": "somce context",
  "method": "deactivateClient",
  "error": {
    "code": 1100,
    "message": "Internal Error"
  }
}

Get the MQTT client status

The status of an MQTT client can be obtained by making a status request, where the response will contain the status and configuration.

1. Request the status of the MQTT client.

http://<servername>/axis-cgi/mqtt/client.cgi
JSON input parameters
{
  "apiVersion": "1.0",
  "context": "some context",
  "method": "getClientStatus",
  "params": {
  }
}

2. Parse the JSON response.

{
  "apiVersion": "1.0",
  "context": "some context",
  "method": "getClientStatus",
  "data": {
    "status": {
      "state": "inactive",
      "connectionStatus": "disconnected"
    },
    "config": {
      "server": {
        "protocol": "ssl",
        "host": "somebroker.somewhere.com",
        "port": 8883
      },
      "username": "user_1",
      "password": "*****",
      "clientId": "unique_id_on_broker",
      "keepAliveInterval": 20,
      "connectTimeout": 30,
      "cleanSession": true,
      "autoReconnect": true,
      "deviceTopicPrefix": "some/topic/prefix",
      "lastWillTestament": {
        "useDefault": false,
        "topic": "Camera_1/ConnectionStatus",
        "message": "Connection Lost",
        "retain": true,
        "qos": 1
      },
      "connectMessage": {
        "useDefault": false,
        "topic": "Camera_1/ConnectionStatus",
        "message": "Connected",
        "retain": true,
        "qos": 1
      },
      "disconnectMessage": {
        "useDefault": false,
        "topic": "Camera_1/ConnectionStatus",
        "message": "Disconnected",
        "retain": true,
        "qos": 1
      },
      "ssl": {
        "validateServerCert": true
      }
    }
  }
}

Get supported versions

Use this example to retrieve a list of API versions supported by your device.

1. Retrieve a list of supported API versions.

http://<servername>/axis-cgi/mqtt/client.cgi
JSON input parameters
{
  "context": "some context",
  "method": "getSupportedVersions"
}

2. Parse the JSON response.

Successful response
{
  "apiVersion": "1.0",
  "context": "some context",
  "method": "getSupportedVersions",
  "data": {
    "apiVersions": ["1.0"]
  }
}
Error response
{
  "apiVersion": "1.0",
  "context": "some context",
  "method": "getSupportedVersions",
  "error": {
    "code": 1100,
    "message": "Internal error"
  }
}

API specifications

configureClient

This API method is used when you want to configure the MQTT client.

Request

Security level

Admin

Method

POST

http://<servername>/axis-cgi/mqtt/client.cgi
{
  "apiVersion": "<Major>.<Minor>",
  "context": <string>,
  "method": "configureClient",
  "params": {

    "server": {
      "protocol": <string>,
      "host": <string>,
      "port": <integer>,
      "basepath": <string>,
      "alpnProtocol": <string>
    },

    "httpProxy": <string>,
    "httpsProxy": <string>,
    "username": <string>,
    "password": <string>,
    "keepExistingPassword": <boolean>
    "clientId": <string>,
    "keepAliveInterval": <int>,
    "connectTimeout": <int>,
    "cleanSession": <boolean>,
    "autoReconnect": <boolean>,
    "deviceTopicPrefix": <string>,

    "lastWillTestament": {
      "useDefault": <boolean>,
      "topic": <string>,
      "message": <string>,
      "retain": <boolean>,
      "qos": <int>
    },

    "connectMessage": {
      "useDefault": <boolean>,
      "topic": <string>,
      "message": <string>,
      "retain": <boolean>,
      "qos": <int>
    },

    "disconnectMessage": {
      "useDefault": <boolean>,
      "topic": <string>,
      "message": <string>,
      "retain": <boolean>,
      "qos": <int>
    },
    "ssl": {
      "validateServerCert": <boolean>,
      "clientCertID": <string>,
      "CACertID": <string>
    }
  }
}
ParameterTypeDescription
apiVersionStringThe API version that should be used in the request.
contextStringThe user sets this value and the application echoes it back in the response (optional).
methodStringThe operation that should be performed.
params.serverObjectContains the address and protocol related information. See sub fields for additional information.
params.server.protocolStringContains the protocols used by params.server. Possible values are:
  • tcp: MQTT over TCP

  • ssl: MQTT over SSL

  • ws: MQTT over Websocket

  • wss: MQTT over Websocket Secure

params.server.hostStringThe Broker location, i.e. the Hostname or IP address.
params.server.portIntegerThe port that should be used (optional). Default values for the protocols are:
  • tcp: 1883

  • ssl: 8883

  • ws: 80

  • wss: 443

params.server.basepathStringThe path that should be used as a suffix for the constructed URL and will only be used for the ws and wss connections (optional). The default value is empty string.
params.server.alpnProtocolStringThe ALPN protocol that should be used if the selected protocol was ssl or wss (optional). If the string value is empty the ALPN will not be used. Default value is empty and the maximum length of the protocol is 255 bytes.
params.httpProxyStringThe HTTP proxy that should be used (optional). If the string value is empty the HTTP proxy will not be used. The maximum length of the proxy name is 255 bytes.
params.httpsProxyStringThe HTTPS proxy that should be used (optional). If the string value is empty the HTTPS proxy will not be used. The maximum length of the proxy name is 255 bytes.
params.usernameStringThe user name that should be used for authentication and authorization (optional).
params.passwordStringThe password that should be used for authentication (optional).
params.keepExistingPasswordBooleanSpecifies whether the stored password should be kept or erased. If set to true the previously stored password will be kept, while the password field will be discarded. When set to false, the given password will be used instead. (optional). The default value is false.
params.clientIdStringThe client identifier sent to the server when the client connects to it.
params.keepAliveIntervalIntegerDefines the maximum time (in seconds) that should pass without communication between the client and server. At least one message will be sent over the network by the client during each keep alive period and the interval makes it possible to detect when the server is no longer available without having to wat for the TCP/IP timeout (optional). The default value is 60.
params.connectTimeoutIntegerThe timed interval (in seconds) to allow a connect to finish (optional). The default value is 60.
params.cleanSessionBooleanThis parameter controls the behavior of both the client and the server during connection and disconnection time. When this parameters is true, the state information is discarded when the client and server change state. Setting the parameter to false means that the state information is kept.
params.autoReconnectBooleanSpecifies if the client should reconnect on an unintentional disconnect (optional). The default value is true.
params.deviceTopicPrefixStringSpecifies a prefix on MQTT topics in various scenarios, such as when you want to configure the translation of events into MQTT messages or prefix all published MQTT messages with a common prefix (optional).
The default value is axis/{device serial number}.
params.lastWillTestamentObjectContains the options related to LWT. If LWT is not required, this parameter should not be included in the request (optional).
params.lastWillTestament.useDefaultBooleanSpecifies if the default LWT should be used. If set to true, other options in this parameter will be discarded. If set to false, topic, messages, retained and qos options are required and used.
See Default LWT/Connected/Disconnected messages for default messages.
params.lastWillTestament.topicStringThe topic that should be used by LWT (optional). This field is only required if the useDefault option is set to false.
params.lastWillTestament.messageStringThe content that should be used by LWT (optional). This field is only required if the useDefault option is set to false.
params.lastWillTestament.retainBooleanThe retained option that should be used by LWT (optional). This field is only required if the useDefault option is set to false.
params.lastWillTestament.qosIntegerThe QoS option that should be used by LWT (optional). This field is only required if the useDefault option is set to false. Possible values are 0, 1, 2.
params.connectMessageObjectSpecifies if a message should be sent when a connection is established and contains options related to connect announcements (optional). If this object is not defined this message won’t be sent. For potential subfields, see lastWillTestament.
params.disconnectMessageObjectSpecifies if a message should be sent when the client is manually disconnected and contains options related to manual disconnect announcements (optional). If this object is not defined this message won’t be sent. This message should not be confused with LWT, as it is used when the connection is lost and managed by the broker. For potential subfields, see lastWillTestament.
params.sslObjectContains the options related to the SSL connection (optional). This object should only be present if the connection type is ssl or wss.
params.ssl.validateServerCertBooleanSpecifies if the server certificate shall be validated.
params.ssl.clientCertIDStringSpecifies the client certificate and key that should be used. The certificates are managed through the user interface or via ONVIF services.
params.ssl.CACertIDStringSpecifies the CA Certificate that should be used to validate the server certificate. The certificates are managed through the user interface or via ONVIF services.

Return value - Success

HTTP code

200 OK

Content-type

application/json

Response body
{
  "apiVersion":"<Major>.<Minor>",
  "context": <string>,
  "method": <string>,
  "data": {
  }
}
ParameterTypeDescription
apiVersionStringThe API version that was used in the request.
contextStringThe context that was used when the request was made.
methodStringThe operation that was performed.
dataObjectAn empty object.

Return value - Failure

HTTP code

200 OK

Content-type

application/json

Response body syntax
{
  "apiVersion": "<Major>.<Minor>",
  "context": <string>,
  "method": <string>,
  "error": {
    "code": <integer>,
    "message": <string>
  }
}
ParameterTypeDescription
apiVersionStringThe API version that was used in the request.
contextStringThe context that was used when the request was made.
methodStringThe operation that was performed.
error.codeIntegerThe code representing the error that occurred in the request.
error.messageIntegerExplains the error that occurred.

Error codes

See Error codes for a complete list of potential error codes for this API.

Default LWT/Connected/Disconnected messages

LWT message

Topic{device topic prefix}/event/connection
Qos1
RetainedYes
Payload
{
  "serialNumber": "{device serial number}",
  "connected": false,
  "description": "Connection Lost",
  "timestamp": null
}

Connected message

Topic{device topic prefix}/event/connection
Qos1
RetainedYes
Payload
{
  "serialNumber": "{device serial number}",
  "connected": true,
  "description": "Connected",
  "timestamp": "2020-09-04T11:02:55z"
}

Disconnected message

Topic{device topic prefix}/event/connection
Qos1
RetainedYes
Payload
{
  "serialNumber": "{device serial number}",
  "connected": false,
  "description": "Disconnected",
  "timestamp": "2020-09-04T11:02:55z"
}

activateClient

This API method is used when you want to activate the MQTT client. Please note that this client will remain active after a reboot is not deactivated.

Request

Security level

Admin

Method

POST

http://<servername>/axis-cgi/mqtt/client.cgi
{
  "apiVersion": "<Major>.<Minor>",
  "context": <string>,
  "method": "activateClient"
}
ParameterTypeDescription
apiVersionStringThe API version that should be used in the request.
contextStringThe user sets this value and the application echoes it back in the response (optional).
methodStringThe operation that should be performed.

Return value - Success

HTTP code

200 OK

Content-type

application/json

Response body
{
  "apiVersion": "<Major>.<Minor>",
  "context": <string>,
  "method": <string>,
  "data": {
  }
}
ParameterTypeDescription
apiVersionStringThe API version that was used in the request.
contextStringThe context that was used when the request was made.
methodStringThe operation that was performed.
dataObjectAn empty object.

Return value - Failure

HTTP code

200 OK

Content-type

application/json

Response body syntax
{
  "apiVersion": "<Major>.<Minor>",
  "context": <string>,
  "method": <string>,
  "error": {
    "code": <integer>,
    "message": <string>
  }
}
ParameterTypeDescription
apiVersionStringThe API version that was used in the request.
contextStringThe context that was used when the request was made.
methodStringThe operation that was performed.
error.codeIntegerThe code representing the error that occurred in the request.
error.messageIntegerExplains the error that occurred.

Error codes

See Error codes for a complete list of potential error codes for this API.

deactivateClient

This API method is used when you want to deactivate the MQTT client.

Request

Security level

Admin

Method

POST

http://<servername>/axis-cgi/mqtt/client.cgi
{
  "apiVersion": "<Major>.<Minor>",
  "context": <string>,
  "method": "deactivateClient"
}
ParameterTypeDescription
apiVersionStringThe API version that should be used in the request.
contextStringThe user sets this value and the application echoes it back in the response (optional).
methodStringThe operation that should be performed.

Return value - Success

HTTP code

200 OK

Content-type

application/json

Response body
{
  "apiVersion": "<Major>.<Minor>",
  "context": <string>,
  "method": <string>,
  "data": {
  }
}
ParameterTypeDescription
apiVersionStringThe API version that was used in the request.
contextStringThe context that was used when the request was made.
methodStringThe operation that was performed.
dataObjectAn empty object.

Return value - Failure

HTTP code

200 OK

Content-type

application/json

Response body syntax
{
  "apiVersion": "<Major>.<Minor>",
  "context": <string>,
  "method": <string>,
  "error": {
    "code": <integer>,
    "message": <string>
  }
}
ParameterTypeDescription
apiVersionStringThe API version that was used in the request.
contextStringThe context that was used when the request was made.
methodStringThe operation that was performed.
error.codeIntegerThe code representing the error that occurred in the request.
error.messageIntegerExplains the error that occurred.

Error codes

See Error codes for a complete list of potential error codes for this API.

getClientStatus

This API method is used when you want to retrieve the status of the MQTT client.

Request

Security level

Admin

Method

POST

http://<servername>/axis-cgi/mqtt/client.cgi
{
  "apiVersion": "<Major>.<Minor>",
  "context": <string>,
  "method": "getClientStatus"
}
ParameterTypeDescription
apiVersionStringThe API version that should be used in the request.
contextStringThe user sets this value and the application echoes it back in the response (optional).
methodStringThe operation that should be performed.

Return value - Success

HTTP code

200 OK

Content-type

application/json

Response body
{
  "apiVersion": "<Major>.<Minor>",
  "context": <string>,
  "method": "getClientStatus",
  "data": {
    "status": {
      "state": <string>,
      "connectionStatus": <string>
    },
    "config": {
      <all options used when configuring the client>
    }
  }
}
ParameterTypeDescription
apiVersionStringThe API version that was used in the request.
contextStringThe context that was used when the request was made.
methodStringThe operation that was performed.
data.status.stateStringThe current state of the client. Possible values are active and inactive.
data.status.connectionStatusStringThe current connection state of your client. Possible values are connected, disconnected.
data.configObjectContains the settings used by your client. Its structure is the same as the one found in configureClient.

Return value - Failure

HTTP code

200 OK

Content-type

application/json

Response body syntax
{
  "apiVersion": "<Major>.<Minor>",
  "context": <string>,
  "method": <string>,
  "error": {
    "code": <integer>,
    "message": <string>
  }
}
ParameterTypeDescription
apiVersionStringThe API version that was used in the request.
contextStringThe context that was used when the request was made.
methodStringThe operation that was performed.
error.codeIntegerThe code representing the error that occurred in the request.
error.messageIntegerExplains the error that occurred.

Error codes

See Error codes for a complete list of potential error codes for this API.

getSupportedVersions

This API method is used when you want to retrieve a list of supported API versions.

Request

Security level

Admin

Method

POST

http://<servername>/axis-cgi/mqtt/client.cgi
{
  "context": <string>,
  "method": "getSupportedVersions"
}
ParameterTypeDescription
contextStringThe user sets this value and the application echoes it back in the response (optional).
methodStringThe operation that should be performed.

Return value - Success

HTTP code

200 OK

Content-type

application/json

Response body syntax
{
  "context": <string>,
  "method": "getSupportedVersions",
  "data": {
    "apiVersions": ["<Major1>.<Minor1>","<Major2>.<Minor2>"]
  }
}
ParameterTypeDescription
contextStringThe context that was used when the request was made (optional).
methodStringThe operation that was performed.
data.apiVersions[]=<list of versions>StringSpecifies the list of supported versions and includes all major versions together with their highest supported minor version.
<list of versions>StringThe list of ”<Major>.<Minor>” versions e.g. [“1.4”, “2.5”].

Return value - Failure

HTTP code

200 OK

Content-type

application/json

Response body syntax
{
  "apiVersion": "<Major>.<Minor>",
  "context": <string>,
  "method": <string>,
  "error": {
    "code": <integer>,
    "message": <string>
  }
}
ParameterTypeDescription
apiVersionStringThe API version that was used in the request.
contextStringThe context that was used when the request was made.
methodStringThe operation that was performed.
error.codeIntegerThe code representing the error that occurred in the request.
error.messageIntegerExplains the error that occurred.

Error codes

See Error codes for a complete list of potential error codes for this API.

Error codes

The error codes for this API are sorted into the following ranges:

1100–1199

Generic errors that are common to many API:s and reserved for server errors, e.g. “Maximum number of configurations reached”. The cause can be seen in the server log and the problems can in some cases be solved by restarting the device.

1200–1999

API-specific server errors.

2100–2199

Generic error codes common to many API:s and reserved for client errors, e.g. “Invalid parameter”. These errors can be solved by changing the input data to the API.

2200–2999

API-specific client errors. Codes in this range may collide between different API:s.

CodeDescription
1100Internal error
2100API version not supported
2101Invalid JSON format
2102Method not supported
2103Required parameter missing or invalid
2104Invalid parameter value specified
2105Authorization failure
2106Request size too large