OpenAI API Key Complete Guide
OpenAI API Key Management: Complete Guide to Security and Best Practices
Your OpenAI API key is the credential that authenticates every request you make. Proper key management is critical to preventing unauthorized usage, unexpected charges, and data breaches. This guide covers how to obtain, secure, rotate, and organize your API keys for both individual and team use.
How to Get Your OpenAI API Key
- Log in to platform.openai.com
- Navigate to Settings → API Keys
- Click "Create new secret key"
- Name the key descriptively (e.g., "production-backend", "dev-testing")
- Copy the key immediately — it is shown only once
API keys follow the format sk-proj-... for project-scoped keys or sk-... for legacy keys. Always use project-scoped keys for better access control.
Types of API Keys
- User Keys — Tied to your personal account. Use only for personal projects or testing.
- Project Keys — Scoped to a specific project within an organization. Recommended for all production use.
- Service Account Keys — For automated systems and CI/CD pipelines. Not tied to a human user.
Securing Your API Key
A leaked API key can result in thousands of dollars in unauthorized charges within hours. Follow these rules strictly:
Environment Variables (Recommended)
# .env file (add to .gitignore!)
OPENAI_API_KEY=sk-proj-your-key-here
OPENAI_BASE_URL=https://claude4u.com/v1
# Load in Python
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["OPENAI_API_KEY"],
base_url=os.environ.get("OPENAI_BASE_URL", "https://api.openai.com/v1")
)
// Load in Node.js
import OpenAI from 'openai';
const client = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
baseURL: process.env.OPENAI_BASE_URL || 'https://claude4u.com/v1'
});
Key Rotation Strategy
Regular key rotation limits the blast radius of a compromised key. Follow this schedule:
- Every 90 days — Rotate production keys
- Immediately — Rotate if you suspect any exposure
- On personnel changes — Rotate when team members leave
- After incidents — Rotate all keys involved in security events
Rotation process for zero-downtime deployments:
- Create a new API key in the dashboard
- Update your secrets manager or environment variables with the new key
- Deploy the updated configuration
- Verify the new key works in production
- Revoke the old key from the dashboard
Organization Key Management
For teams, OpenAI provides organization-level controls:
- Projects — Group related keys and set per-project spending limits
- Usage Limits — Set hard and soft spending caps per project
- Member Roles — Assign Owner, Member, or Reader permissions
- Audit Logs — Track who created, used, or revoked keys
Setting Usage Limits
Always configure spending limits to prevent runaway costs:
- Go to Settings → Limits in your OpenAI dashboard
- Set a soft limit for email notifications
- Set a hard limit to automatically block requests when exceeded
Using a Secrets Manager
For production systems, store keys in a dedicated secrets manager rather than plain environment variables:
# AWS Secrets Manager example
import boto3
import json
from openai import OpenAI
client_sm = boto3.client('secretsmanager')
secret = json.loads(
client_sm.get_secret_value(SecretId='openai/api-key')['SecretString']
)
client = OpenAI(
api_key=secret['api_key'],
base_url="https://claude4u.com/v1"
)
Troubleshooting Key Issues
- 401 Unauthorized — Key is invalid, expired, or revoked. Generate a new one.
- 429 Rate Limited — You have hit your rate or spending limit. Check your plan tier.
- Key not working after creation — Allow up to 60 seconds for propagation.
- Organization mismatch — Ensure the correct
Organizationheader is set when using multi-org accounts.
Get Started with 轻舟 AI
Stable, fast AI API relay — supports Claude, OpenAI, Gemini and more
Sign Up Free
轻舟 AI