OpenAI Node SDK API Cheat Sheet
This quick reference guide summarizes the most common patterns and methods when using the official OpenAI Node.js library.
1. Chat Completion API
const completion = await openai.chat.completions.create({
model: "gpt-4o-mini",
messages: [{ role: "user", content: "Hello" }],
});
const reply = completion.choices[0].message.content;2. Text Embeddings Generation
Convert text into floating-point vector arrays (e.g. for Vector Search or RAG):
const response = await openai.embeddings.create({
model: "text-embedding-3-small",
input: "Next.js routing tutorials",
});
const vector = response.data[0].embedding; // 1536-dimensional array3. Image Generation (DALL-E 3)
const response = await openai.images.generate({
model: "dall-e-3",
prompt: "A neon code editor terminal screen, vector design style.",
n: 1,
size: "1024x1024",
});
const imageUrl = response.data[0].url;4. API Error Troubleshooting Reference
When calling endpoints, wrap execution inside try-catch blocks and match these status codes:
| Code | Error Category | Root Cause and Solution |
401 | Unauthorized | Your API key is incorrect or deactivated. Verify environmental variables setup. |
429 | Rate Limit / Quota | You sent requests too fast or exhausted your billing balance. Check billing limits. |
500 | Server Error | OpenAI servers faced internal issues. Implement retry mechanisms. |
503 | Service Unavailable | OpenAI servers are overloaded. Apply exponential backoff delays. |
Published on Last updated: