All Products
Search
Document Center

Alibaba Cloud SDK:Initialize an SDK client

Last Updated:May 21, 2024

This topic describes how to initialize an SDK client, including the request clients of the remote procedure call (RPC) and resource-oriented architecture (ROA) styles. This topic also describes how to initiate a request after an SDK client is initialized.

Note

API styles that are supported by Alibaba Cloud SDK V1.0 vary based on Alibaba Cloud services. We recommend that you use Alibaba Cloud SDK V2.0.

Initialize an SDK client of the RPC style

You can initialize an SDK client of the RPC style by using the following code and initiate a request. The following sample code provides an example on how to call the DescribeInstances operation of Elastic Compute Service (ECS). The API operation is of the 2014-05-26 version. You can obtain the sample code for calling other operations from OpenAPI Explorer.

const RPCClient = require('@alicloud/pop-core').RPCClient;

const client = new RPCClient({
    accessKeyId: process.env.ALIBABA_CLOUD_ACCESS_KEY_ID,
    accessKeySecret: process.env.ALIBABA_CLOUD_ACCESS_KEY_SECRET,
    endpoint: 'https://ecs.cn-beijing.aliyuncs.com', 
    apiVersion: '2014-05-26', 
});
const params = {};
const action = 'DescribeInstances';

// Call Method 1:=> returns Promise
// client.request(action, params).then((result)=>{
//     console.log(JSON.stringify(result));
// });

// Call Method 2:options
client.request(action, params, {
    timeout: 3000, // default 3000 ms
    formatAction: true, // default true, format the action to Action
    formatParams: true, // default true, format the parameter name to first letter upper case
    method: 'GET', // set the http method, default is GET
    headers: {}, // set the http request headers
}).then( (response) => {
    console.log(JSON.stringify(response));
});

Initialize an SDK client of the ROA style

You can initialize an SDK client of the ROA style by using the following code and initiate a request. The following sample code provides an example on how to call the DescribeClustersV1 operation of Container Service for Kubernetes (ACK). The API operation is of the 2015-12-15 version. You can obtain the sample code for calling other operations from OpenAPI Explorer.

const ROAClient = require('@alicloud/pop-core').ROAClient;

const roaClient = new ROAClient({
    accessKeyId: process.env.ALIBABA_CLOUD_ACCESS_KEY_ID,
    accessKeySecret: process.env.ALIBABA_CLOUD_ACCESS_KEY_SECRET,
    endpoint: 'https://cs.cn-beijing.aliyuncs.com', 
    apiVersion: '2015-12-15', 
});

const method = "GET" // The request method.
const uriPattern = '/api/v1/clusters'; // The URL.
const queryParams ={}; // The request parameters.
const body = ""; // The request body.
const headers = {
    "Content-Type": "application/json"
}; // The custom request header.
const options = {
    timeout: 3000, // default 3000 ms
};

roaClient.request(method, uriPattern, queryParams, body, headers, options)
    .then((response) => {
        console.log('ROA Response:', response);
    })
    .catch((error) => {
        console.error('ROA Error:', error);
    });