Home About us Support Partners SIGN UP NOW

CrystalMQ Broker REST API Documentation

MQTT REST API Documentation

Introduction

While building an IoT application, there is a need to control and manage the edge devices from the manager application. So, our CrystalMQ Broker has been integrated with REST API to help you control your edge devices via the REST API. You can send messages, add new authentication keys, get client details, and do more via the API. This documentation provides a complete guide for the developers to build IoT applications using the RESTful API of our CrystalMQ Broker. Refer our MQTT Broker integration using REST API blog to know more.


Basics
For Post

Set raw data in body.

All the parameter fields should be given as a parameter.

Once the authentication credentials you enter are verified, you will receive response. Then create authorization and set bearer token. And for all API commands except login, it is necessary to set authorization.

For Get

Get Method sends request to the server to collect data. Set the following parameters as directed.

  • Key and value in header type.
  • Content-type and authorization in key field.
  • Value for content-type as application/json and the bearer token value.
Get MQTT Credentials

Access a list of MQTT usernames along with the clients that have been granted access to each username. This helps in monitoring and managing credential usage.

Method:

GET

/crystalmq/api/v1/mqtt-credentials

Example Request

curl --location '//crystalmq/api/v1/mqtt-credentials' \
--header 'Accept: application/json' \
--header 'Authorization: Bearer '

Response:

On Success:

{
"status": "success",
"data": {
"credentials": [
{
"mqtt_username": "mqtt_user1",
"clients": "client1,client2"
}
]
},
"message": "MQTT Credentials and the corresponding Client Ids"
}

Unauthorized

{
"status": "error",
"message": "Unauthorized User"
}

Internal Server Error

{
"status": "error",
"message": "Failed"
}

Add/Update MQTT Credentials

Add new MQTT credentials or update existing ones along with the associated list of authorized clients. This operation allows you to manage and configure username-client mappings, ensuring proper access control and credential management.

Method:

Post

/crystalmq/api/v1/mqtt-credentials

Example Request
{
"mqtt_username": "mqtt_user1",
"mqtt_password": "mqtt_pass1",
"clients": "client1,client.,dev*"
}
Response:
On Success:

{
"status": "success",
"message": "Added MQTT Credentials - MQTT Username: mqtt_user1 for Client Id List: client1,client.,dev*"
}

Bad request - missing or invalid parameters

{
"status": "error",
"message": "Missing required parameter: mqtt_username / mqtt_password / clients"
}

Unauthorized

{
"status": "error",
"message": "Unauthorized User"
}

Internal Server Error

{
"status": "error",
"message": "Failed"
}

Delete MQTT Credentials

Deletes all the created MQTT Credentials. This action clears all username-client mappings, effectively revoking access and ensuring that no credentials remain active.

Method:

Delete

/crystalmq/api/v1/mqtt-credentials

Response:

On Success

{
"status": "success",
"message": "Deleted MQTT Username: mqtt_user1"
}

Missing or invalid parameters

{
"status": "error",
"message": "Missing required parameter: mqtt_username"
}

Unauthorized

{
"status": "error",
"message": "Unauthorized User"
}

Internal Server Error

{
"status": "error",
"message": "Failed"
}

ACL

Get Topic Access Control List

Obtain the Topic Access Control List (ACL) for specified Client IDs and/or MQTT Usernames. This provides detailed information on which topics each client or user is permitted to access, helping you manage topic-level permissions effectively.

Method:
GET

/crystalmq/api/v1/acl

Example Request
curl --location '//crystalmq/api/v1/acl?clientid=%3Cstring%3E&mqtt_username=%3Cstring%3E' \
--header 'Accept: application/json' \
--header 'Authorization: Bearer <token>'

Response:

On Success:

