To facilitate API calls, we recommend that you integrate with Alibaba Cloud SDK in your project. SDKs simplify the development process, quickly integrate features, and greatly reduce the O&M cost. This topic describes how to integrate Alibaba Cloud SDK V1.0 for Node.js in your project and use the SDK for development.
Prerequisites
Node.js 8.x or later is installed.
Install the SDK
Alibaba Cloud SDK V1.0 for Node.js supports only generic calls that use CommonRequest. The call method simplifies the API call process and can be implemented by integrating the @alicloud/pop-core dependencies. You can run the following command to install the SDK dependencies:
npm install @alicloud/pop-coreUse the SDK
You can use Alibaba Cloud SDK V1.0 for Node.js to call API operations in the remote procedure call (RPC) or resource-oriented architecture (ROA) style. The procedures for calling RPC-style API operations and ROA-style API operations are different. This section describes the procedures. In the following examples, the RPC-style DescribeInstances API operation of Elastic Compute Service (ECS) is called, and the ROA-style DescribeClustersV1 API operation of Container Service for Kubernetes (ACK) is called.
1. Initialize a request client
All API operations are called by using the RPC client or ROA client provided by the @alicloud/pop-core SDK library. Before you call an API operation, you must initialize the request client. In this example, an AccessKey pair is used to initialize the request client. For more information about other client initialization methods, see Initialize an SDK client by using an STS token.
In this example, the AccessKey pair is obtained from the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables, which must be configured before you run the code. For more information, see Configure environment variables in Linux, macOS, and Windows.
RPC Client
// The method for initializing 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 configured in the code runtime environment
accessKeyId: process.env['ALIBABA_CLOUD_ACCESS_KEY_ID'],
// Required. Make sure that the following environment variable is set in the code runtime environment: ALIBABA_CLOUD_ACCESS_KEY_SECRET.
accessKeySecret: process.env['ALIBABA_CLOUD_ACCESS_KEY_SECRET'],
// The domain name in the format of http(s)://The cloud service endpoint. We recommend that you use HTTPS. For example, the endpoint of ECS is http://ecs-cn-hangzhou.aliyuncs.com.
endpoint: 'https://ecs.cn-hangzhou.aliyuncs.com',
// The version of the cloud service API. The example, the version number of the ECS API is 2014-05-26.
apiVersion: '2014-05-26'
});ROA Client
// The method for initializing 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 configured in the code runtime environment
accessKeyId: process.env['ALIBABA_CLOUD_ACCESS_KEY_ID'],
// Required. Make sure that the following environment variable is set in the code runtime environment: ALIBABA_CLOUD_ACCESS_KEY_SECRET.
accessKeySecret: process.env['ALIBABA_CLOUD_ACCESS_KEY_SECRET'],
// The domain name in the format of http(s)://The cloud service endpoint. We recommend that you use HTTPS. For example, the endpoint of ECS is http://cs.cn-chengdu.aliyuncs.com.
endpoint: 'https://cs.cn-chengdu.aliyuncs.com',
// The version of the cloud service API. The example, the version number of the ECS API is 2014-05-26.
apiVersion: '2015-12-15'
});2. Build the required request parameters
You must specify the API operation, request parameter object, and runtime parameter object.
Alibaba Cloud SDK V1.0 does not support local file upload. If you need to upload local files, use Alibaba Cloud SDK V2.0.
Parameters for calling RPC-style API operations
// The name of the API 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,
};Parameters for calling ROA-style API operations
// The request method.
const method = "GET"
// The URI parameters.
const uriPattern = '/api/v1/clusters';
// The query parameters.
var queryParams = {
"cluster_type": "Kubernetes",
"name": "cluster-demo"
};
// The request body, which is a JSON string. Example: `{"nodepool_info":{"name":"nodepool-test","type":"ess"}}`;
const body = "";
// Custom request headers.
const headers = {
"Content-Type": "application/json"
};
// The runtime parameters.
const options = {
timeout: 3000, // default 3000 ms
};3. Initiate an API request
Use the client created in Step 1 to call the request operation, which initiates the request. The operation uses the parameters built in Step 2.
Call an RPC-style API operation
// Initiate a request.
client.request(action, params, requestOption).then((result) => {
console.log(JSON.stringify(result));
}, (ex) => {
console.log(ex);
})Call an ROA-style API operation
// Initiate a 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 errors
If the HTTP status code or response message returned from the API operation is not 200, OK, Success, or success, an error is thrown. Alibaba Cloud SDKs support custom HTTP status codes. You can define an error handling method for your program. For more information, see Handle exceptions.
References
For more information about the RPC and ROA styles, see API styles.
For more information about advanced SDK settings, such as proxy settings and timeout settings, see Advanced settings.
For more information about how to create an AccessKey pair, see Create an AccessKey pair.