@alicloud/credentials

中文文档
2.4.7发布于 2026/08/18MITTypeScriptCommonJS近 30 天下载 58.1万

alibaba cloud node.js sdk credentials

原始使用文档
文档目录6 节
  1. Installation
  2. Quick Examples
  3. Credential Type
  4. Provider
  5. Test & Coverage
  6. License

English | 简体中文

Alibaba Cloud Credentials for TypeScript/Node.js

npm version NodeJS codecov license

Installation

npm install @alicloud/credentials

Node.js >= 12 required.

Quick Examples

Before you begin, you need to sign up for an Alibaba Cloud account and retrieve your Credentials.

Credential Type

access_key

Setup access_key credential through User Information Management, it have full authority over the account, please keep it safe. Sometimes for security reasons, you cannot hand over a primary account AccessKey with full access to the developer of a project. You may create a sub-account RAM Sub-account , grant its authorization,and use the AccessKey of RAM Sub-account.

import Credential, { Config } from '@alicloud/credentials';

const config: Config = {
  type:               'access_key',       // credential type
  accessKeyId:        'accessKeyId',      // AccessKeyId of your account
  accessKeySecret:    'accessKeySecret',  // AccessKeySecret of your account
}
const cred = new Credential(config);
const {
  accessKeyId,
  accessKeySecret,
  type
} = await cred.getCredential();

sts

Create a temporary security credential by applying Temporary Security Credentials (TSC) through the Security Token Service (STS).

import Credential, { Config } from '@alicloud/credentials';

const config: Config = {
  type:                  'sts',             // credential type
  accessKeyId:           'accessKeyId',     // AccessKeyId of your account
  accessKeySecret:       'accessKeySecret', // AccessKeySecret of your account
  securityToken:         'securityToken',   // Temporary Security Token
}
const cred = new Credential(config);
const {
  accessKeyId,
  accessKeySecret,
  securityToken,
  type
} = await cred.getCredential();

ram_role_arn

By specifying RAM Role, the credential will be able to automatically request maintenance of STS Token. If you want to limit the permissions(How to make a policy) of STS Token, you can assign value for Policy.

import Credential, { Config } from '@alicloud/credentials';

const config: Config = {
  type:                 'ram_role_arn',     // credential type
  accessKeyId:          'accessKeyId',      // AccessKeyId of your account
  accessKeySecret:      'accessKeySecret',  // AccessKeySecret of your account
  roleArn:              'roleArn',          // Format: acs:ram::USER_ID:role/ROLE_NAME
  roleSessionName:      'roleSessionName',  // Role Session Name
  policy:               'policy',           // Not required, limit the permissions of STS Token
  roleSessionExpiration: 3600,              // Not required, limit the Valid time of STS Token
}
const cred = new Credential(config);
const {
  accessKeyId,
  accessKeySecret,
  securityToken,
  type
} = await cred.getCredential();

oidc_role_arn

By specifying OIDC Role, the credential will be able to automatically request maintenance of STS Token. If you want to limit the permissions(How to make a policy) of STS Token, you can assign value for Policy.

import Credential, { Config } from '@alicloud/credentials';

const config: Config = {
  type:                   'oidc_role_arn',    // credential type
  roleArn:                'roleArn',          // Format: acs:ram::USER_Id:role/ROLE_NAME roleArn can be replaced by setting environment variable: ALIBABA_CLOUD_ROLE_ARN
  oidcProviderArn:        'oidcProviderArn',  // Format: acs:ram::USER_Id:oidc-provider/ROLE_NAME oidcProviderArn can be replaced by setting environment variable: ALIBABA_CLOUD_OIDC_PROVIDER_ARN
  oidcTokenFilePath:      '/Users/xxx/xxx',   // Format: path  OIDCTokenFilePath can be replaced by setting environment variable: ALIBABA_CLOUD_OIDC_TOKEN_FILE
  roleSessionName:        'roleSessionName',  // Role Session Name
  policy:                 'policy',           // Not required, limit the permissions of STS Token
  roleSessionExpiration:   3600,              // Not required, limit the Valid time of STS Token
}
const cred = new Credential(config);
const {
  accessKeyId,
  accessKeySecret,
  securityToken,
  type
} = await cred.getCredential();

ecs_ram_role

By specifying the role name, the credential will be able to automatically request maintenance of STS Token.

import Credential, { Config } from '@alicloud/credentials';
const config: Config = {
  type:                 'ecs_ram_role',       // credential type
  roleName:             'roleName',           // `roleName` is optional. It will be retrieved automatically if not set. It is highly recommended to set it up to reduce requests.
  disableIMDSv1:         true,                 // `disableIMDSv1` is optional and is recommended to be turned on. It can be replaced by setting environment variable: ALIBABA_CLOUD_IMDSV1_DISABLED
}
const cred = new Credential(config);
const {
  accessKeyId,
  accessKeySecret,
  securityToken,
  type
} = await cred.getCredential();

