# REST API
EMQX exposes an HTTP management API designed following OpenAPI (Swagger) 3.0 specification.
After EMQX is started, you can visit http://localhost:18083/api-docs/index.html (opens new window) to view the API document, and also execute the management APIs from this UI.
The section will introduce how to work with EMQX REST API.
# Basic Path
EMQX has version control on the REST API, all API paths from EMQX 5.0.0 start with /api/v5
.
# Authentication
EMQX's REST API uses HTTP Basic Authentication (opens new window).
To create an API key, you can click System -> API Key page on the left navigation menu of the Dashboard, Please refer to Dashboard - API Keys.
You can use the generated API Key and Secret Key as the username and password for Basic authentication:
# HTTP Headers
Unless otherwise specified, most API requests require the Accept
header to be set to application/json
, and then the response will be returned in JSON format.
# HTTP Response Status Code
EMQX follows the HTTP Response Status Code (opens new window) standard, and the possible status codes are as follows:
Codes | Description |
---|---|
200 | Request successfully, and the returned JSON data will provide more details |
201 | Created successfully, and the new object will be returned in the Body |
204 | Request successfully. Usually used for delete and update operations, and the returned Body will be empty |
400 | Bad Request. Usually request body or parameter error |
401 | Unauthorized. API key expires or does not exist. |
403 | Forbidden. Check if the object is in use or has dependency constraints. |
404 | Not Found. You can refer to the message field in the Body to check the reason |
409 | Conflict. The object already exists or the number limit is exceeded |
500 | Internal Server Error. Check the reason in the Body and logs |
# Error Codes
Besides the HTTP response status codes, EMQX also defines a list of error codes to identify specific errors.
When an error happens, the error code is returned in JSON format by the Body:
# GET /clients/foo
{
"code": "RESOURCE_NOT_FOUND",
"reason": "Client id not found"
}
2
3
4
5
6
Error Codes | Description |
---|---|
WRONG_USERNAME_OR_PWD | Wrong username or password |
WRONG_USERNAME_OR_PWD_OR_API_KEY_OR_API_SECRET | Wrong username & password or key & secret |
BAD_REQUEST | Request parameters not legal |
NOT_MATCH | Conditions not matched |
ALREADY_EXISTS | Resources already exist |
BAD_CONFIG_SCHEMA | Configuration data not legal |
BAD_LISTENER_ID | Bad listener ID |
BAD_NODE_NAME | Bad Node Name |
BAD_RPC | RPC Failed. Check the cluster status and the requested node status |
BAD_TOPIC | Topic syntax error, topic needs to comply with the MQTT protocol standard |
EXCEED_LIMIT | Resources to be created exceed the maximum limit or minimum limit |
INVALID_PARAMETER | Request parameters not legal and exceed the boundary value |
CONFLICT | Conflicting request resources |
NO_DEFAULT_VALUE | Request parameters do not use default values |
DEPENDENCY_EXISTS | Resource depends on other resources |
MESSAGE_ID_SCHEMA_ERROR | Message ID parsing error |
INVALID_ID | Bad ID schema |
MESSAGE_ID_NOT_FOUND | Message ID does not exist |
NOT_FOUND | Resource not found or does not exist |
CLIENTID_NOT_FOUND | Client ID not found or does not exist |
CLIENT_NOT_FOUND | Client not found or does not exist(usually not an MQTT client) |
RESOURCE_NOT_FOUND | Resource not found |
TOPIC_NOT_FOUND | Topic not found |
USER_NOT_FOUND | User not found |
INTERNAL_ERROR | Server inter error |
SERVICE_UNAVAILABLE | Service unavailable |
SOURCE_ERROR | Source error |
UPDATE_FAILED | Update fails |
REST_FAILED | Reset source or configuration fails |
CLIENT_NOT_RESPONSE | Client not responding |