The official TypeScript library for the OpenAI API
The openai package is the official TypeScript/JavaScript SDK for interacting with OpenAI's REST API from server-side environments like Node.js, Deno, and Bun. Built by OpenAI and auto-generated from their OpenAPI specification using Stainless, it provides type-safe access to all API endpoints including chat completions, assistants, image generation, audio transcription, fine-tuning, and embeddings.
With over 11 million weekly downloads, this package has become the de facto standard for integrating OpenAI's language models into JavaScript applications. It handles authentication, request/response formatting, streaming, error handling, and retries out of the box, eliminating the need to manually construct HTTP requests or maintain custom type definitions.
The SDK is TypeScript-first with complete type inference for all request parameters and response objects. Version 4.x and later support modern OpenAI features like the Assistants API with persistent threads, structured JSON outputs, function calling, vision capabilities, and real-time streaming. It's designed for production use with built-in support for proxies, custom fetch options, and environment-based configuration.
Developers use this package when building chatbots, content generation tools, code assistants, document analysis systems, or any application requiring access to GPT-4, GPT-3.5, DALL-E, Whisper, or other OpenAI models. The official nature ensures compatibility with new API features as they're released and provides confidence in long-term maintenance.
import OpenAI from 'openai';
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY
});
async function generateChatResponse() {
const completion = await openai.chat.completions.create({
model: 'gpt-4o',
messages: [
{ role: 'system', content: 'You are a helpful coding assistant.' },
{ role: 'user', content: 'Explain async/await in JavaScript concisely.' }
],
temperature: 0.7,
max_tokens: 150
});
console.log(completion.choices[0].message.content);
}
async function streamResponse() {
const stream = await openai.chat.completions.create({
model: 'gpt-4o',
messages: [{ role: 'user', content: 'Count from 1 to 5 slowly.' }],
stream: true
});
for await (const chunk of stream) {
const content = chunk.choices[0]?.delta?.content || '';
process.stdout.write(content);
}
}
async function functionCalling() {
const tools = [{
type: 'function',
function: {
name: 'get_weather',
description: 'Get current weather for a location',
parameters: {
type: 'object',
properties: {
location: { type: 'string' },
unit: { type: 'string', enum: ['celsius', 'fahrenheit'] }
},
required: ['location']
}
}
}];
const response = await openai.chat.completions.create({
model: 'gpt-4o',
messages: [{ role: 'user', content: 'What\'s the weather in Paris?' }],
tools
});
const toolCall = response.choices[0].message.tool_calls?.[0];
if (toolCall) {
console.log('Function:', toolCall.function.name);
console.log('Arguments:', toolCall.function.arguments);
}
}
generateChatResponse();
streamResponse();
functionCalling();Chatbots and conversational AI: Build customer support bots, virtual assistants, or interactive chat interfaces using the chat completions API with GPT-4 or GPT-3.5-turbo. Supports streaming responses for real-time user experience and function calling to integrate external tools or databases.
Document analysis and Q&A systems: Use the Assistants API with file search capabilities to create persistent threads that analyze uploaded documents, answer questions about PDFs, or maintain context across multiple user sessions spanning days or weeks.
Content generation pipelines: Automate blog post creation, social media content, or marketing copy generation by chaining multiple completion requests with specific instructions and structured JSON outputs for consistent formatting.
Image and audio processing: Generate images with DALL-E, transcribe audio files with Whisper, or perform vision tasks by sending images alongside text prompts to multimodal models like GPT-4V for OCR, captioning, or visual analysis.
Custom model fine-tuning: Upload training datasets in JSONL format, create fine-tuning jobs for domain-specific models, and monitor training progress programmatically for specialized applications requiring tailored model behavior.
The official TypeScript library for the Anthropic API
Typescript bindings for langchain
<p align="center"> <img height="100" width="100" alt="LlamaIndex logo" src="https://ts.llamaindex.ai/square.svg" /> </p> <h1 align="center">LlamaIndex.TS</h1> <h3 align="center"> Data framework for your LLM application. </h3>
npm install openaipnpm add openaibun add openai