-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating

ChatGPT for Cybersecurity Cookbook
By :

In this recipe, we will show you how to set up your OpenAI API key as an environment variable. This is an essential step as it allows you to use the API key in your Python code without hardcoding it, which is a best practice for security purposes.
Ensure that you have already obtained your OpenAI API key by signing up for an account and accessing the API key section, as outlined in the Creating an API key and interacting with OpenAI recipe.
This example will demonstrate how to set up your OpenAI API key as an environment variable for secure access in your Python code. Let’s dive into the steps to achieve this.
Environment Variables
, and click Edit the system environment variables.OPENAI_API_KEY
as the variable’s name and paste your API key as the variable value. Click OK to save the new environment variable..bashrc
, .zshrc
, or .profile
) by running the following command (replace your_api_key
with your actual API key):echo 'export OPENAI_API_KEY="your_api_key"' >> ~/.bashrc
Tip
If you are using a different shell configuration file, replace ~/.bashrc
with the appropriate file (for example, ., ~/.zshrc
or ~/.profile
).
source ~/.bashrc
(or the appropriate configuration file) to apply the changes.os
module:import os # Access the OpenAI API key from the environment variable api_key = os.environ["OPENAI_API_KEY"]
Important note
There are many different versions of Linux and Unix-based systems, and the exact syntax for setting environment variables might differ slightly from what is presented here. However, the general approach should be similar. If you encounter issues, consult the documentation specific to your system for guidance on setting environment variables.
By setting up the OpenAI API key as an environment variable, you make it available for use in your Python code without hardcoding the key, which is a security best practice. In the Python code, you use the os
module to access the API key from the environment variable you created earlier.
Using environment variables is a common practice when working with sensitive data, such as API keys or other credentials. This approach allows you to separate your code from your sensitive data and makes it easier to manage your credentials as you only need to update them in one place (the environment variables). Additionally, it helps prevent accidental exposure of sensitive information when you’re sharing code with others or publishing it in public repositories.
In some cases, you may want to use a Python package such as python-dotenv
to manage your environment variables. This package allows you to store your environment variables in a .env
file, which you can load in your Python code. The advantage of this approach is that you can keep all your project-specific environment variables in a single file, making it easier to manage and share your project settings. Keep in mind, though, that you should never commit the .env
file to a public repository; always include it in your .gitignore
file or similar version control ignore configuration.