BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Ky 2.0 Fetch API Wrapper with Revamped Hooks, Smarter Timeouts, and Built-In Schema Validation

Ky 2.0 Fetch API Wrapper with Revamped Hooks, Smarter Timeouts, and Built-In Schema Validation

Listen to this article -  0:00

Ky, the tiny and elegant JavaScript HTTP client built on the Fetch API by Sindre Sorhus, has released version 2.0, its first major version in years, shipping with revamped hooks, smarter timeouts, cleaner URL handling, and built-in response validation.

Ky 2.0 unifies every hook around a single state object. The beforeRequest, afterResponse, beforeRetry and beforeError hooks now all receive one {request, options, retryCount, ...} argument instead of separate parameters, making the API consistent and easier to extend in future. A new init hook runs before everything else, and beforeError now fires for every error type rather than only HTTPError.

Timeouts have also been reworked. Alongside the existing per-attempt timeout, a new totalTimeout option caps the entire operation across all retries and delays, throwing a TimeoutError when it is exceeded. Retry handling is tighter, with a dedicated NetworkError class and automatic retries reserved for genuine network failures such as DNS errors or a refused connection, rather than programming bugs.

URL handling, a long-standing source of confusion, has been split into two clearer options. The prefixUrl option has been renamed to prefix and now allows leading slashes in the input, while a new baseUrl option follows standard URL resolution,

The release also brings Standard Schema validation to .json(), letting developers validate and type responses with Zod, Valibot and other compatible validators. HTTPError now exposes a pre-parsed data property, which fixes resource leaks and makes error bodies available without awaiting a second call.

On Hacker News, maintainer Seth Holladay described the prerelease work:

Many quality of life improvements. Timeouts and hooks have been revamped. Quirks related to base URLs for custom instances have been solved. Every known bug has been fixed.

Asked how Ky compares to axios, he replied that:

Ky was written to use fetch from the beginning, whereas Axios tries to adapt itself to fetch, which doesn't always work well. We also have much better TypeScript support, including built-in schema validation with type inference.

On Reddit, developers praised ky:

This is pretty cool. Basically fetch++. This is all basically stuff that I'd write a fetch wrapper for, but writing the wrapper is a bit annoying and requires some testing that I usually don't want to be doing.

In a separate thread, one commenter who switched from axios said they "love it", and another described the new changes as very handy.

Ky 2.0 now requires Node.js 22, hook callbacks must move to the single state object form, .json() throws on empty and 204 responses instead of returning an empty string, searchParams now merges with any query parameters already in the URL, and error bodies must be read from error.data rather than error.response.json(). The release notes ship a full migration guide with before-and-after examples for each change.

Ky is an open-source HTTP client maintained by Sindre Sorhus, Seth Holladay and Szymon Marczak. At just a few kilobytes, and working across browsers, Node.js, Bun and Deno, it positions itself as a lighter, fetch-native alternative to axios.

About the Author

Rate this Article

Adoption
Style

BT