OpenAI Node SDK
Official Node.js client for the OpenAI API exposing chat completions, embeddings, and file operations.
Official Documentation ↗v7.50.0. Check the official docs for the latest API surface.Installation
npm install openaipnpm add openaiMethods
No methods match this filter.
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.
- Filtering response.output to messages can drop required reasoning or tool-call items and cause the next request to fail. (https://cdn.jsdelivr.net/gh/openai/openai-node@latest/README.md)
- This method will throw an error if the signature is invalid. (https://cdn.jsdelivr.net/gh/openai/openai-node@latest/README.md)
- Like .unwrap(), this method will throw an error if the signature is invalid. (https://cdn.jsdelivr.net/gh/openai/openai-node@latest/README.md)
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.
- Set refreshBufferSeconds on the credential to configure proactive refresh; it defaults to 1,200 seconds (20 minutes) and is capped at half of the token’s lifetime. (https://cdn.jsdelivr.net/gh/openai/openai-node@latest/README.md)
- The AWS entrypoint uses the standard AWS credential chain by default. (https://cdn.jsdelivr.net/gh/openai/openai-node@latest/README.md)
- It also accepts a named profile, static credentials, or a custom credential provider. (https://cdn.jsdelivr.net/gh/openai/openai-node@latest/README.md)
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.
Report a problem with this API docOpens a pre-filled issue on GitHub.