All Products
Search
Document Center

Alibaba Cloud SDK:Get started with Alibaba Cloud Darabonba SDK for Node.js

Last Updated:Jan 10, 2024

This topic describes how to install the environment for using Alibaba Cloud Darabonba SDK for Node.js and run the sample code to call an API operation.

Preparations

Prerequisites

  • Node.js 8.0 or later is downloaded and installed. To download the required Node.js version, go to the Downloads page on the Node.js official website.

  • An integrated development environment (IDE) is downloaded and installed. In this topic, Visual Studio Code is used. To download Visual Studio Code, visit the Visual Studio Code official website.

  • An AccessKey pair is created. When you call an API operation, you must use an AccessKey pair to complete identity authentication. An AccessKey pair consists of an AccessKey ID and an AccessKey secret. For more information about how to create an AccessKey pair, see Create an AccessKey pair.

Node.js version

  • Run the node -v command on the terminal to check the Node.js version.

  • Run the npm -v command on the terminal to check the NPM version.

Run the sample code

Step 1: Download an SDK project

  1. Log on to OpenAPI Explorer.

  2. In the top navigation bar, click Select a cloud service. In the panel that appears, select the cloud service that you want to manage. The page of the selected cloud service appears.

  3. In the top navigation bar, click API Debugging. Then, a page with three columns appears.

    image.png

  4. Select the API operation that you want to call in the leftmost column. Configure the required parameters in the middle column. Then, click the SDK Sample Code tab in the rightmost column.

  5. Select V2.0 from the SDK Version drop-down list. Select a programming language based on your business requirements.

  6. Click Download Project to download the sample code.

  7. If you want to generate generic code, turn on Common Request. For more information, see Generic calls and specialized calls.

Step 2: Configure environment variables

Configure the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables.

  • If you use a Linux or macOS operating system, perform the following operations:

    • Create the .bash_profile file.

      Note

      If you configure environment variables for the first time, you can run the touch ~/.bash_profile command to create the configuration file. If the configuration file already exists, run the vim ~/.bash_profile command.

      touch .bash_profile
      vim ~/.bash_profile
    • Configure the .bash_profile file.

      # Replace <ACCESS_KEY_ID> and <ACCESS_KEY_SECRET> in the following commands with your AccessKey ID and AccessKey secret. 
      export ALIBABA_CLOUD_ACCESS_KEY_ID=<ACCESS_KEY_ID>
      export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<ACCESS_KEY_SECRET>
    • Use the .bash_profile file.

      # Run the commands in the configuration file.
      source ~/.bash_profile
      # Check whether the configuration takes effect.
      echo $ALIBABA_CLOUD_ACCESS_KEY_ID
  • If you use a Windows operating system, create a file to add the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables, and set the variables to your AccessKey ID and AccessKey secret.

Restart the IDE. Otherwise, the environment variables may fail to be read.

Step 3: Run the code

  1. Run the code in Visual Studio Code. After the code is run, view the returned information and request ID in the body of console logs.

  2. If you call an API operation to create an instance, the instance ID is also returned. You can log on to the console of the Alibaba Cloud service to verify the returned information based on the instance ID.