OpenAI Node.js & Python API Cheatsheet 2026
Syntaxes and patterns for OpenAI completions, structured outputs, chat streaming, image generation, and model parameters.
Initialization
When to Use
When calling OpenAI's endpoint engines to generate chat completions, structured JSON, embeddings, or visual artwork.
Common Mistakes
Exposing your private OpenAI API key to client-side browsers instead of proxying calls via an Express backend.
Shortcut / Pro-Tip
Enable 'response_format: { type: "json_object" }' to safely enforce structural JSON parsing.Example
import OpenAI from 'openai'; const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });Output Example
{
"id": "chatcmpl-123",
"object": "chat.completion",
"choices": [
{
"message": {
"role": "assistant",
"content": "Hello! How can I help you today?"
}
}
]
}Chat Completion
When to Use
When calling OpenAI's endpoint engines to generate chat completions, structured JSON, embeddings, or visual artwork.
Common Mistakes
Exposing your private OpenAI API key to client-side browsers instead of proxying calls via an Express backend.
Shortcut / Pro-Tip
Enable 'response_format: { type: "json_object" }' to safely enforce structural JSON parsing.Example
const response = await openai.chat.completions.create({ model: 'gpt-4o', messages: [{ role: 'user', content: 'Hello' }] });Output Example
{
"id": "chatcmpl-123",
"object": "chat.completion",
"choices": [
{
"message": {
"role": "assistant",
"content": "Hello! How can I help you today?"
}
}
]
}When to Use
When calling OpenAI's endpoint engines to generate chat completions, structured JSON, embeddings, or visual artwork.
Common Mistakes
Exposing your private OpenAI API key to client-side browsers instead of proxying calls via an Express backend.
Shortcut / Pro-Tip
Enable 'response_format: { type: "json_object" }' to safely enforce structural JSON parsing.Example
temperature: 0.2, max_completion_tokens: 500Output Example
{
"id": "chatcmpl-123",
"object": "chat.completion",
"choices": [
{
"message": {
"role": "assistant",
"content": "Hello! How can I help you today?"
}
}
]
}Streaming
When to Use
When engineering conversational user interfaces with immediate, word-by-word streaming rendering.
Common Mistakes
Failing to check if the chunk has choices/deltas before writing content stream segments.
Shortcut / Pro-Tip
Use 'for await...of' loops to iterate naturally through the asynchronous token chunks.Example
const stream = await openai.chat.completions.create({ model: 'gpt-4o', messages: [...], stream: true });Output Example
{
"id": "chatcmpl-123",
"object": "chat.completion",
"choices": [
{
"message": {
"role": "assistant",
"content": "Hello! How can I help you today?"
}
}
]
}When to Use
When engineering conversational user interfaces with immediate, word-by-word streaming rendering.
Common Mistakes
Failing to check if the chunk has choices/deltas before writing content stream segments.
Shortcut / Pro-Tip
Use 'for await...of' loops to iterate naturally through the asynchronous token chunks.Example
for await (const chunk of stream) { process.stdout.write(chunk.choices[0]?.delta?.content || ''); }Output Example
{
"id": "chatcmpl-123",
"object": "chat.completion",
"choices": [
{
"message": {
"role": "assistant",
"content": "Hello! How can I help you today?"
}
}
]
}Structured Outputs
When to Use
When calling OpenAI's endpoint engines to generate chat completions, structured JSON, embeddings, or visual artwork.
Common Mistakes
Exposing your private OpenAI API key to client-side browsers instead of proxying calls via an Express backend.
Shortcut / Pro-Tip
Enable 'response_format: { type: "json_object" }' to safely enforce structural JSON parsing.Example
response_format: { type: 'json_object' }Output Example
{
"id": "chatcmpl-123",
"object": "chat.completion",
"choices": [
{
"message": {
"role": "assistant",
"content": "Hello! How can I help you today?"
}
}
]
}When to Use
When calling OpenAI's endpoint engines to generate chat completions, structured JSON, embeddings, or visual artwork.
Common Mistakes
Exposing your private OpenAI API key to client-side browsers instead of proxying calls via an Express backend.
Shortcut / Pro-Tip
Enable 'response_format: { type: "json_object" }' to safely enforce structural JSON parsing.Example
response_format: zodResponseFormat(MySchema, 'result')Output Example
{
"id": "chatcmpl-123",
"object": "chat.completion",
"choices": [
{
"message": {
"role": "assistant",
"content": "Hello! How can I help you today?"
}
}
]
}Image & Embeddings
When to Use
When calling OpenAI's endpoint engines to generate chat completions, structured JSON, embeddings, or visual artwork.
Common Mistakes
Exposing your private OpenAI API key to client-side browsers instead of proxying calls via an Express backend.
Shortcut / Pro-Tip
Enable 'response_format: { type: "json_object" }' to safely enforce structural JSON parsing.Example
const image = await openai.images.generate({ model: 'dall-e-3', prompt: 'a cute cat' });Output Example
{
"id": "chatcmpl-123",
"object": "chat.completion",
"choices": [
{
"message": {
"role": "assistant",
"content": "Hello! How can I help you today?"
}
}
]
}When to Use
When calling OpenAI's endpoint engines to generate chat completions, structured JSON, embeddings, or visual artwork.
Common Mistakes
Exposing your private OpenAI API key to client-side browsers instead of proxying calls via an Express backend.
Shortcut / Pro-Tip
Enable 'response_format: { type: "json_object" }' to safely enforce structural JSON parsing.Example
const embedding = await openai.embeddings.create({ model: 'text-embedding-3-small', input: 'text' });Output Example
{
"id": "chatcmpl-123",
"object": "chat.completion",
"choices": [
{
"message": {
"role": "assistant",
"content": "Hello! How can I help you today?"
}
}
]
}OpenAI API Best Practices
1Use Server-Side Proxies for Keys
Never place your OpenAI API key in client-side code. Always proxy requests through your Express or Node.js server.
2Enforce Structured JSON Outputs
Set the response_format parameter to JSON Object, or leverage Structured Outputs with Zod schemas to ensure perfect parsing compatibility.
3Implement Exponential Backoff Retry Loops
Handle rate limit errors (HTTP 429) gracefully using standard exponential backoff delay retries.
4Keep Track of Token Consumptions
Store and log the usage metrics returned in the API response payload to monitor and budget application costs.
5Leverage System Fingerprints for Cache Verification
Inspect system_fingerprint values to check if backend model updates are altering generation determinism.
Common OpenAI API Errors & Solutions
401 Invalid Authentication
The API key is incorrect or missing. Check that process.env.OPENAI_API_KEY is defined and loaded.
429 Rate Limit Exceeded
You have exceeded your request rate or token limits. Implement throttling, use backoff retries, or upgrade your billing tier.
400 Invalid JSON payload
Your request body contains malformed JSON or invalid parameter structures. Validate schemas against OpenAI API documentation.
Context Length Exceeded
The combined prompt and max_tokens parameters exceed the model's context window. Prune input history length.
API connection timeout
The network request to api.openai.com timed out. Increase timeout values in your HTTP client library.
Common OpenAI API Interview Questions
Q1What is the difference between the Chat Completions and Assistants APIs?
Chat Completions is a stateless API for generating text responses from a list of messages. The Assistants API is stateful, maintaining user threads automatically, executing tools (like code interpreter), and reading files.
Q2What are temperature and top_p, and how do they differ?
Both control response creativity. Temperature scales the logits before softmax (higher = more creative). Top_p (nucleus sampling) limits choices to the top cumulative probability fraction (higher = more diverse). Only adjust one at a time.
Q3How do you ensure structured JSON output from OpenAI?
You can set response_format to { type: 'json_object' } (requiring the word 'JSON' in the prompt), or use Structured Outputs (response_format with a strict JSON Schema) to guarantee 100% adherence.
Q4What are token limits and how are they counted?
Token limits apply to both input (prompt) and output (completion). Tokens are word fragments (~4 characters). Every API model has a specific maximum limit (e.g. 128k for gpt-4o).
Q5Why is streaming useful in LLM APIs and how is it implemented?
Streaming returns model output chunk-by-chunk in real-time, reducing perceived latency. It is implemented via Server-Sent Events (SSE) by setting the 'stream' parameter to true in the API call.
Generated from LearnHubly Developer Cheatsheets
Access interactive sandbox tests, tools, and developer code bases at https://www.learnhubly.com