Welcome to the Alibaba Cloud Software Development Kit (SDK). The Alibaba Cloud SDK for Node.js allows you to access Alibaba Cloud services such as Elastic Compute Service (ECS), Relational Database Service (RDS), and CloudMonitor. You can access Alibaba Cloud services without the need to handle API related tasks, such as signing and constructing your requests. This topic describes how to obtain and use Alibaba Cloud SDK for Node.js.

Prepare the environment

  • To use Alibaba Cloud SDK for Node.js, you must have an Alibaba Cloud account as well as an AccessKey. The AccessKey is required when initializing AcsClient. You can create an AccessKey in the Alibaba Cloud console.
  • Before calling an API for a product using the Alibaba Cloud SDK, make sure that you have already opened the product in the Alibaba Cloud console.
  • Alibaba Cloud SDK for Node.js requires the latest LTS versions of Node.js (Node.js 8.x and Node.js 10.x). You can run the commandnode -v to view the Node.js version.

Debug and generate SDK examples online

You can use OpenAPI Explorer to call APIs of cloud services, dynamically generate SDK sample codes, and quickly retrieve interfaces.

Install Alibaba Cloud SDK for Node.js

Complete the following steps to install the Alibaba Cloud SDK for Node.js:

  1. Download the SDK of the product from GitHub.
  2. Install the Alibaba Cloud SDK for Node.js.

    We recommend that you usenpmto install the Node.js dependency module. Alibaba Cloud SDK for Node.js is under the scope of @alicloud.

    Suppose the path of the downloaded Alibaba Cloud SDK for Node.js is /path/to/aliyun-openapi-Node.js-sdk.

    • If the development is based on the SDK core library, run the following command to install the @alicloud/pop-core module. The --save option in the preceding command writes the module into the package.json file of the application, serving as the dependency module.
      $ npm install @alicloud/pop-core --save
    • When the development is based on a specific cloud product SDK, you must install the module of the cloud product. This topic takes installing the module of MNS product as the example.
      $ npm install @alicloud/mns --save

Code example

  • Call an RPC API
    var RPCClient = require('@alicloud/pop-core').RPCClient;
    
    var client = new RPCClient({
      accessKeyId: '<accessKeyId>',
      secretAccessKey: '<secretAccessKey>',
       endpoint: '<endpoint>',
      apiVersion: '<apiVersion>'
     });
    
     // => returns Promise
     client.request(action, params, options);
  • Call a REST API
    var ROAClient = require('@alicloud/pop-core').ROAClient;
    
    var client = new ROAClient({
      accessKeyId: '<accessKeyId>',
      accessKeySecret: '<secretAccessKey>',
      endpoint: '<endpoint>',
      apiVersion: '<apiVersion>'
    });
    
    // => returns Promise
    // request(HTTPMethod, uriPath, queries, body, headers, options);
    // options => {timeout}
    client.request('GET', '/regions');
  • The following example shows how to use the Alibaba Cloud SDK for Node.js to call a service:

    // Introduce the SDK
    const Client = require('@alicloud/oam');
    
    // Create an instance
    const client = new Client({
      accessKeyId: '<ACCESS_KEY_ID>',
      secretAccessKey: '<ACCESS_KEY_SECRET>',
      securityToken: '', // Supports STS
      endpoint: 'ENDPOINT'
    });
    
    // Call a service
    // Detailed API, return the promise
    client.addRoleCellToRole({
      RoleName: "role1",
      Resource: "*:*:*:*",
      ActionList: ["Read", "Write"],
      GrantOption: 0
    }, {timeout: 10000});