Calling AI APIs in Node.js: OpenAI SDK and Native Fetch Integration, Streaming, Timeouts, Retries, and Error Handling

Call OpenAI-compatible AI APIs in Node.js 18+: use the official openai SDK by changing baseURL and apiKey, or native fetch with manual SSE parsing, AbortController timeouts, status-code-based retries, and handling of 401, 429, and connection errors.

How to Integrate a Streaming Output API: SSE Parsing, Token Usage, and Proxy Buffering Troubleshooting

Starting from a working curl command, this guide explains the SSE message structure of streaming output APIs, the empty choices and cross-packet issues easily missed during parsing, how to get token usage with stream_options, the troubleshooting order for reverse proxy buffering that causes one-shot output, and how to abort upstream requests when the client disconnects.