Updated on 2023-12-07 GMT+08:00

Application Node.js SDK

The IoT platform provides an application SDK in Node.js for developers. This topic describes how to install and configure the Node.js SDK and how to use it to call application-side APIs.

Currently, the SDK AK/SK authentication supports only the basic edition. You are advised to use token authentication for standard and enterprise editions.

Obtaining and Installing the SDK

  1. Install the Node.js development environment.

    Visit the Node.js website, and download and install the Node.js development environment.

    The Node.js SDK can be used in Node 10.16.1 and later versions.

  2. Install dependencies.

    npm install @huaweicloud/huaweicloud-sdk-core
    npm install @huaweicloud/huaweicloud-sdk-iotda

Code Sample

The following code sample shows how to use the Node.js SDK to call API Querying the Device List.

  1. Create a credential.
  2. Create and initialize an IoTDAClient instance.
  3. Instantiate a request object.
  4. Call the API for querying the device list.

    const core = require('@huaweicloud/huaweicloud-sdk-core');
    const iotda = require("@huaweicloud/huaweicloud-sdk-iotda");
    // There will be security risks if the AK/SK used for authentication is directly written into code. Encrypt the AK/SK in the configuration file or environment variables for storage;
    // In this example, the AK/SK stored in the environment variables are used. Configure the environment variables HUAWEICLOUD_SDK_AK and HUAWEICLOUD_SDK_SK in the local environment first.
    const ak = process.env.HUAWEICLOUD_SDK_AK;
    const sk = process.env.HUAWEICLOUD_SDK_SK;
    // endpoint: On the console, choose Overview and click Access Addresses to view the HTTPS application access address.
    // const endpoint = "https://iotda.cn-north-4.myhuaweicloud.com";
    const endpoint = "<YOUR ENDPOINT>";
    const project_id = "<YOUR PROJECT_ID>";
    // Create a credential.
    const credentials = new core.BasicCredentials()
                         .withAk(ak)
                         .withSk(sk)
                         .withProjectId(project_id)
    // Create and initialize an IoTDAClient instance.
    const client = iotda.IoTDAClient.newBuilder()
                                .withCredential(credentials)
                                .withEndpoint(endpoint)
                                .build();
    // Instantiate a request object.
    const request = new iotda.ListDevicesRequest();
    // Call the API for querying the device list.
    const result = client.listDevices(request);
    result.then(result => {
        console.log("JSON.stringify(result)::" + JSON.stringify(result));
    }).catch(ex => {
        console.log("exception:" + JSON.stringify(ex));
    });

    Parameter

    Description

    ak

    Access key ID of your Huawei Cloud account. You can create and view an AK/SK on the My Credentials > Access Keys page of the Huawei Cloud management console. For more information, see Access Keys.

    sk

    Secret access key (SK) of your Huawei Cloud account.

    endpoint

    Endpoint of the region where the Huawei Cloud service to be accessed is located.

    On the console, you can view the region name of the current service and the mapping between regions and endpoints. For details, see Platform Connection Information.

    project_id

    ID of the project where the Huawei Cloud service to be accessed is located. Select a project ID based on the region to which the project belongs.

Additional Information

For details on the project source code and usage guide, see Huawei Cloud Node.js Software Development Kit (Node.js SDK).