Implementation:Openai Openai node OpenAI Constructor
| Knowledge Sources | |
|---|---|
| Domains | SDK_Architecture, Authentication |
| Last Updated | 2026-02-15 00:00 GMT |
Overview
Concrete tool for initializing the OpenAI API client provided by the openai-node SDK.
Description
The OpenAI class is the main entry point for all API interactions. Its constructor accepts a ClientOptions configuration object and initializes authentication, connection parameters, and resource accessors (chat, responses, audio, files, fineTuning, realtime, etc.). The constructor reads environment variables as defaults and validates that an API key is present.
Usage
Import and instantiate this class at application startup before making any API calls. Use explicit options for testing, multi-tenant deployments, or custom base URLs (e.g., Azure OpenAI).
Code Reference
Source Location
- Repository: openai-node
- File: src/client.ts
- Lines: L237-427 (ClientOptions interface + constructor)
Signature
export interface ClientOptions {
apiKey?: string | ApiKeySetter | undefined;
organization?: string | null | undefined;
project?: string | null | undefined;
webhookSecret?: string | null | undefined;
baseURL?: string | null | undefined;
timeout?: number | undefined;
fetchOptions?: MergedRequestInit | undefined;
fetch?: Fetch | undefined;
maxRetries?: number | undefined;
defaultHeaders?: HeadersLike | undefined;
defaultQuery?: Record<string, string | undefined> | undefined;
dangerouslyAllowBrowser?: boolean | undefined;
logLevel?: LogLevel | undefined;
logger?: Logger | undefined;
}
export class OpenAI {
constructor(opts?: ClientOptions);
}
Import
import OpenAI from 'openai';
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| apiKey | ApiKeySetter | No (reads OPENAI_API_KEY env) | API key for authentication; can be a string or async function |
| organization | null | No (reads OPENAI_ORG_ID env) | Organization ID for multi-org accounts |
| project | null | No (reads OPENAI_PROJECT_ID env) | Project ID for scoped access |
| baseURL | null | No (defaults to https://api.openai.com/v1) | Override base URL for API requests |
| timeout | number | No (defaults to 600000ms / 10min) | Request timeout in milliseconds |
| maxRetries | number | No (defaults to 2) | Maximum retry attempts for failed requests |
| dangerouslyAllowBrowser | boolean | No (defaults to false) | Allow usage in browser environments |
| fetch | Fetch | No (uses global fetch) | Custom fetch implementation |
Outputs
| Name | Type | Description |
|---|---|---|
| client | OpenAI | Configured client instance with resource accessors: client.chat, client.responses, client.audio, client.files, client.fineTuning, client.realtime, etc. |
Usage Examples
Basic Initialization
import OpenAI from 'openai';
// Reads OPENAI_API_KEY from environment
const client = new OpenAI();
Explicit Configuration
import OpenAI from 'openai';
const client = new OpenAI({
apiKey: 'sk-...',
organization: 'org-...',
project: 'proj-...',
maxRetries: 3,
timeout: 30000,
});
Custom Base URL (e.g., proxy or Azure)
import OpenAI from 'openai';
const client = new OpenAI({
apiKey: 'sk-...',
baseURL: 'https://my-proxy.example.com/v1',
});
Related Pages
Implements Principle
Requires Environment
- Environment:Openai_Openai_node_Node_20_Runtime
- Environment:Openai_Openai_node_OpenAI_API_Credentials