openai fetch
v3.3.2
A minimal and opinionated OpenAI client powered by fetch.
Unfortunately, the official openai package patches fetch in problematic ways and is quite bloated.
openai-fetch:openai-fetch is ~14kb and openai is ~152kbopenai package if:npm install openai-fetchThis package requires node >= 18 or an environment with fetch support.
This package exports ESM. If your project uses CommonJS, consider switching to ESM or use the dynamic import() function.
import { OpenAIClient } from 'openai-fetch';
const client = new OpenAIClient({ apiKey: '<your api key>' });The apiKey is optional and will be read from process.env.OPENAI_API_KEY if present.
The API follows OpenAI very closely, so their reference documentation can generally be used. Everything is strongly typed, so you will know if anything is different as soon as TypeScript parses your code.
// Generate a single chat completion
client.createChatCompletion(params: ChatParams): Promise<ChatResponse>;
// Stream a single completion via a ReadableStream
client.streamChatCompletion(params: ChatStreamParams): Promise<ChatStreamResponse>;
// Generate one or more completions
client.createCompletions(params: CompletionParams): Promise<CompletionResponse>;
// Stream a single completion via a ReadableStream
client.streamCompletion(params: CompletionStreamParams): Promise<CompletionStreamResponse>;
// Generate one or more embeddings
client.createEmbeddings(params: EmbeddingParams): Promise<EmbeddingResponse>
// Checks for potentially harmful content
client.createModeration(params: ModerationParams): Promise<ModerationResponse>
// Text-to-Speech
client.createSpeech(params: SpeechParams): Promise<SpeechResponse>The type definitions are avaible through TSServer, and can be found here: type definitions.
MIT © Dexa