Skip to content

How to Create an OpenAI API Key | Step-by-Step Guide

This comprehensive guide walks you through the process of creating and managing OpenAI API keys to access powerful AI models like GPT-4, GPT-3.5 Turbo, DALL-E, Whisper, and more.


An OpenAI API key is a unique authentication token that allows you to:

  • Access OpenAI’s AI models programmatically
  • Integrate GPT models into your applications
  • Use services like ChatGPT, DALL-E, and Whisper via API
  • Track usage and billing for your API requests

Before creating an API key, you need:

  1. OpenAI Account → Create one at https://platform.openai.com/signup
  2. Payment Method → Add a credit card for pay-as-you-go pricing
  3. Email Verification → Verify your email address

🔹 Step-by-Step: Creating an OpenAI API Key

Section titled “🔹 Step-by-Step: Creating an OpenAI API Key”
  1. Go to OpenAI Platform
  2. Click “Sign in” in the top-right corner
  3. Log in with your OpenAI account credentials
  1. Click on your profile icon (top-right corner)
  2. Select “View API keys” or go directly to https://platform.openai.com/account/api-keys
  1. Click ”+ Create new secret key” button
  2. Enter a descriptive name for your API key (e.g., “My Python App”, “Production Bot”)
  3. (Optional) Set permissions for the key:
    • All → Full access to all API endpoints
    • Restricted → Limited access to specific resources
  4. Click “Create secret key”

⚠️ IMPORTANT: The API key will only be displayed once. Make sure to:

  1. Copy the key immediately and store it securely
  2. Save it in a password manager or secure vault
  3. Never share your API key publicly or commit it to version control

Example API key format:

sk-proj-AbCdEfGhIjKlMnOpQrStUvWxYz1234567890AbCdEfGhIjKl

To view your existing API keys:

  1. Go to API Keys page
  2. You’ll see a list of all your API keys with:
    • Name → The label you gave it
    • Created → When it was created
    • Last used → Last activity timestamp
    • Actions → Delete option

⚠️ Note: For security reasons, you cannot view the full key after creation. If you lose it, you must delete and create a new one.


To prevent unexpected charges:

  1. Go to Usage Limits
  2. Set a monthly spending limit
  3. Enable email notifications for usage alerts
  4. Configure hard limits to automatically stop API access when reached

  • Store keys in environment variables (.env file)
  • Use secrets management tools (AWS Secrets Manager, Azure Key Vault, etc.)
  • Rotate keys regularly
  • Use different keys for development, staging, and production
  • Set appropriate usage limits
  • Hardcode API keys in source code
  • Commit keys to Git repositories
  • Share keys publicly in forums or chat
  • Use the same key across multiple projects
  • Embed keys in client-side applications

Section titled “Method 1: Environment Variable (Recommended)”

Linux/macOS:

Terminal window
export OPENAI_API_KEY="sk-proj-your-api-key-here"

Windows CMD:

Terminal window
set OPENAI_API_KEY="sk-proj-your-api-key-here"

Windows PowerShell:

Terminal window
$env:OPENAI_API_KEY="sk-proj-your-api-key-here"

Create a .env file in your project root:

OPENAI_API_KEY=sk-proj-your-api-key-here

Load it in your code:

Python:

from dotenv import load_dotenv
import os
load_dotenv()
api_key = os.getenv("OPENAI_API_KEY")

Node.js:

require('dotenv').config();
const apiKey = process.env.OPENAI_API_KEY;

import openai
import os
client = openai.OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "user", "content": "Hello, OpenAI!"}
]
)
print(response.choices[0].message.content)
Terminal window
curl https://api.openai.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"model": "gpt-3.5-turbo",
"messages": [{"role": "user", "content": "Hello!"}]
}'

  1. Go to API Keys page
  2. Find the key you want to remove
  3. Click the trash icon or “Revoke” button
  4. Confirm deletion

⚠️ Warning: Revoking a key will immediately stop all applications using it.


OpenAI uses pay-as-you-go pricing based on:

  • Model used (GPT-4 is more expensive than GPT-3.5)
  • Token count (input + output tokens)
  • Additional features (function calling, embeddings, etc.)

Check current pricing: https://openai.com/pricing


Solution:

  • Verify the key is copied correctly (no extra spaces)
  • Ensure the key hasn’t been revoked
  • Check that you’re using the right key for your project

Solution:

  • You’ve exceeded the requests per minute (RPM) limit
  • Implement exponential backoff in your code
  • Upgrade to a higher tier plan

Solution:

  • Your account has reached its spending limit
  • Add more credits or increase your usage limit
  • Check billing settings

OpenAI offers different key types:

TypeDescriptionUse Case
AllFull access to all resourcesProduction applications
RestrictedLimited to specific endpointsThird-party integrations
Service AccountFor automated systemsCI/CD pipelines

Track your API usage:

  1. Go to Usage Dashboard
  2. View metrics:
    • Requests per day
    • Token consumption
    • Cost breakdown
    • Model usage


You now know how to: ✅ Create an OpenAI API key
✅ Secure and manage your keys
✅ Set usage limits
✅ Test your API connection
✅ Troubleshoot common issues

Start building amazing AI-powered applications with OpenAI! 🎯