{
"status": "success",
"data": {
"acl": {
"clients": {
"additionalProp1": {
"publish": {
"access": "allow",
"topics": [
"topic1",
"topic2"
]
},
"subscribe": {
"access": "allow",
"topics": [
"topic1",
"topic2"
]
}
},
"additionalProp2": {
"publish": {
"access": "allow",
"topics": [
"topic1",
"topic2"
]
},
"subscribe": {
"access": "allow",
"topics": [
"topic1",
"topic2"
]
}
},
"additionalProp3": {
"publish": {
"access": "allow",
"topics": [
"topic1",
"topic2"
]
},
"subscribe": {
"access": "allow",
"topics": [
"topic1",
"topic2"
]
}
}
},
"mqtt_username": {
"additionalProp1": {
"publish": {
"access": "allow",
"topics": [
"topic1",
"topic2"
]
},
"subscribe": {
"access": "allow",
"topics": [
"topic1",
"topic2"
]
}
},
"message": "Topic based Access Control List"
}

Bad request - missing or invalid parameters

{
"status": "error",
"message": "Missing required parameter: clientid / mqtt_username"
}

Unauthorized

{
"status": "error",
"message": "Unauthorized User"
}

Internal Server Error

{
"status": "error",
"message": "Failed"
}

Add/Update Topic Access Control List for a Client ID or MQTT Username

Add or update the Topic Access Control List (ACL) for a Client ID or MQTT Username. Ensure that only one of mqtt_username or clientid is included in the request body.

Method:
Post

/crystalmq/api/v1/acl

Example Value

{
"mqtt_username": "string",
"clientid": "string",
"publish_access": "allow",
"publish_topics": "#",
"subscribe_access": "allow",
"subscribe_topics": "#"
}

Response:

On Success

{
"status": "success",
"message": "Updated Access Control List for Client: clientid"
}

Bad request - Either mqtt_username OR clientid must be given; but not both

{
"status": "error",
"message": "Missing required parameter: Either mqtt_username OR clientid must be given"
}

Unauthorized - missing or invalid API token

{
"status": "error",
"message": "Unauthorized"
}

Internal Server Error

{
"status": "error",
"message": "Failed"
}

Delete Topic Access Control List for a Client ID or MQTT Username

Delete the Topic Access Control List (ACL) for a Client ID or MQTT Username. Ensure that only one, either mqtt_username or clientid, is provided in the request body.

Method:
DELETE

/crystalmq/api/v1/acl

Parameters:

Name Description
clientid
<string>
ID of the client

Response:

On Success

{
"status": "success",
"message": "Deleted Access Control List for ClientId: mqtt_client1 if the ClientId exists. MQTT Username based ACL will be applied for this Client."
}

Bad request - missing or invalid parameter

{
"status": "error",
"message": "Missing required parameter: clientid"
}

Unauthorized

{
"status": "error",
"message": "Unauthorized User"
}

Internal Server Error

{
"status": "error",
"message": "Failed"
}

Clients

Get Clients Count

Obtain the total count of clients categorized by their current status. This provides a detailed overview of active, inactive, and other client states, helping in effective monitoring and management.

Method:
Get

/crystalmq/api/v1/clients/count

Example Request

curl --location '//crystalmq/api/v1/clients/count?status=all' \
--header 'Accept: application/json' \
--header 'Authorization: Bearer <token>'

Response:

On Success

{
"status": "success",
"data": {
"active": 100,
"inactive": 50,
"all": 150
},
"message": "Number of Clients"
}

Bad requests - invalid parameters

{
"status": "error",
"message": "Invalid parameter value for status"
}

Unauthorized

{
"status": "error",
"message": "Unauthorized User"
}

Internal Server Error

{
"status": "error",
"message": "Failed"
}

Get Client Information

Provides detailed information about a particular client. This helps in monitoring and troubleshooting individual clients by providing comprehensive insights into their activity and configuration.

Method:
Get

/crystalmq/api/v1/clients/info

Example Request

curl --location '//crystalmq/api/v1/clients/info?clientid=%3Cstring%3E' \
--header 'Accept: application/json' \
--header 'Authorization: Bearer <token>'

Response:

On Success

{
"status": "success",
"data": {
"clientid": "client123",
"mqtt_version": 5,
"status": "active",
"status_timestamp": "2024-07-17T12:00:00Z",
"ip": "192.168.1.100",
"keep_alive": 60,
"clean_session": true,
"session_expiry_interval": 3600
},
"message": "Detailed information of client client123"
}

Bad requests - missing or invalid parameter

{
"status": "error",
"message": "Missing required parameter: clientid"
}

Unauthorized

{
"status": "error",
"message": "Unauthorized User"
}

Internal Server Error

{
"status": "error",
"message": "Failed"
}

Get Client Last Will

Fetch the last will information for a specific MQTT client or for all clients. This provides details on the last will message set for clients, which is used to notify other clients of an unexpected disconnection.

Method:
Get

/crystalmq/api/v1/clients/last-will

Parameters:

PARAMETER VALUE DETAIL
Client ID The ID of the client for which last will information is requested.

Example Request

curl --location '//crystalmq/api/v1/clients/last-will?clientid=%3Cstring%3E' \
--header 'Accept: application/json' \
--header 'Authorization: Bearer <token>'

Response:

When Device is Connected:

{
"status": "success",
"data": {
"client_id": {
"additionalProp1": {
"last_will": {
"topic": "last/will",
"message": "This is the last will message",
"qos": 1,
"retain": true,
"properties": {
"additionalProp1": 300,
"additionalProp2": 300,
"additionalProp3": 300
}
}
},
"additionalProp2": {
"last_will": {
"topic": "last/will",
"message": "This is the last will message",
"qos": 1,
"retain": true,
"properties": {
"additionalProp1": 300,
"additionalProp2": 300,
"additionalProp3": 300
}
}
},
"message": "Last Will of Client(s)"
}

Bad request - missing or invalid paremeter

{
"status": "error",
"message": "Failed to retrieve last will information"
}

Unauthorized

{
"status": "error",
"message": "Unauthorized User"
}

Internal Server Error

{
"status": "error",
"message": "Failed"
}

Disconnect MQTT5 Client

Disconnects a particular MQTT5 client. This action terminates the client's current session, stopping any ongoing communications and freeing up resources.

Method:
POST

/crystalmq/api/v1/clients/disconnect

Response:

On Success when limit is 5:

{
"status": "success",
"message": "Disconnected the Client: client123"
}

Bad request - missing or invalid parameter

{
"status": "error",
"message": "Missing required parameter: clientid"
}

Unauthorized

{
"status": "error",
"message": "Unauthorized User"
}

Internal Server Error

{
"status": "error",
"message": "Failed"
}

Get Client Subscriptions

Fetch the list of subscriptions for all clients or for a specific client when a client ID is provided. This allows you to view which topics each client is subscribed to, aiding in subscription management.

Method:
Get

/crystalmq/api/v1/clients/subscriptions

Parameters:

PARAMETER VALUE DETAIL
client id The ID of the client whose subscriptions are to be retrieved.

Example Request

curl --location '//crystalmq/api/v1/clients/subscriptions?clientid=%3Cstring%3E' \
--header 'Accept: application/json' \
--header 'Authorization: Bearer <token>'

Response:

On Success

{
"status": "success",
"data": {
"clients": {
"additionalProp1": {
"subscriptions": [
{
"filter": "topic/filter",
"qos": 1,
"subscription_options": {
"subscription_identifier": 1,
"user_property": "property_value",
"no_local": true,
"retain_handling": 0,
"retain_as_published": 1
}
}
]
},
"additionalProp2": {
"subscriptions": [
{
"filter": "topic/filter",
"qos": 1,
"subscription_options": {
"subscription_identifier": 1,
"user_property": "property_value",
"no_local": true,
"retain_handling": 0,
"retain_as_published": 1
}
}
]
},
"message": "Client subscriptions"
}

Bad request - missing or invalid parameter

{
"status": "error",
"message": "Missing required parameter: clientid"
}

Unauthorized

{
"status": "error",
"message": "Unauthorized User"
}

Internal Server Error

{
"status": "error",
"message": "Failed"
}

Get Clients

Obtain a comprehensive list of clients categorized by their current status. This retrieval provides insight into client activity and status, helping you monitor and manage client connections effectively.

Method:
Get

/crystalmq/api/v1/clients

Response:

On Success

{
"status": "success",
"data": {
"active": [
"client1",
"client2"
],
"inactive": [
"client3",
"client4"
]
},
"message": "Client Ids"
}

Bad request - invalid parameter

{
"status": "error",
"message": "Invalid parameter value for status"
}

Unauthorized

{
"status": "error",
"message": "Unauthorized User"
}

Internal Server Error

{
"status": "error",
"message": "Failed"
}

Topics

Get Clients Subscribed to Topic

Fetch a list of clients currently subscribed to a specific MQTT topic. This provides insights into which clients are receiving messages from the topic.

Method:
Get

/crystalmq/api/v1/topics/subscriptions

Example Request

curl --location '//crystalmq/api/v1/topics/subscriptions?topic=%3Cstring%3E' \
--header 'Accept: application/json' \
--header 'Authorization: Bearer <token>'

Response:

On Success

{
"status": "success",
"data": {
"clients": {
"additionalProp1": {
"filter": "topic/filter",
"qos": 1,
"subscription_options": {
"subscription_identifier": 1,
"user_property": "property_value",
"no_local": true,
"retain_handling": 0,
"retain_as_published": 1
}
},
"additionalProp2": {
"filter": "topic/filter",
"qos": 1,
"subscription_options": {
"subscription_identifier": 1,
"user_property": "property_value",
"no_local": true,
"retain_handling": 0,
"retain_as_published": 1
}
},
"message": "Clients subscribed to topic"
}

Bad requests - missing or invalid parameters

{
"status": "error",
"message": "Missing required parameter: topic"
}

Unauthorized

{
"status": "error",
"message": "Unauthorized User"
}

Internal Server Error

{
"status": "error",
"message": "Failed"
}

Get Recent Events

Access the latest events recorded by the broker, offering insights into recent activities and system interactions. This feature helps monitor current event trends and troubleshoot any issues promptly.

Method:
GET

/crystalmq/api/v1/recent-events

Example Request

curl --location '//crystalmq/api/v1/recent-events?clientid=%3Cstring%3E&topic=%3C string%3E&limit=1' \
--header 'Accept: application/json' \
--header 'Authorization: Bearer <token>'

Response:

On Success

{
"status": "success",
"data": [
{
"client": "client001",
"topic": "/devices/+/data",
"event": "Event data here",
"timestamp": "2024-07-17T14:30:00Z",
"properties": {
"qos": 1
}
}
],
"message": "Event(s) received from Client(s)"
}{
"status": "success",
"data": [
{
"client": "client001",
"topic": "/devices/+/data",
"event": "Event data here",
"timestamp": "2024-07-17T14:30:00Z",
"properties": {
"qos": 1
}
}
],
"message": "Event(s) received from Client(s)"
}

Bad request due to missing or invalid data

{
"status": "error",
"message": "Missing required parameter: topic"
}

Unauthorized

{
"status": "error",
"message": "Unauthorized"
}

Internal Server Error

{
"status": "error",
"message": "Failed"
}

Get recent commands

Fetch a list of the most recent commands sent to clients. This feature provides insights into recent command activities, helping you track and review client interactions effectively.

Method:
GET

/crystalmq/api/v1/recent-commands

Response:

On Success

{
"status": "success",
"data": [
{
"client": "client001",
"topic": "/devices/+/data",
"command": "GET",
"timestamp": "2024-07-17T14:30:00Z",
"properties": {
"qos": "1"
}
}
]
}

Bad request due to missing or invalid data

Parameters are missing:

{
"status": "error",
"message": "Missing required parameter: topic"
}

Unauthorized

{
"status": "error",
"message": "Unauthorized"
}

Internal Server Error

{
"status": "error",
"message": "Failed"
}

Send Command to Client

Send a command to a specific client by publishing it to a designated topic. This action allows you to remotely control or instruct clients based on their subscribed topics.

Method:
GET

/crystalmq/api/v1/send-command

Example Request

{
"clientid": "client123",
"topic": "/devices/+/data",
"message": "Command message",
"qos": 1,
"retain": 1
}

Response:

On Success

{
"status": "success",
"message": "Command sent to the Client: client123"
}

Bad request due to missing or invalid data

Parameters are missing:

{
"status": "error",
"message": "Missing required parameter: clientid / topic"
}

Unauthorized

{
"status": "error",
"message": "Unauthorized"
}

Internal Server Error

{
"status": "error",
"message": "Failed"
}