Registry / ai-ml / openai-fetch

openai-fetch

JSON →
library3.4.2jsnpmunverified

A minimal and opinionated OpenAI client powered by native fetch (ESM-only, ~14kb). Version 3.4.2 requires Node 18+ or any runtime with built-in fetch. It supports only chat, completions, embeddings, moderations, and TTS endpoints, offering a lean alternative to the official `openai` package (~152kb) that patches fetch. Ships TypeScript types and is actively maintained on GitHub.

npm install openai-fetch
INSTALL
IMPORT
SIG · OPENAI-FETCH
O
openai-fetch
ai-mljavascriptv3.4.2
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

OpenAIClient
import { OpenAIClient } from 'openai-fetch'
const OpenAIClient = require('openai-fetch')
ESM-only; CommonJS require is not supported.
type ChatParams
import type { ChatParams } from 'openai-fetch'
import { ChatParams } from 'openai-fetch'
ChatParams is a type; use import type to avoid runtime errors in TypeScript.
ChatParams
import { ChatParams } from 'openai-fetch'
const { ChatParams } = require('openai-fetch')
Require is invalid for ESM packages; also, ChatParams is a type, so import type is preferred.

Demonstrates creating a chat completion using the native fetch client with TypeScript.

import { OpenAIClient } from 'openai-fetch'; const client = new OpenAIClient({ apiKey: process.env.OPENAI_API_KEY ?? '' }); async function main() { const response = await client.createChatCompletion({ model: 'gpt-4', messages: [{ role: 'user', content: 'Hello!' }] }); console.log(response.choices[0].message?.content); } main().catch(console.error);
Debug
Known issues
breakingopenai-fetch is ESM-only. It cannot be require()'d from CommonJS.
fix
Use dynamic import: import('openai-fetch') or switch your project to ESM.
affects: >=1.0.0
breakingRequires Node >= 18 or platform with native fetch. Older Node versions or environments without fetch will throw.
fix
Upgrade to Node 18+ or use a fetch polyfill.
affects: >=1.0.0
deprecatedThe client method 'createCompletions' (plural) is deprecated in favor of 'createCompletion' (singular) in v3.0.0? (Check docs).
fix
Use createCompletion for the singular endpoint.
affects: >=3.0.0
gotchaThe apiKey is read from process.env.OPENAI_API_KEY if not provided explicitly. If that env var is not set and no key is passed, an error will be thrown.
fix
Always set OPENAI_API_KEY or pass apiKey option explicitly.
affects: >=1.0.0
Errors
Common errors & fixes
Error: The 'openai-fetch' package is ESM only. Use 'import' instead of 'require'.
Trying to require ESM package in CommonJS module.
fix
import { OpenAIClient } from 'openai-fetch' or use dynamic import.
TypeError: fetch is not a function
Running in Node < 18 or environment without native fetch.
fix
Upgrade to Node >= 18 or use a fetch polyfill like 'node-fetch'.
OPENAI_API_KEY must be provided or set as environment variable
No apiKey provided and OPENAI_API_KEY not set.
fix
Set OPENAI_API_KEY or pass apiKey in client constructor.
TypeScript error: 'ChatParams' is a type and must be imported using 'import type'
Importing a type as a value.
fix
import type { ChatParams } from 'openai-fetch'
Upgrade
Version history
3.4.2latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
7 hits · last 30 days
node
6
Resources
openai-fetch — npm install openai-fetch · libregistry