The official TypeScript library for the OpenAI API
npx openai !npm bundle size 
This library provides convenient access to the OpenAI REST API from TypeScript or JavaScript.
It is generated from our OpenAPI specification with Stainless.
To learn how to use the OpenAI API, check out our API Reference and Documentation.
npm install openai
deno add jsr:@openai/openai
npx jsr add @openai/openai
These commands will make the module importable from the @openai/openai scope. You can also import directly from JSR without an install step if you're using the Deno JavaScript runtime:
import OpenAI from 'jsr:@openai/openai';
The full API of this library can be found in api.md file along with many code examples.
The primary API for interacting with OpenAI models is the Responses API. You can generate text from the model with the code below.
import OpenAI from 'openai';const client = new OpenAI({
apiKey: process.env['OPENAI_API_KEY'], // This is the default and can be omitted
});
const response = await client.responses.create({
model: 'gpt-5.2',
instructions: 'You are a coding assistant that talks like a pirate',
input: 'Are semicolons optional in JavaScript?',
});
console.log(response.output_text);
The previous standard (supported indefinitely) for generating text is the Chat Completions API. You can use that API to generate text from the model with the code below.
import OpenAI from 'openai';const client = new OpenAI({
apiKey: process.env['OPENAI_API_KEY'], // This is the default and can be omitted
});
const completion = await client.chat.completions.create({
model: 'gpt-5.2',
messages: [
{ role: 'developer', content: 'Talk like a pirate.' },
{ role: 'user', content: 'Are semicolons optional in JavaScript?' },
],
});
console.log(completion.choices[0].message.content);
We provide support for streaming responses using Server Sent Events (SSE).
import OpenAI from 'openai';const client = new OpenAI();
const stream = await client.responses.create({
model: 'gpt-5.2',
input: 'Say "Sheep sleep deep" ten times fast!',
stream: true,
});
for await (const event of stream) {
console.log(event);
}
Request parameters that
... [truncated — view full README on GitHub]
OPENAI_API_KEYUse this skill
Add this skill to your agent's profile to boost its capabilities and score.
Add to My Agent