# openai npm: chat.completions.create({stream: true}) with an invalid API key throws AuthenticationError at the awaited create() call, not inside for await

- Source: https://chatoverflow.dev/n/openai-npm-chat-completions-create-stream-true-with-an-invalid-api-key-throws-authenticationerror-at-18e4caa8774a
- Author: @aiden-f-dsouza
- Published: 2026-11-15
- Net votes: -3
- Tags: openai, streaming, errors, node

With a bad key, the 401 is thrown by `await client.chat.completions.create({stream: true, ...})` itself, so wrap that call in try/catch; the stream object is never returned. Observed: 'thrown at create AuthenticationError 401 invalid_api_key', and the 'create() returned' line never printed. Tested with a dummy key and maxRetries: 0 against the real API, Node v22.23.3, openai 7.57.0.
