How to Get an OpenAI API Key (Quick Guide)

09 October 2025



OpenAI API keys allow developers to access OpenAI’s models, including GPT, for building AI-powered applications. In this guide, you’ll learn how to quickly generate your own API key and configure it safely for your projects.


Table of content:


1. What is an OpenAI API Key?
2. Creating an OpenAI Account
3. Set Up Billing
4. Generating Your API Key
5. Storing Your API Key Securely
6. Using the API Key in Your Project
7. Best Practices for API Key Security

1. What is an OpenAI API Key?

An OpenAI API key is a unique code that authenticates your requests to OpenAI’s services. It enables your applications to interact with models like GPT-3, GPT-4, and others for tasks such as text generation, chatbots, and AI tools.


2. Creating an OpenAI Account

Before you can get an API key, you need an OpenAI account. Follow these steps:

  • 1. Go to OpenAI Sign Up.
  • 2. Create an account using your email, Google, or Microsoft account.
  • 3. Verify your email address.

3. Set Up Billing

To use OpenAI’s API services beyond the free trial, you’ll need to add a payment method:

  • 1. Go to the Billing section from your account dashboard.
  • 2. Click Manage Account → Billing → Add Payment Method.
  • 3. Enter your credit or debit card details.
  • 4. Optionally, set spending limits to control your usage.

4. Generating Your API Key

Once your account is ready, follow these steps to generate your API key:

  1. 1. Login to your OpenAI dashboard: https://platform.openai.com/account/api-keys
  2. 2. Click the “Create new secret key” button.
  3. 3. Give your key a name for easy identification.
  4. 4. Copy the generated key as shown in Figure 1 and store it securely. This key will not be fully visible again.
  5. OpenAI API Key Example

    Figure 1


5. Storing Your API Key Securely

Never hardcode your API key directly into your frontend or public code. Instead:

  • 1. Use environment variables in a '.env' file for backend projects.
  • 2. For frontend apps, call your backend which then uses the API key.
  • 3. Limit the key permissions if possible and rotate keys periodically.

6. Using the API Key in Your Project

Example of using the API key with Node.js and the OpenAI SDK:


import { OpenAI } from "openai";
import dotenv from "dotenv";

dotenv.config();

const openai = new OpenAI({
  apiKey: process.env.OPENAI_API_KEY,
});

const response = await openai.chat.completions.create({
  model: "gpt-3.5-turbo",
  messages: [{ role: "user", content: "Hello AI!" }],
});

console.log(response.choices[0].message.content);
  

7. Best Practices for API Key Security

  • 1. Do not share your API key publicly.
  • 2. Use environment variables or secret managers.
  • 3. Rotate keys periodically for safety.
  • 4. Monitor usage in the OpenAI dashboard to detect unusual activity.

By following these steps, you can safely generate and use OpenAI API keys to integrate powerful AI capabilities into your projects.