All Products
Search
Document Center

OpenSearch:TypeScript Config class

Last Updated:Aug 09, 2023

Configure environment variables

Configure the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables.

Important
  • The AccessKey pair of an Alibaba Cloud account can be used to access all API operations. We recommend that you use a Resource Access Management (RAM) user to call API operations or perform routine O&M. For information about how to use a RAM user, see Create a RAM user.

  • For information about how to create an AccessKey pair, see Create an AccessKey pair.

  • If you use the AccessKey pair of a RAM user, make sure that the required permissions are granted to the AliyunServiceRoleForOpenSearch role by using your Alibaba Cloud account. For more information, see AliyunServiceRoleForOpenSearch and Access authorization rules.

  • We recommend that you do not include your AccessKey pair in materials that are easily accessible to others, such as the project code. Otherwise, your AccessKey pair may be leaked and resources in your account become insecure.

  • Linux and macOS

    Run the following commands. Replace <access_key_id> and <access_key_secret> with the AccessKey ID and AccessKey secret of the RAM user that you use.

    export ALIBABA_CLOUD_ACCESS_KEY_ID=<access_key_id> 
    export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<access_key_secret>
  • Windows

    1. Create an environment variable file, add the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables to the file, and then set the environment variables to your AccessKey ID and AccessKey secret.

    2. Restart Windows for the AccessKey pair to take effect.

Add dependencies

Download dependency packages at https://www.npmjs.com/.

  • dependencies

@alicloud/credentials 
@alicloud/opensearch-util 
@alicloud/tea-typescript 
@alicloud/tea-util
  • devDependencies

typescript
ts-node

Config.ts

import * as $tea from '@alicloud/tea-typescript';

class Config extends $tea.Model {
  endpoint?: string;
  protocol?: string;
  type?: string;
  securityToken?: string;
  accessKeyId?: string;
  accessKeySecret?: string;
  userAgent?: string;
  static names(): { [key: string]: string } {
    return {
      endpoint: 'endpoint',
      protocol: 'protocol',
      type: 'type',
      securityToken: 'securityToken',
      // Specify your AccessKey pair.
    	// Obtain the AccessKey ID and AccessKey secret from environment variables. 
    	// You must configure environment variables before you run this code. For more information, see the "Configure environment variables" section in this topic.
      accessKeyId: process.env.ALIBABA_CLOUD_ACCESS_KEY_ID,
      accessKeySecret: process.env.ALIBABA_CLOUD_ACCESS_KEY_SECRET,
      userAgent: 'userAgent',
    };
  }

  static types(): { [key: string]: any } {
    return {
      endpoint: 'string',
      protocol: 'string',
      type: 'string',
      securityToken: 'string',
      accessKeyId: 'string',
      accessKeySecret: 'string',
      userAgent: 'string',
    };
  }

  constructor(map?: { [key: string]: any }) {
    super(map);
  }
}

export default Config;