OpenAI APIs: From ChatGPT to Image Generatio
I wanted to understand what OpenAI's APIs could actually do. Not the marketing -- the real capabilities, the costs, and the gotchas.
30 Dec 2023

I wanted to understand what OpenAI's APIs could actually do. Not the marketing -- the real capabilities, the costs, and the gotchas.
The setup turned out to be simple. Here's how to get started.
Installation
npm init
npm install openai --save
Chat completions
The core API. You send messages, you get responses. Here's the minimal version:
import OpenAI from "openai"
const openai = new OpenAI()
const response = await openai.chat.completions.create({
model: "gpt-4",
messages: [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: "Explain recursion in one sentence." }
]
})
console.log(response.choices[0].message.content)
The messages array is a conversation history. The system message sets behavior. The user message is the prompt. You can add assistant messages to provide context from previous turns.
The cost model: You pay per token. Input tokens (your prompt) and output tokens (the response) are priced separately. Longer conversations cost more because you resend the entire history each time.
Image generation
OpenAI's DALL-E API generates images from text descriptions:
const image = await openai.images.generate({
model: "dall-e-3",
prompt: "A minimalist logo for a developer blog, dark background, geometric shapes",
n: 1,
size: "1024x1024"
})
console.log(image.data[0].url)
You describe what you want, and the API returns a URL to the generated image. Image generation is priced per image, not per token.
Trade-offs
The benefit: Getting from zero to a working AI feature takes minutes, not months. The API abstracts away model training, infrastructure, and scaling.
The cost: You're paying per request, you're sending data to OpenAI's servers, and you're dependent on their uptime and pricing decisions. For high-volume production use, costs add up fast.
What to watch for: Rate limits, response latency under load, and the fact that model behavior can change between versions. Pin your model version in production.