Skip to main content
Version: Next

FetchHttpClient

The default HTTP client of ApifyClient, built on the fetch of the runtime, so it works the same way in Node.js, browsers and edge runtimes and brings no dependency of its own.

It inherits the request pipeline from HttpClient and adds the transport. Network errors are retried, timeouts included, and a stream body is sent without following redirects, since the part of it already sent could not be replayed. The timeout of a request runs until the response headers arrive and starts over with every chunk of the response body and of a stream body, so a large download or upload does not time out while it makes progress.

In Node.js, fetch keeps connections alive on its own, and goes through a proxy only when Node.js is told to read the HTTP_PROXY, HTTPS_PROXY and NO_PROXY environment variables: start it with NODE_USE_ENV_PROXY=1 (Node.js 22.21 and 24.0) or --use-env-proxy (Node.js 22.21 and 24.5).

Hierarchy

Index

Constructors

constructor

Properties

readonlyinheritedhttpCompressor

httpCompressor: HttpCompressor

Compressor the request pipeline runs the bodies worth compressing through.

inheritedlogger

logger: Log

Logger for the retry warnings.

inheritedmaxRetries

maxRetries: number

How many times a failed request is retried at most.

inheritedminDelayBetweenRetriesMillis

minDelayBetweenRetriesMillis: number

Lower bound for the delay before the first retry in milliseconds. It doubles with every further retry.

inheritedstats

stats: Statistics

Statistics of the API calls made through this client.

inheritedtimeoutMaxMillis

timeoutMaxMillis: number

Upper bound for the timeout of a single attempt, in milliseconds.

inheritedtimeoutMillis

timeoutMillis: Record<TimeoutTier, number>

Duration of each timeout tier, in milliseconds.

Methods

inheritedcall

  • Makes an API request with automatic retries and exponential backoff.

    Network errors the transport classifies as retryable, rate limits (HTTP 429) and server errors (HTTP 5xx) are retried up to maxRetries times, and config.retryStatusCodes picks other statuses to retry. Any other error status is thrown as ApifyApiError right away, or returned when config.throwOnErrorStatus is false. A request whose body is a Readable or a web ReadableStream is never retried, since part of the stream has already been consumed by the time the failure shows. A Blob body is read again for every attempt, so it is retried like any other, unless reading the blob itself fails: that error is thrown right away, with the transport error as its cause when that error does not already wrap it. Aborting config.signal ends the attempt in flight, skips the remaining retries and rejects the call with the signal's reason.


    Parameters

    Returns Promise<ApifyResponse<T>>

    The successful response, or the error response when config.throwOnErrorStatus is false.

    Throws - When the API responds with an error status the retries could not fix, unless config.throwOnErrorStatus is false.

inheritedclose

  • close(): Promise<void>
  • Releases resources owned by the transport, such as a connection pool. The default does nothing.


    Returns Promise<void>

isRetryableTransportError

  • isRetryableTransportError(error): boolean
  • A network failure is the TypeError that fetch rejects with: a connection that could not be made, dropped mid-way or timed out. In Node.js it carries the failure as its cause, which tells it apart from a request that was never sent, such as one with an invalid URL. Bun rejects with an Error that has a code instead.


    Parameters

    • error: unknown

    Returns boolean

isTimeoutError

  • isTimeoutError(error): boolean
  • A timeout is the TimeoutError this client aborts a late request with, or a fetch failure caused by one of the connection, headers and body timeouts of Node.js.


    Parameters

    • error: unknown

    Returns boolean

sendRequest

  • Sends the request through fetch and hands the raw response back. A stream body is sent without following redirects, and so is a request with followRedirects: false. A browser hides such a redirect response from the page, so there it fails the request.


    Parameters

    Returns Promise<HttpResponse>

inheritedsetDefaultAuthorization

  • setDefaultAuthorization(token): void
  • Sets the Authorization header from the token, unless an authorization header is already configured.


    Parameters

    • token: string

      The Apify API token to send as the Bearer token.

    Returns void