OpenAI: Official Node.js SDK

raw JSON →
6.34.0 verified Mon May 11 auth: no javascript install: verified quickstart: stale

The official TypeScript and Node.js SDK for the OpenAI API, providing convenient access to frontier models like GPT-5.4, GPT-5.4-mini, and Sora 2. Currently at version 6.34.0, it features auto-retries, native streaming, structured data handling, and robust typing. It is the recommended standard to interact with OpenAI from server-side JavaScript runtimes (Node.js, Deno, Bun, Cloudflare Workers), abstracting away raw HTTP calls and providing seamless integration with advanced capabilities like Computer Use, Tool Search, and WebSocket-based Realtime streams.

error Error 429: You exceeded your current quota, please check your plan and billing details
cause You are attempting to use the API without pre-paid credits. OpenAI APIs are no longer free and require a pre-funded balance, even on newly created accounts or accounts with a ChatGPT Plus subscription (which is billed separately).
fix
Navigate to platform.openai.com/account/billing, attach a valid payment method, and add an initial credit balance.
error Error: It looks like you're running in a browser-like environment. This is disabled by default
cause The SDK detected a browser environment (like React, Vue, or Next.js client-components). OpenAI blocks this to prevent developers from accidentally exposing their secret `OPENAI_API_KEY` in client-side bundles.
fix
Move the API call to a backend server/serverless API route. If you must run it in the client, instantiate with new OpenAI({ dangerouslyAllowBrowser: true }).
error Error 400: Purpose must be fine-tune/assistants
cause When uploading files via `openai.files.create`, a `purpose` argument is required to validate the file format. The endpoint strictly expects certain enumerations like 'fine-tune' or 'assistants'.
fix
Ensure you provide a valid purpose string. Example: await openai.files.create({ file: fs.createReadStream('data.jsonl'), purpose: 'assistants' });
breaking The v4.0 release was a complete architectural rewrite. Code written for v3 using `Configuration` and `OpenAIApi` will instantly fail and requires a full migration to the new `OpenAI` client instance architecture.
fix Rewrite initialization to use `new OpenAI()` and migrate method calls from `openai.createChatCompletion` to `openai.chat.completions.create`.
gotcha Attempting to use the SDK in a browser environment will intentionally throw an error by default to prevent leaking API keys to the client side.
fix Pass `dangerouslyAllowBrowser: true` to the constructor ONLY if you are absolutely sure about the security implications or are proxying requests securely through a backend.
gotcha With the introduction of new generation capabilities (like Codex and Computer Use), OpenAI implemented automated cybersecurity safeguards. Suspicious traffic patterns may result in sudden HTTP 403 or `cyber_policy` errors.
fix Implement a unique `safety_identifier` per end-user. If access is revoked due to suspicious activity, passing this identifier ensures it only impacts the offending user rather than your entire organization.
npm install openai
yarn add openai
pnpm add openai
python os / libc status wheel install import disk
18 alpine (musl) - - - -
18 slim (glibc) - - - -
20 alpine (musl) - - - -
20 slim (glibc) - - - -
22 alpine (musl) - - - -
22 slim (glibc) - - - -
3.10 alpine (musl) - - 1.74s 46.9M
3.10 slim (glibc) - - 1.20s 46M
3.11 alpine (musl) - - 2.51s 50.7M
3.11 slim (glibc) - - 1.95s 50M
3.12 alpine (musl) - - 2.29s 42.0M
3.12 slim (glibc) - - 2.24s 41M
3.13 alpine (musl) - - 2.07s 41.7M
3.13 slim (glibc) - - 2.16s 41M
3.9 alpine (musl) - - 1.67s 46.2M
3.9 slim (glibc) - - 1.44s 45M

A basic Chat Completions API request using the latest gpt-5.4-mini model, illustrating the standard async initialization and usage pattern.

import OpenAI from 'openai';

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

async function main() {
  const response = await client.chat.completions.create({
    model: 'gpt-5.4-mini',
    messages: [{ role: 'user', content: 'What is the speed of light?' }],
  });
  console.log(response.choices[0].message.content);
}

main();