Use an Amazon Bedrock API key
You can use your Amazon Bedrock API key in the following ways:
-
Set it as environment variable – The Amazon Bedrock service recognizes the environment variable
AWS_BEARER_TOKEN_BEDROCKYou have the following options to set the key:-
Open a terminal to set it:
-
MacOS/Linux
export AWS_BEARER_TOKEN_BEDROCK=${api-key} -
Windows
setx AWS_BEARER_TOKEN_BEDROCK "${api-key}"
-
-
Set it as an environment variable in your code before you make the API request. For example, you could include the following lines before making the request:
-
Python
import os os.environ['AWS_BEARER_TOKEN_BEDROCK'] = "${api-key}"
-
-
-
Specify it in a request – You can include the Amazon Bedrock API key in the authorization header in the following ways (replace
$AWS_BEARER_TOKEN_BEDROCKwith the actual value):-
In a direct HTTP request – Include the following as an authorization header:
Authorization: Bearer$AWS_BEARER_TOKEN_BEDROCK -
As a parameter in a supported SDK – Specify the value in the parameter when setting up the client. For example, you can specify it in the
api_keyfield when setting up a client with the OpenAI Python SDK.
-
Note
Amazon Bedrock API keys are limited to Amazon Bedrock and Amazon Bedrock Runtime actions. You can't use them with the following API operations:
-
Agents for Amazon Bedrock or Agents for Amazon Bedrock Runtime API operations.
-
Data Automation for Amazon Bedrock or Runtime for Amazon Bedrock Data Automation API operations.
To see an example of using the API key to send a Converse request to generate a response, choose the tab for your preferred method, and then follow the steps: