@aws/bedrock-token-generator

中文文档
1.1.0发布于 2025/07/25Apache-2.0TypeScriptCommonJS近 30 天下载 375.9万

A lightweight library for generating short-term bearer tokens for AWS Bedrock API authentication

原始使用文档
文档目录20 节
  1. Features
  2. Installation
  3. Quick Start
  4. Using Token Provider with Default Credential and Region Pro…
  5. Using Token Provider with Credential Provider and Region
  6. Using Token Provider with Specific Credentials, Region and …
  7. Using Stateless Function with Specific Credentials, Region …
  8. API Reference
  9. Token Format
  10. Security Considerations
  11. Requirements
  12. Development
  13. Setting up Development Environment
  14. Running Tests
  15. Contributing
  16. Development Workflow
  17. Support
  18. License
  19. Related Projects
  20. Changelog

AWS Bedrock Token Generator for JavaScript/TypeScript

npm version License TypeScript Node.js Version

A lightweight library for generating short-term bearer tokens for AWS Bedrock API authentication.

Features

  • ✅ Simple API: Async functions that generate bearer tokens
  • ✅ Secure: Uses AWS SigV4 signing with configurable token expiration (up to 12 hours)
  • ✅ AWS SDK Integration: Seamlessly works with AWS credential providers
  • ✅ Lightweight: Minimal dependencies, focused functionality
  • ✅ Well-tested: Comprehensive unit tests with multiple scenarios
  • ✅ TypeScript: Full type definitions for better IDE experience

Installation

npm install @aws/bedrock-token-generator

Quick Start

Using Token Provider with Default Credential and Region Provider

import { getTokenProvider } from "@aws/bedrock-token-generator";

// Create a token provider that uses default credentials and region providers.
// You can configure it to use other credential providers.
const provideToken = getTokenProvider();

async function example() {
    
  const token = await provideToken();

  // Use the token for API calls. The token has a default expiration of 12 hour.
  // If the expiresInSeconds parameter is specified during token creation, the 
  // expiration can be configured up to a maximum of 12 hours. However, the actual 
  // token validity period will always be the minimum of the requested expiration 
  // time and the AWS credentials' expiry time
  console.log(`Bearer Token: ${token}`);
}

Using Token Provider with Credential Provider and Region

You can find the supported credentials provider here.

import { getTokenProvider } from "@aws/bedrock-token-generator";
import { fromTemporaryCredentials } from "@aws-sdk/credential-providers";

const provideToken = getTokenProvider({
  credentials: fromTemporaryCredentials({
    params: {
      RoleArn: "arn:aws:iam::123456789012:role/BedrockRole",
    },
  }),
  region: "us-east-1",
});

async function example() {
    
  const token = await provideToken();

  // Use the token for API calls. The token has a default expiration of 12 hour.
  // If the expiresInSeconds parameter is specified during token creation, the 
  // expiration can be configured up to a maximum of 12 hours. However, the actual 
  // token validity period will always be the minimum of the requested expiration 
  // time and the AWS credentials' expiry time
  console.log(`Bearer Token: ${token}`);
}

Using Token Provider with Specific Credentials, Region and Expiry

import { getTokenProvider } from "@aws/bedrock-token-generator";

const credentials = {
  accessKeyId: "YOUR_ACCESS_KEY_ID",
  secretAccessKey: "YOUR_SECRET_ACCESS_KEY",
  sessionToken: "YOUR_SESSION_TOKEN",
};

const provideToken = getTokenProvider({
  credentials,
  region: "us-east-1",
  expiresInSeconds: 7200,
});

async function example() {
  const token = await provideToken();

  // Use the token for API calls. The token has an expiration of 2 hour. However, the actual token validity period
  // will always be the minimum of the requested expiration time and the AWS credentials' expiry time
  console.log(`Bearer Token: ${token}`);
}

Using Stateless Function with Specific Credentials, Region and Expiry

