Application Node.js SDK
IoTDA 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.
SDK Obtaining and Installing
- 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.
- 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.
- Create a credential.
- Create and initialize an IoTDAClient instance.
- Instantiate a request object.
- 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, and decrypt the AK/SK when using them; // 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>"; // region_id: If CN East-Shanghai1 is used, enter cn-east-3. If CN North-Beijing4 is used, enter cn-north-4. If CN South-Guangzhou is used, enter cn-south-1. const region_id = "<YOUR REGION_ID>"; // (Optional) Skip server certificate verification. process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0" // Create a credential. const credentials = new core.BasicCredentials() .withAk(ak) .withSk(sk) // WithDerivedPredicate is used for the standard or enterprise edition. For the basic edition, delete the line. .withDerivedPredicate(core.BasicCredentials.getDefaultDerivedPredicate) .withProjectId(project_id) // Create and initialize an IoTDAClient instance. const client = iotda.IoTDAClient.newBuilder() .withCredential(credentials) .withEndpoint(endpoint) .withRegion(new core.Region(region_id, 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 Access Keys.
page of the Huawei Cloud console. For details, seesk
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.
region_id
If CN East-Shanghai1 is used, enter cn-east-3. If CN North-Beijing4 is used, enter cn-north-4. If CN South-Guangzhou is used, enter cn-south-4.
Additional Information
For details on the project source code and usage guide, see Huawei Cloud Node.js Software Development Kit (Node.js SDK).
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot