Integrate the Alibaba Cloud SDK into your project to simplify OpenAPI calls, speed up feature integration, and reduce maintenance costs. This topic covers SDK installation and basic usage.
Prerequisites
Node.js 8.x or later
Install the SDK
The V1.0 Node.js SDK supports only generalized calls (CommonRequest), which require only the @alicloud/pop-core dependency. Run the following command to install it:
npm install @alicloud/pop-core
Use the SDK
The V1.0 Node.js SDK supports two call styles: RPC and ROA. The following examples use the ECS DescribeInstances operation for RPC and the Container Service for Kubernetes (ACK) DescribeClustersV1 operation for ROA.
1. Initialize the request client
All OpenAPI operations are called through the RPCClient or ROAClient provided by @alicloud/pop-core. You must initialize the client before calling any operation. The following example uses an AccessKey pair. For other initialization methods, see Initialize the client using an STS token.
The example obtains credentials from environment variables. Before you run the code, configure the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables. For more information, see Configure environment variables on Linux, macOS, and Windows systems.
RPC client
// Initialize the RPC client
const Core = require('@alicloud/pop-core');
var client = new Core({
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is set.
accessKeyId: process.env['ALIBABA_CLOUD_ACCESS_KEY_ID'],
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is set.
accessKeySecret: process.env['ALIBABA_CLOUD_ACCESS_KEY_SECRET'],
// The domain name in the http(s)://<product_endpoint> format. We recommend that you use HTTPS. Example for ECS: http://ecs-cn-hangzhou.aliyuncs.com
endpoint: 'https://ecs.cn-hangzhou.aliyuncs.com',
// The API version of the cloud product. Example for ECS: 2014-05-26
apiVersion: '2014-05-26'
});
ROA client
// Initialize the ROA client
var ROAClient = require('@alicloud/pop-core').ROAClient;
var client = new ROAClient({
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is set.
accessKeyId: process.env['ALIBABA_CLOUD_ACCESS_KEY_ID'],
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is set.
accessKeySecret: process.env['ALIBABA_CLOUD_ACCESS_KEY_SECRET'],
// The domain name in the http(s)://<product_endpoint> format. We recommend that you use HTTPS. Example for Container Service: http://cs.cn-chengdu.aliyuncs.com
endpoint: 'https://cs.cn-chengdu.aliyuncs.com',
// The API version of the cloud product. Example for Container Service: 2015-12-15
apiVersion: '2015-12-15'
});
2. Construct request parameters
A request requires three parameters: the OpenAPI operation name, the request parameter object, and the runtime parameter object.
Uploading a local file is not supported. To upload a local file, use the V2.0 SDK.
RPC call parameters
// The name of the OpenAPI operation
var action = 'DescribeInstances'
// The request parameters
var params = {
"RegionId": "cn-hangzhou",
"InstanceIds": "[\"i-bp67************\", \"i-7xva************\", … \"i-7xvc************\"]"
}
// The runtime parameters
var requestOption = {
method: 'POST',
formatParams: false,
};
ROA call parameters
// The request method
const method = "GET"
// The path parameter
const uriPattern = '/api/v1/clusters';
// The query parameters
var queryParams = {
"cluster_type": "Kubernetes",
"name": "cluster-demo"
};
// The request body in the JSON string format. Example: `{"nodepool_info":{"name":"nodepool-test","type":"ess"}}`;
const body = "";
// The custom request headers
const headers = {
"Content-Type": "application/json"
};
// The runtime parameters
const options = {
timeout: 3000, // default 3000 ms
};
3. Send the request
Call the request operation of the client created in Step 1 and pass the parameters constructed in Step 2.
RPC call
// Send the request
client.request(action, params, requestOption).then((result) => {
console.log(JSON.stringify(result));
}, (ex) => {
console.log(ex);
})
ROA call
// Send the request
roaClient.request(method, uriPattern, queryParams, body, headers, options)
.then((response) => {
console.log('ROA Response:', response);
})
.catch((error) => {
console.error('ROA Error:', error);
});
4. Handle exceptions
By default, an exception is thrown if the Code field in the API response is not 200, OK, Success, or success. You can define custom error codes for your program. For more information, see Exception handling.
References
-
For more information about RPC and ROA, see OpenAPI styles.
-
For more information about advanced SDK configurations such as proxy and timeout settings, see Advanced configurations.
-
For more information about creating an AccessKey pair, see Create an AccessKey pair.