edit-icon download-icon

Use Node.js SDK

Last Updated: Mar 22, 2018

The Alibaba Cloud Node.js Software Development Kit (SDK) allows you to access Alibaba Cloud services such as Elastic Compute Service (ECS), Server Load Balancer (SLB), and CloudMonitor. You can access Alibaba Cloud services without the need to handle API related tasks, such as signing and constructing your requests.

This document introduces how to obtain and call Alibaba Cloud Node.js SDK.

If you have any problem while using Node.js SDK, please join the DingTalk group: 11771185 (the official SDK customer service group of Alibaba Cloud) for consultation.

Prerequisites

  1. To use Alibaba Cloud Node.js SDK, 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. For more information, see Create an AccessKey.

    Note: To increase the security of your account, we recommend that you use the AccessKey of the RAM user to access Alibaba Cloud services.

  2. To use Alibaba Cloud Node.js SDK to access the APIs of a product, you must first activate the product on the Alibaba Cloud console if required.

  3. Alibaba Cloud Node.js SDK requires the latest LTS versions of Node.js (Node.js 4.x and Node.js 6.x). You can run the node -v to view the Node.js version.

Install Node.js SDK

We recommend that you use npm to install Node.js dependency modules. Alibaba Cloud Node.js SDK is under the scope of @alicloud.

  1. Run the following command to install the @alicloud/pop-core module to install the core library of the SDK:

    1. $ npm install @alicloud/pop-core --save

    The --save option in the preceding command writes the module into the package.json file of the application, serving as the dependency module.

  2. Install the SDK module of the service that you want to use.

    The following code uses the ECS as an example:

    1. $ npm install @alicloud/ecs --save

Code example

Alibaba Cloud OpenAPI is classified into two types, RPC APIs and REST APIs.

Call an RPC API

The following example shows how to use the Node.js SDK to call an RPC API:

  1. var RPCClient = require('@alicloud/pop-core').RPCClient;
  2. var client = new RPCClient({
  3. accessKeyId: '<accessKeyId>',
  4. secretAccessKey: '<secretAccessKey>',
  5. endpoint: '<endpoint>',
  6. apiVersion: '<apiVersion>'
  7. });
  8. // => returns Promise
  9. client.request(action, params, options);

REST API

The following example shows how to use the Node.js SDK to call a REST API:

  1. var ROAClient = require('@alicloud/pop-core').ROAClient;
  2. var client = new ROAClient({
  3. accessKeyId: '<accessKeyId>',
  4. accessKeySecret: '<secretAccessKey>',
  5. endpoint: '<endpoint>',
  6. apiVersion: '<apiVersion>'
  7. });
  8. // => returns Promise
  9. // request(HTTPMethod, uriPath, queries, body, headers, options);
  10. // options => {timeout}
  11. client.request('GET', '/regions');

Call a service

The following example shows how to use the Node.js SDK to call a service:

  1. // Introduce the SDK
  2. const Client = require('@alicloud/oam');
  3. // Create an instance
  4. const client = new Client({
  5. accessKeyId: '<ACCESS_KEY_ID>',
  6. secretAccessKey: '<ACCESS_KEY_SECRET>',
  7. securityToken: '', // Supports STS
  8. endpoint: 'ENDPOINT'
  9. });
  10. // Call a service
  11. // Detailed API, return the promise
  12. client.addRoleCellToRole({
  13. RoleName: "role1",
  14. Resource: "*:*:*:*",
  15. ActionList: ["Read", "Write"],
  16. GrantOption: 0
  17. }, {timeout: 10000});
Thank you! We've received your feedback.