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.

Tip: If you are in a region where OpenAI access is restricted or you want a more reliable connection, use a relay service like claude4u.com which provides an OpenAI-compatible endpoint with no geographic restrictions.

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"
Warning: Never commit your API key to version control. Use environment variables or a secrets manager. If a key is exposed, rotate it immediately from your dashboard.

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:

Common Parameters

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.

Tip: Using claude4u.com as your base URL gives you access to multiple AI providers through a single OpenAI-compatible interface, making it easy to switch between models without changing your code.

Get Started with 轻舟 AI

Stable, fast AI API relay — supports Claude, OpenAI, Gemini and more

Sign Up Free