Hi. Can someone tell me how I can see my API Key? When I login to OpenAI to see it, it only shows me the "sk" and the last four characters. Is there a way to see the full API Key? Thank you.
Videos
What is the OpenAI API?
How does OpenAI API handle API key rotation?
How can I keep my OpenAI API key secure?
Hello @Rao, Vinod (CTR) HHHH
Thanks for reaching out to us, you can find the key by going to your resource in the Azure portal. The Endpoint and Keys can be found in the Resource Management section. Copy your endpoint and access key as you'll need both for authenticating your API calls. You can use either KEY1 or KEY2. Always having two keys allows you to securely rotate and regenerate keys without causing a service disruption.
Then you need to create and assign persistent environment variables for your key and endpoint before you run the line - openai.api_key = os.getenv("OPENAI_API_KEY") You need to save it to your Environment variables, please open your command line and run below, replace with your key value -
setx OPENAI_API_KEY "REPLACE_WITH_YOUR_KEY_VALUE_HERE"
You can also use PowerShell as below -
[System.Environment]::SetEnvironmentVariable('OPENAI_API_KEY', 'REPLACE_WITH_YOUR_KEY_VALUE_HERE', 'User')
Please let me know if you need more help, we are happy to help you further.
Regards,
Yutong
-Please kindly accept the answer and vote 'Yes' if you feel helpful to support the community, thanks a lot.
Hello!
Please check out the Python quickstart for details on retrieving and using your API keys for Azure OpenAI.
In short, once you've onboarded with Azure OpenAI and created an Azure OpenAI resource, you can copy one of the two keys from the "Keys and Endpoint" pane for the resource in Azure Portal and use that as the value for the environment variable.
The Python codes shown, accesses openai.Model, but this is no longer supported in openai>=1.0.0, see the v1.0.0 Migration Guide or README at https://github.com/openai/openai-python for the API.
Here is the adapted python code:
import openai
def check_openai_api_key(api_key):
client = openai.OpenAI(api_key=api_key)
try:
client.models.list()
except openai.AuthenticationError:
return False
else:
return True
OPENAI_API_KEY = "sk-7....."
if check_openai_api_key(OPENAI_API_KEY):
print("Valid OpenAI API key.")
else:
print("Invalid OpenAI API key.")
To check if your OpenAI API key is valid, you can try making a test API call and see if it works without any errors. Here's a simple code example:
import openai
openai.api_key = 'YOUR_API_KEY'
def is_api_key_valid():
try:
response = openai.Completion.create(
engine="davinci",
prompt="This is a test.",
max_tokens=5
)
except:
return False
else:
return True
# Check the validity of the API key
api_key_valid = is_api_key_valid()
print("API key is valid:", api_key_valid)
Replace 'YOUR_API_KEY' with your actual OpenAI API key. The is_api_key_valid function attempts a simple test API call. If the call succeeds without any errors, it means your API key is valid, and the function returns True. However, if any error occurs during the API call, it means the key is likely invalid, and the function returns False.
By running the code and checking the value of api_key_valid, you can determine if your OpenAI API key is valid.