rsa_key_pair

By specifying the public key ID and the private key file, the credential will be able to automatically request maintenance of the AccessKey before sending the request. Only Japan station is supported.

import Credential, { Config } from '@alicloud/credentials';
const config: Config = {
  type:                     'rsa_key_pair',       // credential type
  privateKeyFile:           'privateKeyFile',     // The file path to store the PrivateKey
  publicKeyId:              'publicKeyId',        // PublicKeyId of your account
}
const cred = new Credential(config);
const {
  accessKeyId,
  accessKeySecret,
  securityToken,
  type
} = await cred.getCredential();

credentials_uri

By specifying a local or remote URI to get credentials and refresh automanticly.

import Credential, { Config } from '@alicloud/credentials';
const config: Config = {
  type: 'credentials_uri',
  credentialsURI: 'http://a_local_or_remote_address/'  //credentialsURI can be replaced by setting environment variable: ALIBABA_CLOUD_CREDENTIALS_URI
};
const cred = new Credential(config);
const {
  accessKeyId,
  accessKeySecret,
  securityToken,
  type
} = await cred.getCredential();

The URI must reponse meets following conditions:

  • response status code is 200
  • response body struct must be:
{
  "Code": "Success",
  "AccessKeySecret": "AccessKeySecret",
  "AccessKeyId": "AccessKeyId",
  "Expiration": "2021-09-26T03:46:38Z",
  "SecurityToken": "SecurityToken"
}

bearer

If credential is required by the Cloud Call Centre (CCC), please apply for Bearer Token maintenance by yourself.

import Credential, { Config } from '@alicloud/credentials';
const config: Config = {
  type:                 'bearer',            // credential type
  bearerToken:          'bearerToken',       // BearerToken of your account
}
const cred = new Credential(config);
let bearerToken: string = await cred.getBearerToken();
let type: string = cred.getType();

Provider

If you call new Credential() with empty, it will use provider chain to get credential for you.

1. Environment Credentials

The program first looks for environment credentials in the environment variable. If the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables are defined and are not empty, the program will use them to create the default credential. If not, the program loads and looks for the client in the configuration file.

2. Config File

If there is ~/.alibabacloud/credentials default file (Windows shows C:\Users\USER_NAME\.alibabacloud\credentials), the program will automatically create credential with the name of 'default'. The default file may not exist, but a parse error throws an exception. The specified files can also be loaded indefinitely: AlibabaCloud::load('/data/credentials', 'vfs://AlibabaCloud/credentials', ...); This configuration file can be shared between different projects and between different tools. Because it is outside the project and will not be accidentally committed to the version control. Environment variables can be used on Windows to refer to the home directory %UserProfile%. Unix-like systems can use the environment variable $HOME or ~ (tilde). The path to the default file can be modified by defining the ALIBABA_CLOUD_CREDENTIALS_FILE environment variable.

[default]                          # Default credential
type = access_key                  # Certification type: access_key
access_key_id = foo                # access key id
access_key_secret = bar            # access key secret

3. Instance RAM Role

If the environment variable ALIBABA_CLOUD_ECS_METADATA is defined and not empty, the program will take the value of the environment variable as the role name and request http://100.100.100.200/latest/meta-data/ram/security-credentials/ to get the temporary Security credential.

4. Credentials URI

If the environment variable ALIBABA_CLOUD_CREDENTIALS_URI is defined and not empty, the program will take the value of the environment variable as the credentials uri.

Test & Coverage

  • run test
npm run test
  • run code coverage
npm run cov

License

MIT

Copyright (c) 2009-present, Alibaba Cloud All rights reserved.

版本与发布记录

@alicloud/credentials 共发布 25 个版本,首次发布于 2019/03/01;过去 12 个月发布了 2 个版本。

版本发布日期距今
2.4.7latest2026/08/182 个月前
2.4.52026/06/094 个月前
2.4.42025/07/021.3 年前
2.4.32025/04/181.5 年前
2.4.2beta2025/03/071.7 年前
2.4.12025/03/071.7 年前

关于 @alicloud/credentials 的常见问题

@alicloud/credentials 是什么?

alibaba cloud node.js sdk credentials

如何安装 @alicloud/credentials?

在项目目录运行 npm install @alicloud/credentials,或使用 pnpm add / yarn add / bun add。

@alicloud/credentials 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@alicloud/credentials 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@alicloud/credentials 可以商用吗?

@alicloud/credentials 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@alicloud/credentials 的下载量有多少?

根据 npm 官方下载统计,@alicloud/credentials 在 2026/10/06 – 2026/11/04 这 30 天内被下载 581,033 次,日均约 19,368 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@alicloud/credentials 还在维护吗?

最新版本 2.4.7 发布于 2026/08/18(2 个月前);至今共发布 25 个版本,其中过去 12 个月发布了 2 个;首次发布于 2019/03/01。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。