Authorization: Bearer $AWS_BEARER_TOKEN_BEDROCK
作为支持的 SDK 中的参数
– 在设置客户端时在参数中指定值。例如,在使用
OpenAI Python SDK
设置客户端时,可以在
api_key
字段中指定密钥。
要查看使用 API 密钥发送
Converse
请求以生成响应的示例,请选择与首选方法对应的选项卡,然后按照以下步骤操作:
-
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"}]