API Reference

OpenAI Node SDK

Official Node.js client for the OpenAI API exposing chat completions, embeddings, and file operations.

Official Documentation ↗
CompatibilityThis documentation covers version v7.50.0. Check the official docs for the latest API surface.

Installation

npmnpm install openai
pnpmpnpm add openai

Methods

Overview

OpenAI Node SDK is the official Node.js client for the OpenAI API, covering chat completions, embeddings, and file operations. This reference lists the most frequently used APIs with accurate signatures and runnable snippets.

Core APIs

  • new OpenAI — Creates a client, reading the API key from the argument or OPENAI_API_KEY by default.
  • chat.completions.create — Sends a chat-style request and resolves with the model response.
  • embeddings.create — Computes vector embeddings for one or more input strings.

Example

const OpenAI = require('openai');

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

Notes

The API key is never to be embedded in client code. Network failures surface as typed errors, so wrap calls and handle rate limits explicitly.

Verification

Create a client with a key and issue a small chat request, confirming the response contains at least one choice.

Errors

The following points are quoted verbatim from the official documentation. 来源:https://cdn.jsdelivr.net/gh/openai/openai-node@latest/README.md · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:51:21.091Z. Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.

Security notes

Quoted verbatim from the official documentation; this is not a complete threat model. 来源:https://cdn.jsdelivr.net/gh/openai/openai-node@latest/README.md · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:51:21.091Z.

FAQ

How are rate limit and server errors surfaced?
The SDK throws typed APIError subclasses such as RateLimitError and InternalServerError, so retries can target them.
What is the default automatic retry behavior?
The client retries certain connection and 429 or 5xx responses a small number of times with backoff unless maxRetries is set.
How do I stream responses?
Pass stream: true and iterate the returned async iterable of chunks, appending each delta content.