API & Integration Intermediate

The OpenAI API lets you integrate ChatGPT models into your own applications. This lesson covers getting an API key, making your first API call, function calling, streaming responses, the Assistants API, pricing, and rate limits.

OpenAI API Overview

The OpenAI API provides programmatic access to GPT-4o, GPT-4o mini, o1, o3, DALL-E, Whisper, and other models. The primary endpoint for conversational AI is the Chat Completions API.

💡
API vs ChatGPT: The API is separate from the ChatGPT consumer product. You need an API key from platform.openai.com, and usage is billed separately based on token consumption.

Getting Your API Key

  1. Create an OpenAI Platform account

    Go to platform.openai.com and sign up or log in.

  2. Add billing information

    Navigate to Settings > Billing and add a payment method. New accounts get free credits to start.

  3. Generate an API key

    Go to API Keys, click "Create new secret key", give it a name, and copy the key immediately — you will not see it again.

  4. Store securely

    Save the key as an environment variable. Never hardcode API keys in source code or commit them to version control.

Environment Variable
# Add to your shell profile (.bashrc, .zshrc, etc.)
export OPENAI_API_KEY="sk-proj-your-api-key-here"

Chat Completions API

The Chat Completions endpoint accepts a list of messages and returns a model-generated response.

curl Example

curl
curl https://api.openai.com/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -d '{
    "model": "gpt-4o",
    "messages": [
      {"role": "system", "content": "You are a helpful assistant."},
      {"role": "user", "content": "Explain REST APIs in 3 sentences."}
    ],
    "temperature": 0.7,
    "max_tokens": 200
  }'

Python Example

Python
from openai import OpenAI

client = OpenAI()  # Uses OPENAI_API_KEY env var

response = client.chat.completions.create(
    model="gpt-4o",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Explain REST APIs in 3 sentences."}
    ],
    temperature=0.7,
    max_tokens=200
)

print(response.choices[0].message.content)

Node.js Example

JavaScript (Node.js)
import OpenAI from "openai";

const openai = new OpenAI(); // Uses OPENAI_API_KEY env var

const response = await openai.chat.completions.create({
  model: "gpt-4o",
  messages: [
    { role: "system", content: "You are a helpful assistant." },
    { role: "user", content: "Explain REST APIs in 3 sentences." }
  ],
  temperature: 0.7,
  max_tokens: 200
});

console.log(response.choices[0].message.content);

Function Calling / Tools

Function calling lets the model invoke external functions you define. The model decides when and how to call them based on the conversation context.

Python - Function Calling
response = client.chat.completions.create(
    model="gpt-4o",
    messages=[
        {"role": "user", "content": "What's the weather in San Francisco?"}
    ],
    tools=[{
        "type": "function",
        "function": {
            "name": "get_weather",
            "description": "Get current weather for a location",
            "parameters": {
                "type": "object",
                "properties": {
                    "location": {
                        "type": "string",
                        "description": "City and state"
                    }
                },
                "required": ["location"]
            }
        }
    }]
)

Streaming Responses

Stream responses token by token for a better user experience in real-time applications:

Python - Streaming
stream = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Write a haiku about coding"}],
    stream=True
)

for chunk in stream:
    if chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="")

Assistants API

The Assistants API provides a higher-level abstraction for building AI assistants with persistent threads, file handling, and tool use:

Feature Chat Completions Assistants API
State management Stateless (you manage history) Persistent threads (OpenAI manages history)
File handling Manual base64 encoding Built-in file upload and retrieval
Tools Function calling only Code Interpreter, File Search, Function calling
Best for Simple request/response, high control Complex assistants with file processing and multi-step tasks

API Pricing

OpenAI charges per token (roughly 4 characters). Pricing varies by model:

Model Input (per 1M tokens) Output (per 1M tokens)
GPT-4o $2.50 $10.00
GPT-4o mini $0.15 $0.60
o1 $15.00 $60.00
o3 $10.00 $40.00
GPT-4 Turbo $10.00 $30.00
Cost saving tip: Use GPT-4o mini for simple tasks (it is ~60x cheaper than GPT-4o). Reserve GPT-4o and reasoning models for tasks that truly need their capability. Set max_tokens to limit response length.

Rate Limits

OpenAI applies rate limits based on your usage tier. Limits increase automatically as you spend more:

  • Tier 1 (Free trial): 500 RPM, 30,000 TPM for GPT-4o
  • Tier 2 ($50+ spent): 5,000 RPM, 450,000 TPM
  • Tier 3 ($100+ spent): 5,000 RPM, 800,000 TPM
  • Tier 4 ($250+ spent): 10,000 RPM, 2,000,000 TPM
  • Tier 5 ($1,000+ spent): 10,000 RPM, 10,000,000 TPM
Rate limit handling: Implement exponential backoff retry logic in your application. Check the x-ratelimit-remaining-tokens response header to monitor your usage. The OpenAI Python library includes built-in retry logic.