

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 使用 Amazon Bedrock API 密钥
<a name="api-keys-use"></a>

您可以通过以下方式使用 Amazon Bedrock API 密钥：
+ **设置为环境变量** – Amazon Bedrock 服务可识别环境变量 `AWS_BEARER_TOKEN_BEDROCK`。您可以通过以下选项来设置密钥：
  + 打开终端进行设置：
    + **MacOS/Linux**

      ```
      export AWS_BEARER_TOKEN_BEDROCK={{${api-key}}}
      ```
    + **Windows**

      ```
      setx AWS_BEARER_TOKEN_BEDROCK "{{${api-key}}}"
      ```
  + 在发出 API 请求之前，将密钥设置为代码中的环境变量。例如，您可以在发出请求之前包含以下几行：
    + **Python**

      ```
      import os                      
      os.environ['AWS_BEARER_TOKEN_BEDROCK'] = "{{${api-key}}}"
      ```
+ 在@@ **请求中指定** — 您可以通过以下方式将 Amazon Bedrock API 密钥包含在授权标题中（{{$AWS\_BEARER\_TOKEN\_BEDROCK}}替换为实际值）：
  + **在直接 HTTP 请求中** – 包括以下内容作为授权标头：

    ```
    Authorization: Bearer {{$AWS_BEARER_TOKEN_BEDROCK}}
    ```
  + **作为支持的 SDK 中的参数** – 在设置客户端时在参数中指定值。例如，在使用 [OpenAI Python SDK](https://github.com/openai/openai-python?tab=readme-ov-file#usage) 设置客户端时，可以在 `api_key` 字段中指定密钥。

**注意**  
Amazon Bedrock API 密钥仅限于 [Amazon Bedrock](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_Operations_Amazon_Bedrock.html) 和 [Amazon Bedrock 运行时](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_Operations_Amazon_Bedrock_Runtime.html)操作。您无法将这样的密钥与以下 API 操作结合使用：  
[InvokeModelWithBidirectionalStream](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InvokeModelWithBidirectionalStream.html).
[Amazon Bedrock 代理](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_Operations_Agents_for_Amazon_Bedrock.html)或 [Amazon Bedrock 代理运行时](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_Operations_Agents_for_Amazon_Bedrock.html) API 操作。
[Amazon Bedrock 数据自动化](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_Operations_Data_Automation_for_Amazon_Bedrock.html)或 [Amazon Bedrock 数据自动化运行时](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_Operations_Runtime_for_Amazon_Bedrock_Data_Automation) API 操作。

要查看使用 API 密钥发送 [Converse](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_Converse.html) 请求以生成响应的示例，请选择与首选方法对应的选项卡，然后按照以下步骤操作：

------
#### [ Python ]

以下示例说明如何通过 适用于 Python (Boto3) 的 AWS SDK发送 API 请求。如果您尚未将 API 密钥设置为`AWS_BEARER_TOKEN_BEDROCK`环境变量，请在以下代码{{${api-key}}}中将其替换为：

```
import os
import boto3
                        
# If you already set the API key as an environment variable, you can comment this line out                        
os.environ['AWS_BEARER_TOKEN_BEDROCK'] = "{{${api-key}}}"

# Create an Amazon Bedrock client
client = boto3.client(
    service_name="bedrock-runtime",
    region_name="us-east-1" # If you've configured a default region, you can omit this line
)

# Define the model and message
model_id = "us.anthropic.claude-sonnet-4-6"
messages = [{"role": "user", "content": [{"text": "Hello"}]}]

response = client.converse(
    modelId=model_id,
    messages=messages,
)
```

------
#### [ HTTP Client (requests package in Python) ]

**先决条件：**通过打开终端并运行以下命令来安装 `requests` 软件包：

```
python3 -m pip install requests
```

以下示例说明如何通过 HTTP 客户端直接发送 API 请求。在标题{{${api-key}}}中指定。

```
import requests

url = "https://bedrock-runtime.us-east-1.amazonaws.com/model/us.anthropic.claude-sonnet-4-6/converse"

payload = {
    "messages": [
        {
            "role": "user",
            "content": [{"text": "Hello"}]
        }
    ]
}

headers = {
    "Content-Type": "application/json",
    "Authorization": "Bearer {{${api-key}}}"
}

response = requests.request("POST", url, json=payload, headers=headers)

print(response.text)
```

------
#### [ HTTP request using cURL ]

以下示例说明如何使用 cURL 直接发送 API 请求。如果您没有将 API 密钥设置为 AWS\_BEARER\_TOKEN \_BEDROCK 环境变量，则必须将示例`$AWS_BEARER_TOKEN_BEDROCK`中的密钥替换为密钥的字面值。

```
curl -X POST "https://bedrock-runtime.us-east-1.amazonaws.com/model/us.anthropic.claude-sonnet-4-6/converse" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $AWS_BEARER_TOKEN_BEDROCK" \
  -d '{
    "messages": [
        {
            "role": "user",
            "content": [{"text": "Hello"}]
        }
    ]
  }'
```

------