OpenAI API Getting Started Guide
OpenAI API Getting Started: Your Complete Beginner Tutorial
The OpenAI API provides programmatic access to powerful language models like GPT-4o, GPT-4 Turbo, and GPT-3.5 Turbo. Whether you are building a chatbot, content generator, or code assistant, this guide walks you through every step from creating an account to making your first streaming API call.
Step 1: Create Your OpenAI Account
Visit platform.openai.com and sign up with your email, Google, or Microsoft account. After verifying your email address, you will need to add a payment method before you can generate API keys. OpenAI offers a free trial credit for new accounts, but availability varies by region.
Step 2: Generate Your API Key
Navigate to API Keys in your OpenAI dashboard. Click "Create new secret key", give it a descriptive name, and copy it immediately. The key starts with sk- and will only be shown once.
# Store your API key as an environment variable
export OPENAI_API_KEY="sk-your-api-key-here"
export OPENAI_BASE_URL="https://claude4u.com/v1"
Step 3: Install the SDK
Install the official OpenAI SDK for your preferred language:
# Python
pip install openai
# Node.js
npm install openai
Step 4: Make Your First API Call (Python)
Here is a minimal Python example that sends a prompt and prints the response:
from openai import OpenAI
client = OpenAI(
api_key="sk-your-key",
base_url="https://claude4u.com/v1"
)
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Explain quantum computing in simple terms."}
]
)
print(response.choices[0].message.content)
Step 5: Make Your First API Call (Node.js)
import OpenAI from 'openai';
const client = new OpenAI({
apiKey: 'sk-your-key',
baseURL: 'https://claude4u.com/v1'
});
const response = await client.chat.completions.create({
model: 'gpt-4o',
messages: [
{ role: 'system', content: 'You are a helpful assistant.' },
{ role: 'user', content: 'Explain quantum computing in simple terms.' }
]
});
console.log(response.choices[0].message.content);
Step 6: Enable Streaming Responses
Streaming returns tokens as they are generated, providing a much faster perceived response time. This is essential for real-time chat applications.
# Python streaming example
from openai import OpenAI
client = OpenAI(
api_key="sk-your-key",
base_url="https://claude4u.com/v1"
)
stream = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Write a short poem about AI."}],
stream=True
)
for chunk in stream:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="", flush=True)
Understanding the Response Object
Every non-streaming response includes these key fields:
- id - Unique identifier for the completion
- choices - Array of response candidates (usually one)
- usage - Token counts for prompt, completion, and total
- model - The actual model used for generation
- finish_reason - Why the model stopped: "stop", "length", or "tool_calls"
Common Parameters
- temperature (0-2) - Controls randomness. Lower values produce more deterministic output.
- max_tokens - Maximum tokens in the response. Set this to avoid unexpectedly long outputs.
- top_p - Nucleus sampling alternative to temperature. Use one or the other, not both.
- frequency_penalty - Reduces repetition of token sequences.
Next Steps
Now that you have made your first API call, explore advanced features like function calling, embeddings for search, and image generation with DALL-E. For production deployments, review our best practices guide to optimize cost, latency, and reliability.
Get Started with 轻舟 AI
Stable, fast AI API relay — supports Claude, OpenAI, Gemini and more
Sign Up Free
轻舟 AI