openai/openai-node
TypeScript
Captured source
source ↗openai/openai-node
Description: Official JavaScript / TypeScript library for the OpenAI API
Language: TypeScript
License: Apache-2.0
Stars: 10965
Forks: 1517
Open issues: 264
Created: 2021-12-14T22:32:58Z
Pushed: 2026-06-10T23:37:05Z
Default branch: main
Fork: no
Archived: no
README:
OpenAI TypeScript and JavaScript API Library
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.
Installation
npm install openai
Installation from JSR
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';
Usage
The full API of this library can be found in [api.md file](api.md) 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.5',
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.5',
messages: [
{ role: 'developer', content: 'Talk like a pirate.' },
{ role: 'user', content: 'Are semicolons optional in JavaScript?' },
],
});
console.log(completion.choices[0].message.content);Workload Identity Authentication
For secure, automated environments like cloud-managed Kubernetes, Azure, and GCP, you can use workload identity authentication with short-lived tokens from cloud identity providers instead of long-lived API keys.
The workloadIdentity parameter is mutually exclusive with apiKey.
The required fields are identityProviderId, serviceAccountId, and provider.
Kubernetes (service account tokens)
import OpenAI from 'openai';
import { k8sServiceAccountTokenProvider } from 'openai/auth';
const client = new OpenAI({
workloadIdentity: {
identityProviderId: 'idp-123',
serviceAccountId: 'sa-456',
provider: k8sServiceAccountTokenProvider('/var/run/secrets/kubernetes.io/serviceaccount/token'),
},
});
const response = await client.chat.completions.create({
model: 'gpt-5.5',
messages: [{ role: 'user', content: 'Hello!' }],
});Azure (managed identity)
import OpenAI from 'openai';
import { azureManagedIdentityTokenProvider } from 'openai/auth';
const client = new OpenAI({
workloadIdentity: {
identityProviderId: 'idp-123',
serviceAccountId: 'sa-456',
provider: azureManagedIdentityTokenProvider(),
},
});GCP (compute engine metadata)
import OpenAI from 'openai';
import { gcpIDTokenProvider } from 'openai/auth';
const client = new OpenAI({
workloadIdentity: {
identityProviderId: 'idp-123',
serviceAccountId: 'sa-456',
provider: gcpIDTokenProvider(),
},
});Custom subject token provider
import OpenAI from 'openai';
const client = new OpenAI({
workloadIdentity: {
identityProviderId: 'idp-123',
serviceAccountId: 'sa-456',
provider: {
tokenType: 'jwt',
getToken: async () => {
return 'your-jwt-token';
},
},
},
});You can also customize the token refresh buffer (default is 1200 seconds (20 minutes) before expiration):
import OpenAI from 'openai';
import { k8sServiceAccountTokenProvider } from 'openai/auth';
const client = new OpenAI({
workloadIdentity: {
identityProviderId: 'idp-123',
serviceAccountId: 'sa-456',
provider: k8sServiceAccountTokenProvider('/var/token'),
refreshBufferSeconds: 120.0,
},
});Streaming responses
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.5',
input: 'Say "Sheep sleep deep" ten times fast!',
stream: true,
});
for await (const event of stream) {
console.log(event);
}File uploads
Request parameters that correspond to file uploads can be passed in many different forms:
File(or an object with the same structure)- a
fetchResponse(or an object with the same structure) - an
fs.ReadStream - the return value of our
toFilehelper
import fs from 'fs';
import OpenAI, { toFile } from 'openai';
const client = new OpenAI();
// If you have access to Node `fs` we recommend using `fs.createReadStream()`:
await client.files.create({ file: fs.createReadStream('input.jsonl'), purpose: 'fine-tune' });
// Or if you have the web `File` API you can pass a `File` instance:
await client.files.create({ file: new File(['my bytes'], 'input.jsonl'), purpose: 'fine-tune' });
// You can also pass a `fetch` `Response`:
await client.files.create({
file: await fetch('https://somesite/input.jsonl'),
purpose: 'fine-tune',
});
// Finally, if none of the above are convenient, you can use our `toFile` helper:
await client.files.create({
file: await toFile(Buffer.from('my bytes'), 'input.jsonl'),
purpose: 'fine-tune',
});
await client.files.create({
file: await toFile(new Uint8Array([0, 1, 2]), 'input.jsonl'),
purpose: 'fine-tune',
});Webhook Verification
Verifying webhook signatures is _optional but encouraged_.
For more information about webhooks, see the API docs.
Parsing webhook…
Excerpt shown — open the source for the full document.