Install the SDK
```bash
npm install openai
```
Your First Chat Completion
```javascript
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: "user", content: "Explain async/await in JavaScript" }],
});
console.log(response.choices[0].message.content);
```
Model Selection
- **gpt-4o-mini** — Cheap, fast, handles most tasks well
- **gpt-4o** — Best multimodal performance, vision support
- **gpt-4.5** — Frontier performance for complex tasks
- **o1/o3** — Extended reasoning for math and science
Function Calling
Structured JSON function calling makes tool integrations reliable:
```javascript
const tools = [{ type: "function", function: { name: "get_weather", parameters: {...} }}];
const result = await client.chat.completions.create({ model: "gpt-4o", messages, tools });
```
Free Tier
$5 free credit on new accounts. API pricing is per-token — use gpt-4o-mini for development.