How to use OpenAI API — a practical guide

If you're here, you probably want working code in five minutes. That's exactly what this guide does.

Five steps to get started

  1. 1
    Register at platform.openai.com and load $5–10 in credit.
  2. 2
    Create an API key (sk-…).
  3. 3
    Install the SDK: `npm install openai`.
  4. 4
    Call chat.completions.create with a messages array.

Shortest snippet that will actually run

import OpenAI from "openai";

const client = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });

const response = await client.chat.completions.create({
  model: "gpt-4o-mini",
  messages: [
    { role: "system", content: "אתה עוזר בעברית." },
    { role: "user", content: "מה מזג האוויר בתל אביב?" },
  ],
});

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

Common mistakes (the honest list)

Set temperature=0 for deterministic tasks (parsing, classification).
Use streaming for a live UX.
Add your own rate limiter — prevents a bug from burning credit.