Initialization

Updated at:

This topic describes how to initialize the Node.js software development kit (SDK) using an account AccessKey pair or a Security Token Service (STS) token.

Prerequisites

Obtain an AccessKey pair from environment variables

Set the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables to configure the default credentials. When you call an API operation, the SDK reads the AccessKey pair from these environment variables and uses it for authentication. For setup instructions, see Configure environment variables in Linux, macOS, and Windows.

Initialization

You can initialize the client by providing your account AccessKey pair. The following code provides an example.

let config = new OpenApi.Config({
      // Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is set.
      accessKeyId: process.env['ALIBABA_CLOUD_ACCESS_KEY_ID'],
      // Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is set.
      accessKeySecret: process.env['ALIBABA_CLOUD_ACCESS_KEY_SECRET'],
});

Complete code

The following sample code shows an example of calling the SearchPipeline API operation.

'use strict';

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

class Client {

  /**
   * Initialize the client using an AccessKey ID and an AccessKey secret.
   * @return Client
   * @throws Exception
   */
  static createClient() {

    let config = new OpenApi.Config({
      // Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is set.
      accessKeyId: process.env['ALIBABA_CLOUD_ACCESS_KEY_ID'],
      // Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is set.
      accessKeySecret: process.env['ALIBABA_CLOUD_ACCESS_KEY_SECRET'],
    });
    config.endpoint = `mts.cn-qingdao.aliyuncs.com`;
    return new Mts20140618.default(config);
  }

  static async main(args) {
    let client = Client.createClient();
    let searchPipelineRequest = new Mts20140618.SearchPipelineRequest({ });
    let runtime = new Util.RuntimeOptions({ });
    try {
      // If you copy this code to run it, print the API return value.
      await client.searchPipelineWithOptions(searchPipelineRequest, runtime);
    } catch (error) {
      // This is for demonstration only. Handle exceptions with caution. Do not ignore exceptions in your projects.
      // Error message
      console.log(error.message);
      // Diagnosis address
      console.log(error.data["Recommend"]);
      Util.default.assertAsString(error.message);
    }    
  }

}

exports.Client = Client;
Client.main(process.argv.slice(2));