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.
Getting Your API Key
-
Create an OpenAI Platform account
Go to platform.openai.com and sign up or log in.
-
Add billing information
Navigate to Settings > Billing and add a payment method. New accounts get free credits to start.
-
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.
-
Store securely
Save the key as an environment variable. Never hardcode API keys in source code or commit them to version control.
# 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 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
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
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.
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:
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 |
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
x-ratelimit-remaining-tokens response header to monitor your usage. The OpenAI Python library includes built-in retry logic.
Lilly Tech Systems