Adding your OpenAI API Key to System Environment Variables

In order to communicate with OpenAI, you need an account on openai.com and an API key that they provide.

  • Go to https://platform.openai.com

  • Create an account if you don’t have one

  • Click on the account dropdown in the top right corner

  • Go to View API keys

  • Click the button to “Create new secret key”

  • Save your secret key in your private notes

In this tutorial we will be saving the API key in our environment variables so that it doesn’t get saved in our source code. The benefit of this is that you will not accidentally share your secret key with the world if you put your code on GitHub.

On Windows:

  • Use the search bar in the Start menu to find “Edit the system environment variables”.

  • Click “Environment variables”

  • Use the upper “New…” button to add a User variable

  • Create a new variable called OPENAI_API_KEY and set the value to the secret key you got from your account settings on openai.com

For Mac or Linux:

  • Find the .bashrc, .bash_profile, or .zshrc in your home directory

  • Open the file in a text editor

  • Add a new line to the file:

    export OPENAI_API_KEY=<your secret key>