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().

  • body?: null | BodyInit

    A BodyInit object or null to set request's body.

  • 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-Encoding request header accordingly.

    • true is 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; ReadableStream bodies are sent as-is. If the request already has a Content-Encoding header, 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 the Content-Encoding header 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.

  • keepalive?: boolean

    A boolean to set request's keepalive.

  • maxRedirects?: number

    The maximum number of redirects to follow when redirect is "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 });
  • method?: string

    A string to set request's method.

  • mode?: RequestMode

    A string to indicate whether the request will use CORS, or will be restricted to same-origin URLs. Sets request's mode.

  • priority?: RequestPriority
  • protocol?: 'http2' | 'http1.1' | 'http3' | 'h2' | 'h1' | 'h3'

    Force the underlying HTTP version. "http2" advertises only h2 in the TLS ALPN list and the request fails with HTTP2Unsupported if 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_CLIENT if 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_proxy and HTTPS_PROXY environment variables. Accepts a URL string, a URL instance, or an object with url and optional headers.

    If a Proxy-Authorization header is provided in proxy.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.

  • referrerPolicy?: ReferrerPolicy

    A referrer policy to set request's referrerPolicy.

  • s3?: S3Options

    Override the default S3 options

    const response = await fetch("s3://bucket/key", {
      s3: {
        accessKeyId: "AKIAIOSFODNN7EXAMPLE",
        secretAccessKey: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
        region: "us-east-1",
      }
    });
  • signal?: null | AbortSignal

    An AbortSignal to set request's signal.

  • 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_TIMEOUT default (5 minutes).
    • 0, false, or a non-finite number disables the idle timer for this request.
    • true or 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,
    });
  • tls?: BunFetchRequestInitTLS

    Override the default TLS options

  • 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.

  • window?: null

    Can only be null. Used to disassociate request from any Window.