All Products
Search
Document Center

Alibaba Cloud SDK:Generic calls

Last Updated:Aug 12, 2024

You can make generic calls and specialized calls of operations by using Alibaba Cloud SDKs. For more information about the differences between the generic calls and specialized calls, see Generic calls and specialized calls. This topic describes how to view API information and install SDK for Java V2.0, and provide sample code. This helps you make generic calls of operations.

View API information

Go to the Alibaba Cloud API Documentation page and select an Alibaba Cloud service. In this example, Elastic Compute Service (ECS) is selected.

  1. In the left-side pane, click Metadata below the cloud service name. On the page that appears, find the info.style parameter to view the API style supported by the cloud service, such as remote procedure call (RPC) or resource-oriented architecture (ROA).

    image

    image

    Note

    The metadata displayed on the page contains all API information of the cloud service. If you want to view the metadata of an API operation, go to Step 2.

  2. Select the API operation that you want to call and click Metadata in the upper-right corner of the page.

    image

    The metadata of an API operation defines the network protocols, request methods, parameters, and parameter locations supported by the API operation. As shown in the following figure, the metadata of the RunInstances operation contains the following information:

    • Supported network protocols: HTTP and HTTPS. We recommend that you use HTTPS.

    • Supported request methods: GET and POST. You can use the two request methods to obtain the same response. However, if you use the GET method, a request packet can be up to 32 KB in size. We recommend that you use the POST method.

    • Supported parameters include RegionId and ImageId. The parameter location is query, which indicates that these parameters are concatenated and placed after the request URL. Example: https://ecs.cn-beijing.aliyuncs.com/?ImageId=aliyun_2_1903_x64_20G_alibase_20231221.vhd&InstanceChargeType=PostPaid&InstanceType=ecs.e-c1m1.large&InternetChargeType=PayByTraffic&MinAmount=1&Password=test%401234&RegionId=cn-beijing&SecurityGroupId=sg-2zec0dm6qi66XXXXXXXX&SystemDisk.Category=cloud_essd&SystemDisk.Size=40&VSwitchId=vsw-2ze3aagwn397gXXXXXXXX.

    image

    Note

    Other content of the metadata has no impact on signature calculation. For more information about the metadata, see Guidelines for use.

Install SDK for Node.js V2.0

Run the following command in the terminal to install the core library of SDK for Node.js V2.0:

npm install @alicloud/openapi-client

Sample code

Call an RPC API operation

In this example, the DescribeRegions operation of ECS is called to show how to make a generic call of an operation.

JavaScript

const OpenApi = require('@alicloud/openapi-client');
const Util = require('@alicloud/tea-util');

class Client {
    static async main() {
        // Obtain the AccessKey ID and AccessKey secret from environment variables. 
        let config = new OpenApi.Config({
            accessKeyId: process.env['ALIBABA_CLOUD_ACCESS_KEY_ID'],
            accessKeySecret: process.env['ALIBABA_CLOUD_ACCESS_KEY_SECRET'],
        });
        config.endpoint = `ecs-cn-hangzhou.aliyuncs.com`;
        let client = new OpenApi.default(config);
        let params = new OpenApi.Params({
            style: 'RPC', // The API style.
            action: 'DescribeRegions', // The operation that you want to call.
            version: '2014-05-26', // The version number of the operation.
            protocol: 'HTTPS', // The protocol of the operation.
            method: 'POST', // The HTTP method of the operation.
            authType: 'AK',
            pathname: `/`, // The URL of the operation.
            reqBodyType: 'json', // The format of the request body.
            bodyType: 'json', // The format of the response body.
        });
        let query = { 'InstanceChargeType': 'PrePaid' };
        let request = new OpenApi.OpenApiRequest({
            query: query,
        });
        let runtime = new Util.RuntimeOptions({});
        let response = await client.callApi(params, request, runtime);
        console.log(response.body.Regions);
    }
}

exports.Client = Client;
Client.main();

TypeScript

import OpenApi, * as $OpenApi from '@alicloud/openapi-client';
import Util, * as $Util from '@alicloud/tea-util';

