What are some node libraries for sending requests to OpenAI providers. I don’t want to…
- User
What are some node libraries for sending requests to OpenAI providers. I don’t want to use the OpenAI library
- Assistant
Below are a handful of popular (and battle-tested) Node.js libraries you can use to hit the OpenAI REST endpoints without relying on the official openai npm package.
I grouped them so you can pick the level of abstraction you’re comfortable with.
1. “Pure” HTTP clients (do-it-yourself wrappers)
Library Install Why you might like it Streaming support axios npm i axiosSimple API, automatic JSON, good error handling, interceptors & retries via plugins Yes – access response.dataas a streamgot npm i gotModern, ESM-first, built-in retry/back-off and hooks, very little overhead Yes – .stream(url)or{ isStream: true }node-fetch / undici Node ≥ 18 already has global fetch; for older versions usenpm i node-fetchMinimal, Web-standard syntax, “it just works”, great for serverless Yes – body is a ReadableStreamsuperagent npm i superagentChainable API, nice for quick scripts & tests Yes – .pipe()orres.on('data')When you go “pure HTTP” you manually set the endpoint, headers, query params, etc.—but you stay 100 % in control and keep your bundle size tiny.
2. Thin, community-built OpenAI helpers (unofficial)
Library Install What it gives you Caveats openai-api npm i openai-apiPromise-based wrapper, tiny (≈ 2 kB), lets you call Chat / Completions / Embeddings with one-liners Unmaintained for a while—check endpoints match current API openai-streams npm i openai-streamsUtility that wraps the streaming Chat endpoint and returns an async generator → super handy for SSE Does only ChatCompletions; no images, files, etc. gpt-3.5-turbo-proxy npm i gpt-3.5-turbo-proxyLightweight proxy & retry logic, fetch-based, no other deps Small community, updates may lag These libs save a bit of boilerplate but still keep you independent from the official monorepo.
3. Multi-provider abstractions (if you want OpenAI + Azure + others)
Library Install Supports Notes openai-proxy npm i openai-proxyOpenAI, Azure OpenAI Adds auth routing, rate-limit helpers genAI-clients (experimental) npm i genai-clientsOpenAI, Anthropic, Cohere Community project; nice if you need to swap providers behind one interface
Quick usage examples
Below are two minimal snippets so you can see what “rolling your own” looks like.
A. Using the built-in
fetch(Node 18+)// .env: OPENAI_API_KEY=sk-**** import 'dotenv/config'; // npm i dotenv const endpoint = 'https://api.openai.com/v1/chat/completions'; const res = await fetch(endpoint, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${process.env.OPENAI_API_KEY}` }, body: JSON.stringify({ model: 'gpt-3.5-turbo', messages: [ { role: 'user', content: 'Explain the Doppler effect in 2 sentences.' } ], stream: false // stream: true → handle SSE }) }); if (!res.ok) throw new Error(await res.text()); const data = await res.json(); console.log(data.choices[0].message.content);B. Streaming with axios
import axios from 'axios'; import { pipeline } from 'stream'; import { promisify } from 'util'; const api = axios.create({ baseURL: 'https://api.openai.com/v1', headers: { Authorization: `Bearer ${process.env.OPENAI_API_KEY}` }, responseType: 'stream' }); const rsp = await api.post('/chat/completions', { model: 'gpt-3.5-turbo', messages: [{ role: 'user', content: 'Stream 5 words.' }], stream: true }); rsp.data.on('data', chunk => { const lines = chunk.toString().split('\n').filter(Boolean); for (const line of lines) { if (line === 'data: [DONE]') return; const payload = JSON.parse(line.replace(/^data: /, '')); process.stdout.write(payload.choices[0].delta?.content || ''); } });
Tips when you’re not using the official SDK
- Handle rate limits (HTTP 429) & back-off.
gotalready has retry logic; with others add a wrapper (exponential backoff). - Watch for streaming vs. non-streaming responses—set
Accept: text/event-streamautomatically whenstream:true. - If you deploy to Cloudflare Workers, Vercel Edge, etc., prefer
fetch/undicifor smallest bundles. - Keep up with the OpenAI changelog (new models, parameters) since no SDK will insulate you.
Happy hacking 🤖✨
- Handle rate limits (HTTP 429) & back-off.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.