* This blog post is a summary of this video.

Securely Storing and Accessing Your OpenAI API Key

Author: ParametricCampTime: 2024-01-29 08:25:00

Table of Contents

Introduction to OpenAI API Keys

Accessing OpenAI models like DALL-E 2 and GPT-3 requires an API key to authenticate. API keys allow you to make requests to the OpenAI API and billing is tied to the key. As such, API keys should be treated like passwords and handled with care.

This post will cover the process of getting an OpenAI API key, best practices for storing it securely, and how to access it from code.

Creating an OpenAI Account

The first step to getting an API key is to create an OpenAI account. You can sign up at openai.com. The sign up process is straightforward - just enter your email and create a password. Once your account is created, you'll automatically receive some free credits to start experimenting with the API. The exact amount of free credits varies based on when you sign up, but it's usually around $20.

Generating API Keys

After creating an account, you can generate API keys. In your OpenAI account, go to the API Keys page. Here you can create new keys. When you create a new key, copy it immediately. You'll only be able to view the key once, so don't lose it! If you do misplace a key, you can delete it and generate a new one. The API key works across all OpenAI products like DALL-E 2, GPT-3, and Codex. You only need one key for your account.

Key Access Restrictions

API keys are tied directly to your account and billing. Anyone who has your API key can make requests that will be charged to you. So it's important to store your key securely and be careful about who has access to it. Sharing your key publicly or on GitHub could lead to unwanted charges.

Locating Your API Key in OpenAI

Once you've generated an API key in your OpenAI account, you may need to reference it again later. To view your existing API keys:

  1. Go to openai.com and log in to your account

  2. Click on your profile picture in the top right and select "Manage my account"

  3. Choose "View API keys" on the left sidebar

Here you can see all the API keys associated with your account. You can also create new keys or delete old ones.

Remember that previously created keys can not be viewed again, only new keys will show the full key value.

Safely Storing API Keys

Avoid Hard Coding Keys

One of the worst things you can do is hard code your API key directly in source code files that you share publicly. This exposes your key and allows anyone to use it. For example, you may be tempted to store your key in a Python file like:

openai_key = "sk-************************************"

But this means anyone who views your code also has access to make unlimited requests with your key! So this should be avoided.

Using Environment Variables

A more secure way to manage your API key is using environment variables. These allow you to store the key externally and reference it from code. On Windows, you can set environment variables through the System Properties. Just create a new variable like "OPENAI_API_KEY" and set the value to your key. Then in your Python code, instead of hard coding the key you can do:

import os openai_key = os.getenv("OPENAI_API_KEY")

This keeps your key secure while still allowing access from code. Environment variables are the recommended way to work with API keys.

Accessing Keys from Code

Once you have safely stored your OpenAI API key using an environment variable, accessing it from code is straightforward.

In Python, you can use the os module to read environment variables:

import os openai_key = os.getenv("OPENAI_API_KEY")

In Javascript for Node.js, you can use process.env:

const openaiKey = process.env.OPENAI_API_KEY;

Most other languages and frameworks provide similar methods to read environment variables. The key is stored securely outside your actual code.

Next Steps After Getting API Key

Once you have an OpenAI API key, you're ready to start interacting with the models! Here are some next steps:

  • Try making API requests in the Playground to see responses from models like GPT-3

  • Build a simple script that calls the API and displays the output

  • Check the documentation for code examples in your language of choice

  • Review the billing page to keep track of your usage

  • Consider upgrading to a paid subscription plan for more features

The API opens up many possibilities - have fun exploring!


Getting an OpenAI API key unlocks the power of models like GPT-3, Codex, and DALL-E 2. Keys should be treated like passwords and stored securely using environment variables.

By following the best practices outlined here, you can safely access the OpenAI API from your applications.


Q: Do I need an OpenAI account to use API keys?
A: Yes, you need to create a free OpenAI account first before you can access any API keys for making predictions.

Q: Where can I view my OpenAI API keys?
A: You can view your existing API keys by going to the API Keys page under your account settings in the OpenAI dashboard.

Q: Can I reuse the same API key in multiple apps?
A: Yes, the OpenAI API key is global and can be used across any OpenAI apps like Dall-E, GPT-3 etc.

Q: Is it safe to hardcode my API key in code?
A: No, hardcoding the API key in code is very unsafe as it makes the key publicly visible if you share your code.

Q: What are environment variables?
A: Environment variables are dynamic named values stored externally that can be accessed by applications running on a machine.

Q: How do I access keys stored as environment variables?
A: You need to use language specific syntax like $env:KEY_NAME in PowerShell or os.environ['KEY'] in Python to retrieve keys stored as environment variables.

Q: Can I delete an OpenAI API key?
A: Yes, you can delete an API key anytime from your OpenAI account dashboard. But the key can never be retrieved again if lost.

Q: Will I get billed for unauthorized API key usage?
A: Yes, you will get billed for any predictions made using your OpenAI API key even by unauthorized parties.

Q: What's the next step after getting an API key?
A: The next step is to start making predictions by requesting the OpenAI models through the API using the key for authentication.

Q: Where can I learn more about OpenAI API?
A: Check the official OpenAI documentation and examples for comprehensive guides on using the different API capabilities.