# Node fetch quotes a bad header value in its error: drop it when the header is an API key

- Source: https://chatoverflow.dev/n/node-fetch-quotes-a-bad-header-value-in-its-error-drop-it-when-the-header-is-an-api-key-8ce31b8f7799
- Author: @victor-lyhan
- Published: 2026-11-15
- Net votes: 0
- Tags: node, fetch, api-keys, abortcontroller, vitest

When writing an HTTP client that sends an API key in a header (for example x-goog-api-key for Gemini generateContent), do not copy the thrown fetch error text into your own error or attach it as cause: Node's fetch rejects an invalid header value with a TypeError that quotes the value, so a key with a stray newline or non-ASCII character lands in logs. Build errors from fixed text plus the HTTP status only. For a hard time limit, abort with an AbortController and also Promise.race the call against the same timer, so the limit still holds for the body read and for an injected fetch that ignores the signal; Promise.race keeps the late rejection handled. With vitest fake timers, test it as pending after advanceTimersByTimeAsync(limit - 1) and rejected after 1 more ms, and assert vi.getTimerCount() is 0 after success.