import { getToken } from "@aws/bedrock-token-generator";

async function example() {
  const credentials = {
    accessKeyId: "YOUR_ACCESS_KEY_ID",
    secretAccessKey: "YOUR_SECRET_ACCESS_KEY",
    sessionToken: "YOUR_SESSION_TOKEN",
  };

  const token = await getToken({
    credentials,
    region: "us-east-1",
    expiresInSeconds: 7200,
  });

  // Use the token for API calls. The token has an expiration of 2 hour. However, the actual token validity period
  // will always be the minimum of the requested expiration time and the AWS credentials' expiry time
  console.log(`Bearer Token: ${token}`);
}

API Reference

Token Format

The generated tokens follow this format:

bedrock-api-key-<base64-encoded-presigned-url>&Version=1
  • Prefix: bedrock-api-key- identifies the token type
  • Payload: Base64-encoded presigned URL with embedded credentials
  • Version: &Version=1 for future compatibility
  • Expiration: The token has a default expiration of 12 hour. If the expiresInSeconds parameter is specified during token creation, the expiration can be configured up to a maximum of 12 hours. However, the actual token validity period will always be the minimum of the requested expiration time and the AWS credentials' expiry time.

Security Considerations

  • Token Expiration: The token has a default expiration of 12 hour. If the expiresInSeconds parameter is specified during token creation, the expiration can be configured up to a maximum of 12 hours. However, the actual token validity period will always be the minimum of the requested expiration time and the AWS credentials' expiry time. The token must be generated again once it expires, as it cannot be refreshed or extended.
  • Secure Storage: Store tokens securely and avoid logging them
  • Credential Management: Use IAM roles and temporary credentials when possible
  • Network Security: Always use HTTPS when transmitting tokens
  • Principle of Least Privilege: Ensure underlying credentials have minimal required permissions

Requirements

  • Node.js: 16.0.0 or later
  • TypeScript: 4.7.0 or later (for TypeScript users)

Development

Setting up Development Environment

# Clone the repository
git clone https://github.com/aws/aws-bedrock-token-generator-js.git
cd aws-bedrock-token-generator-js

# Install dependencies
npm install

# Build the project
npm run build

# Run tests
npm test

Running Tests

# Run all tests
npm test

# Run tests in watch mode
npm run test:watch

# Run linter
npm run lint

# Format code
npm run format

Contributing

We welcome contributions! Please see CONTRIBUTING.md for details.

Development Workflow

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature-name
  3. Make changes and add tests
  4. Run tests: npm test
  5. Format code: npm run format
  6. Submit a pull request

Support

License

This project is licensed under the Apache License 2.0 - see the LICENSE file for details.

Changelog

See CHANGELOG.md for a list of changes and version history.

版本与发布记录

@aws/bedrock-token-generator 共发布 2 个版本,首次发布于 2025/07/01;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.1.0latest2025/07/251.3 年前
1.0.02025/07/011.4 年前

关于 @aws/bedrock-token-generator 的常见问题

@aws/bedrock-token-generator 是什么?

A lightweight library for generating short-term bearer tokens for AWS Bedrock API authentication

如何安装 @aws/bedrock-token-generator?

在项目目录运行 npm install @aws/bedrock-token-generator,或使用 pnpm add / yarn add / bun add。

@aws/bedrock-token-generator 支持 TypeScript 吗?

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

@aws/bedrock-token-generator 支持 ESM 还是 CommonJS?

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

@aws/bedrock-token-generator 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=16.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@aws/bedrock-token-generator 可以商用吗?

@aws/bedrock-token-generator 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@aws/bedrock-token-generator 的下载量有多少?

根据 npm 官方下载统计,@aws/bedrock-token-generator 在 2026/10/08 – 2026/11/06 这 30 天内被下载 3,759,317 次,日均约 125,311 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@aws/bedrock-token-generator 还在维护吗?

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