How to access the API interface provided by EMQX

Reference

Official website HTTP API HTTP protocol

Create an application and specify the authorization code

Log in to the text management terminal of EMQX. If no changes have been made, the address of the management terminal should be port 18083 of the server IP. After successfully logging in to the WEB management terminal, click "General" and "Application" in turn. We first create a new APP, the ID and name of the APP can be entered at will, the status selection allows access, and the expiration date is selected according to actual needs. After the creation is successful, delete the EMQX default application, otherwise there will be security risks. We also need to comment out the default APP account and key configuration in /etc/emqx/plugins/emqx_management.conf, otherwise the default APP will be recreated after restarting the EMQX service. Then we go back to the application list page and click View in the operation to view the application we just created. In the detailed information, we can see the APP key.

Test access API interface

After we get the APP key, we can use HTTP requests to access the API interface. It is important to note that we need to add authorization information in the Header. For details, please refer to the following example. For definitions of other interfaces, please refer to the official reference at the top of the article. In the following two paragraphs of examples, the first paragraph is a direct CURL request in the Linux command line, and the second paragraph is a request using PHP code.

            curl -i --basic -u app_id:app_key-X GET "http://localhost:8081/api/v4/nodes"
        
            $app_id = 'app_id';
$app_key = 'app_key';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Authorization: Basic '.base64_encode($app_id.':'.$app_key)]);
$result = curl_exec($ch);
curl_close($ch);
echo $result;