OpenAI API Beginner: Your First GPT-5.6 Call Explained
From account creation and API Key retrieval to your first Responses API call in Python and Node.js — a complete beginner's path to the OpenAI API.
How to
Sign up and create an API key
Register on platform.openai.com, verify your phone, add a card and top up at least $5 in Billing. Then open API Keys, create a new secret key, and copy it immediately — you will not see it again.
Put the API key in an environment variable
On macOS / Linux run export OPENAI_API_KEY="sk-...". On Windows PowerShell run $env:OPENAI_API_KEY="sk-...". Or write it to a project-root .env file and add .env to .gitignore.
Install the Python SDK and make your first Responses call
Run pip install openai. Then call client.responses.create(model="gpt-5.6", input=..., reasoning={"effort": "medium"}, max_output_tokens=200) and print response.output_text.
Run the same call from Node.js
Run npm install openai, instantiate new OpenAI(), and await client.responses.create({...}). Console.log response.output_text.
Troubleshoot the three most common errors
401 invalid_api_key: check OPENAI_API_KEY. 429 rate_limit_exceeded: add exponential-backoff retries or switch to Luna. 400 invalid_request_error: usually means you passed messages to the Responses API (use input instead).
OpenAI API Beginner: Your First GPT-5.6 Call Explained
The OpenAI API is the most direct way to integrate GPT models into your own product. This guide walks you from zero to your first call.
1. Create an OpenAI Account
- Visit platform.openai.com
- Sign up with email or Google
- Verify your phone number (note: mainland China numbers may face restrictions)
- Add a payment method (credit card)
- Top up your Billing page (minimum $5)
2. Get an API Key
Path: API Keys → Create new secret key → name it (e.g. my-app-dev) → create.
⚠️ The key is shown only once. Copy it immediately and store it somewhere safe.
3. Set Up Environment Variables
Never hardcode the API key. Use environment variables:
macOS / Linux:
export OPENAI_API_KEY="sk-..."
Windows PowerShell:
$env:OPENAI_API_KEY="sk-..."
.env file (project root):
OPENAI_API_KEY=sk-...
Add .env to .gitignore.
4. Python — Your First Call
Install the SDK:
pip install openai
Sample code (Responses API):
from openai import OpenAI
client = OpenAI() # auto-reads OPENAI_API_KEY from env
response = client.responses.create(
model="gpt-5.6",
input=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Explain what ChatGPT is in one sentence."},
],
reasoning={"effort": "medium"},
max_output_tokens=200,
)
print(response.output_text)
print(f"Tokens used: {response.usage.total_tokens}")
5. Node.js — Your First Call
Install the SDK:
npm install openai
Sample code:
import OpenAI from 'openai';
const client = new OpenAI(); // auto-reads process.env.OPENAI_API_KEY
const response = await client.responses.create({
model: 'gpt-5.6',
input: [
{ role: 'system', content: 'You are a helpful assistant.' },
{ role: 'user', content: 'Explain what ChatGPT is in one sentence.' },
],
reasoning: { effort: 'medium' },
max_output_tokens: 200,
});
console.log(response.output_text);
console.log('Tokens used:', response.usage?.total_tokens);
6. Multi-turn Conversation
Pass the whole conversation as the input array:
response = client.responses.create(
model="gpt-5.6",
input=[
{"role": "system", "content": "You are a Python mentor"},
{"role": "user", "content": "What is a decorator?"},
{"role": "assistant", "content": "A decorator is..."},
{"role": "user", "content": "Can you give me an example?"},
],
)
Or just pass the previous response ID and let the API stitch the history:
response = client.responses.create(
model="gpt-5.6",
input="What is a decorator?",
previous_response_id=prev_id,
)
7. Streaming Response (SSE)
Show tokens as they arrive for a smoother UX:
stream = client.responses.create(
model="gpt-5.6",
input=messages,
stream=True,
)
for event in stream:
if event.type == "response.output_text.delta":
print(event.delta, end="")
8. Tools and Function Calling
The Responses API mounts tools via the tools field: Web search, file search, computer use, Code Interpreter, and custom functions.
response = client.responses.create(
model="gpt-5.6",
input="What's the weather in Beijing today?",
tools=[{"type": "web_search"}],
)
9. Error Troubleshooting
- 401 invalid_api_key — check the API Key
- 429 rate_limit_exceeded — back off and add retries, or switch to Luna
- 402 insufficient_quota — top up your account
- 400 invalid_request_error — common cause: passing
messagesto the Responses API (useinputinstead) - timeout — network problems; retry or change region
10. Cost Optimisation
- Use Luna for high-throughput batch jobs
- Enable Prompt Caching for repeated prefixes
- Compress system prompts
- Cap output with
max_output_tokens - Pre-summarise long inputs
- Set
reasoning.effort = "none"for trivial tasks
11. Migrating from Chat Completions
If you have existing Chat Completions code, the move to Responses API is usually a few lines:
- response = client.chat.completions.create(
- model="gpt-4o",
- messages=[{"role": "user", "content": "..."}],
+ response = client.responses.create(
+ model="gpt-5.6",
+ input=[{"role": "user", "content": "..."}],
)
- text = response.choices[0].message.content
+ text = response.output_text
Chat Completions still works but is no longer gaining new features. New projects should use the Responses API directly.
12. Next Steps
Once you are comfortable here, continue with:
- The complete guide to GPT models: GPT-5.6 Sol, Terra, Luna
- Prompt Engineering Core Patterns: 8 Templates That 2x GPT Output
- Realtime Voice guide: gpt-realtime and Voice Mode
Key points
- The API Key is your credential — manage it via environment variables and never commit it to Git
- The Responses API is the current recommended endpoint — pass an `input` array, get back `output_text` and `usage`
- GPT-5.6 Terra is the workhorse at $2.50 / $15 per 1M tokens; Sol flagship at $5 / $30; Luna low-cost at $1 / $6
- Control depth with `reasoning.effort`: none / low / medium / high / xhigh / max
- OpenAI ships official Python (`openai`) and Node.js (`openai-node`) SDKs
- Chat Completions is now in legacy status; new projects should use the Responses API
❓ Frequently asked questions
Official references
Subscribe to GPTMap Weekly
One email every Monday: curated OpenAI updates, deep dives, and best practices. No ads, unsubscribe anytime.