interface
BunFetchRequestInit
interface BunFetchRequestInit
Extra options Bun supports in fetch().
These extensions are not part of RequestInit because they don't work when passed to new Request().
- cache?: RequestCache
A string indicating how the request will interact with the browser's cache to set request's cache.
- compress?: boolean | 'deflate' | 'gzip' | 'br' | 'zstd' | { encoding: 'deflate' | 'gzip' | 'br' | 'zstd'; level: number }
Automatically compress the request body before sending and set the
Content-Encodingrequest header accordingly.trueis equivalent to"gzip".- A string selects the encoding with its default level.
- An object selects the encoding and an explicit compression
level.
Only buffered bodies (string,
ArrayBuffer/TypedArray,Blob) are compressed;ReadableStreambodies are sent as-is. If the request already has aContent-Encodingheader, the body is left unchanged. Not part of the Fetch API specification.await fetch("https://example.com/upload", { method: "POST", body: JSON.stringify(bigPayload), compress: "gzip", }); - credentials?: RequestCredentials
A string indicating whether credentials will be sent with the request always, never, or only when sent to a same-origin URL. Sets request's credentials.
- decompress?: boolean
Control automatic decompression of the response body. When
false, the body is not decompressed and theContent-Encodingheader is preserved. This can improve performance when you handle compressed data yourself or forward it as-is. Not part of the Fetch API specification.// Disable automatic decompression for a proxy server const response = await fetch("https://example.com/api", { decompress: false }); // response.headers.get('content-encoding') might be 'gzip' or 'br' - headers?: HeadersInit
A Headers object, an object literal, or an array of two-item arrays to set request's headers.
- integrity?: string
A cryptographic hash of the resource to be fetched by request. Sets request's integrity.
- maxRedirects?: number
The maximum number of redirects to follow when
redirectis"follow". If the response chain redirects more than this many times, the request rejects with a "too many redirects" error. Not part of the Fetch API specification.const response = await fetch("https://example.com/", { maxRedirects: 3 }); - mode?: RequestMode
A string to indicate whether the request will use CORS, or will be restricted to same-origin URLs. Sets request's mode.
- protocol?: 'http2' | 'http1.1' | 'http3' | 'h2' | 'h1' | 'h3'
Force the underlying HTTP version.
"http2"advertises onlyh2in the TLS ALPN list and the request fails withHTTP2Unsupportedif the server doesn't select it."http3"sends the request over HTTP/3 (QUIC)."http1.1"pins the request to HTTP/1.1, overriding--experimental-http2-fetch/BUN_FEATURE_FLAG_EXPERIMENTAL_HTTP2_CLIENTif set. Omit to use the default (h2 is offered iff the flag is on)."h2","h3"and"h1"are aliases.Requires
https. Not part of the Fetch API specification. - proxy?: string | URL | { headers: HeadersInit; url: string | URL }
The proxy to send the request through, overriding the
http_proxyandHTTPS_PROXYenvironment variables. Accepts a URL string, a URL instance, or an object withurland optionalheaders.If a
Proxy-Authorizationheader is provided inproxy.headers, it takes precedence over credentials parsed from the proxy URL.Not part of the Fetch API specification.
// String format const response = await fetch("http://example.com", { proxy: "https://username:password@127.0.0.1:8080" }); // Object format with custom headers sent to the proxy const response = await fetch("http://example.com", { proxy: { url: "https://127.0.0.1:8080", headers: { "Proxy-Authorization": "Bearer token", "X-Custom-Proxy-Header": "value" } } }); - redirect?: RequestRedirect
A string indicating whether request follows redirects, results in an error upon encountering a redirect, or returns the redirect (in an opaque fashion). Sets request's redirect.
- referrer?: string
A string whose value is a same-origin URL, "about:client", or the empty string, to set request's referrer.
- timeout?: number | boolean
Control the socket idle timeout for this request. The timer is reset on every byte sent or received; if the connection stays idle for longer than this, the request fails with a timeout error.
- A finite positive number sets the idle deadline in milliseconds, overriding the
BUN_CONFIG_HTTP_IDLE_TIMEOUTdefault (5 minutes). 0,false, or a non-finite number disables the idle timer for this request.trueor an omitted value uses the default.
This is not a whole-request deadline; use
AbortSignal.timeout(ms)for that. Not part of the Fetch API specification.// Allow a slow streaming response to stay idle for up to an hour const response = await fetch("https://example.com/llm", { timeout: 60 * 60 * 1000, }); - A finite positive number sets the idle deadline in milliseconds, overriding the
- unix?: string
Make the request over a Unix socket
const response = await fetch("http://example.com", { unix: "/path/to/socket" }); - verbose?: boolean
Log the raw HTTP request and response to stdout, as a debugging aid. This API may be removed in a future version of Bun without notice. Not part of the Fetch API specification.