export default class Client {
  static async main(): Promise<void> {
    // Obtain the AccessKey ID and AccessKey secret from environment variables. 
    let config = new $OpenApi.Config({
      accessKeyId: process.env['ALIBABA_CLOUD_ACCESS_KEY_ID'],
      accessKeySecret: process.env['ALIBABA_CLOUD_ACCESS_KEY_SECRET'],
    });
    // Set the endpoint of the service to ecs-cn-hangzhou.aliyuncs.com. 
    config.endpoint = `ecs-cn-hangzhou.aliyuncs.com`;
    let client = new OpenApi(config);
    let params = new $OpenApi.Params({
      style: 'RPC', // The API style.
      action: 'DescribeRegions', // The operation that you want to call.
      version: '2014-05-26', // The version number of the operation.
      protocol: 'HTTPS', // The protocol of the operation.
      method: 'POST', // The HTTP method of the operation.
      authType: 'AK',
      pathname: `/`, // The URL of the operation.
      reqBodyType: 'json', // The format of the request body.
      bodyType: 'json', // The format of the response body.
    });
    // Specify the query parameters. 
    let query: { [key: string]: any } = { "InstanceChargeType": "PrePaid" };
    // Create an API request. 
    let request = new $OpenApi.OpenApiRequest({
      query: query,
    });

    // Specify the runtime parameters.
    let runtime = new $Util.RuntimeOptions({});
    // The response is of the MAP type, which contains the response body, response headers, and HTTP status code. 
    let response = await client.callApi(params, request, runtime);
    console.log(response.body.Regions);
  }
}

Client.main();

Call a RESTful API operation

In this example, the DescribeClustersV1 operation of Container Service for Kubernetes (ACK) is called to show how to make a generic call of an operation.

JavaScript

const OpenApi = require('@alicloud/openapi-client');
const OpenApiUtil = require('@alicloud/openapi-util');
const Util = require('@alicloud/tea-util');
const Tea = require('@alicloud/tea-typescript');

class Client {
    static async main() {
        // Obtain the AccessKey ID and AccessKey secret from environment variables. 
        let config = new OpenApi.Config({
            accessKeyId: process.env['ALIBABA_CLOUD_ACCESS_KEY_ID'],
            accessKeySecret: process.env['ALIBABA_CLOUD_ACCESS_KEY_SECRET'],
        });
        config.endpoint = `cs.cn-qingdao.aliyuncs.com`;
        let client = new OpenApi.default(config);
        let params = new OpenApi.Params({
            // The operation that you want to call.
            action: 'DescribeClustersV1',
            // The version number of the operation.
            version: '2015-12-15',
            // The protocol of the operation.
            protocol: 'HTTPS',
            // The HTTP method of the operation.
            method: 'GET',
            authType: 'AK',
            style: 'ROA',
            // The URL of the operation.
            pathname: `/api/v1/clusters`,
            // The format of the request body.
            reqBodyType: 'json',
            // The format of the response body.
            bodyType: 'json',
        });
        // query params
        let queries = {};
        queries['name'] = 'cluster-demo';
        let request = new OpenApi.OpenApiRequest({
            query: OpenApiUtil.default.query(queries),
        });
        // runtime options
        let runtime = new Util.RuntimeOptions({});
        // The response is of the MAP type, which contains the response body, response headers, and HTTP status code. 
        let response = await client.callApi(params, request, runtime);
        console.log(response.body);
    }
}

exports.Client = Client;
Client.main();

TypeScript

import OpenApi, * as $OpenApi from '@alicloud/openapi-client';
import OpenApiUtil from '@alicloud/openapi-util';
import Util, * as $Util from '@alicloud/tea-util';
import * as $tea from '@alicloud/tea-typescript';

export default class Client {
  static async main(): Promise<void> {
    let config = new $OpenApi.Config({
      accessKeyId: process.env['ALIBABA_CLOUD_ACCESS_KEY_ID'],
      accessKeySecret: process.env['ALIBABA_CLOUD_ACCESS_KEY_SECRET'],
    });
    config.endpoint = `cs.cn-qingdao.aliyuncs.com`;
    let client = new OpenApi(config);
    let params = new $OpenApi.Params({
      // The operation that you want to call.
      action: "DescribeClustersV1",
      // The version number of the operation.
      version: "2015-12-15",
      // The protocol of the operation.
      protocol: "HTTPS",
      // The HTTP method of the operation.
      method: "GET",
      authType: "AK",
      style: "ROA",
      // The URL of the operation.
      pathname: `/api/v1/clusters`,
      // The format of the request body.
      reqBodyType: "json",
      // The format of the response body.
      bodyType: "json",
    });
    // query params
    let queries: { [key: string]: any } = { "name": "cluster-demo" };
    let request = new $OpenApi.OpenApiRequest({
      query: OpenApiUtil.query(queries),
    });
    // runtime options
    let runtime = new $Util.RuntimeOptions({});
    // The response is of the MAP type, which contains the response body, response headers, and HTTP status code. 
    let response = await client.callApi(params, request, runtime);
    console.log(response.body);
  }
}

Client.main();