How to Get Your OpenAI API Key in 3 Steps?
You get an OpenAI API key by creating an account at platform.openai.com and generating a key under API Keys settings. You then pass it as a Bearer token in your request headers. This guide walks through every step, including a working Python example and how to keep your key secure.
To get an OpenAI API key, sign up at platform.openai.com, navigate to the API Keys section, and click 'Create new secret key.' You then use that key in your code by passing it as a Bearer token in the Authorization header of every API request.
What Is an OpenAI API Key and Why Do You Need One?
An OpenAI API key is a unique string of characters — like sk-proj-abc123... — that identifies your account when your code talks to OpenAI's servers. Think of it as a password your application presents automatically with every request, so OpenAI knows who is making the call, how much to charge, and whether to allow access.
Without an API key, OpenAI has no way to authenticate your request and will reject it with a 401 Unauthorized error. The key ties usage to your account, which means every token you generate gets tracked and billed to you. This is different from using ChatGPT in the browser — that interface handles authentication for you. When you write code that calls GPT-4o or another model directly, your API key is the credential that makes it work. Every developer building on top of OpenAI's models — whether a chatbot, a summarizer, or an image generator — starts exactly here.
Step-by-Step: How to Get Your OpenAI API Key and Make Your First Call
1. Go to platform.openai.com and sign up or log in. 2. Click your profile icon → select 'API Keys' from the left sidebar. 3. Click 'Create new secret key,' give it a name (e.g., 'my-first-project'), and copy it immediately — OpenAI only shows it once. 4. Add a payment method under 'Billing' so your requests don't fail.
Now use it in Python with the official openai package:
```python import os from openai import OpenAI
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
response = client.chat.completions.create( model="gpt-4o", messages=[{"role": "user", "content": "Explain APIs in one sentence."}] )
print(response.choices[0].message.content) ```
Or use curl to test immediately from your terminal:
```bash curl https://api.openai.com/v1/chat/completions \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "Content-Type: application/json" \ -d '{"model": "gpt-4o", "messages": [{"role": "user", "content": "Hello"}]}' ```
Install the library first with: pip install openai
How to Keep Your OpenAI API Key Secure and Control Costs
Your API key is as sensitive as a password — anyone who has it can make requests billed to your account. Follow these rules from day one:
**Never hardcode your key.** Store it in an environment variable (OPENAI_API_KEY) and read it with os.environ.get() as shown above. On Mac/Linux, run: export OPENAI_API_KEY='sk-...' in your terminal.
**Never commit it to Git.** Add a .env file to your .gitignore. Use python-dotenv locally to load it: from dotenv import load_dotenv; load_dotenv().
**Set usage limits.** In platform.openai.com under Billing → Usage Limits, set a monthly spending cap so a bug in your code can't generate a surprise bill.
**Use separate keys per project.** This way, if one key leaks, you revoke only that key without breaking every other project.
**Rotate keys if exposed.** If you accidentally push a key to GitHub, revoke it immediately in the API Keys dashboard and generate a new one. GitHub often detects leaked keys and alerts you, but don't rely on that.
Key Takeaways
- Your OpenAI API key is generated at platform.openai.com under API Keys — copy it immediately because it's only shown once.
- Pass the key as a Bearer token in the Authorization header of every API request, or let the official openai Python library handle this automatically.
- Always store your key in an environment variable, never directly in your source code.
- Set a monthly billing cap in your OpenAI account to prevent unexpected charges from runaway API calls.
- Create a separate API key for each project so you can revoke individual keys without disrupting your other applications.
FAQ
Q: Is the OpenAI API free to use?
A: OpenAI does not offer a permanently free API tier — you pay per token used, and you need a valid payment method to make calls. New accounts may receive a small free credit to experiment with, but check platform.openai.com for the current offer.
Q: What should I do if I accidentally pushed my API key to GitHub?
A: Go immediately to platform.openai.com → API Keys, find the exposed key, and click Delete to revoke it. Then generate a new key and update your project's environment variables — assume the old key was already compromised.
Q: Can I use my OpenAI API key in a frontend JavaScript app?
A: No — never expose your API key in client-side code, because anyone can view it in the browser. Route all OpenAI requests through your own backend server, which keeps the key hidden on the server side.
Conclusion
Getting and using an OpenAI API key takes less than five minutes: create an account at platform.openai.com, generate a key, and pass it as a Bearer token in your requests. The single most important next step after getting your key is storing it in an environment variable and setting a billing cap — these two habits protect both your security and your wallet from the start.
Related Posts
- What Is Answer Engine Optimization (AEO)? 2026 Guide
Answer Engine Optimization (AEO) is the practice of structuring content so AI-powered search engines select it as the authoritative answer. This 2026 guide covers the full AEO framework: from schema markup and entity optimization to content architecture that wins AI citations. - How Does Anthropic's OpenClaw API Pricing Work?
Anthropic has blocked Claude Pro and Max subscribers from using their flat-rate plans with third-party AI agent frameworks like OpenClaw, effective April 4, 2026. Developers who relied on subscription plans to power API-driven agents must now pay usage-based API costs on top of their subscription. T - How Do You Use Claude Code to Build Your First App?
You can build a real app with Claude Code even if you've never written a line of code. Just install it, describe your idea in plain English, and let Claude write, fix, and run the code for you. This guide walks you through every step.