ky
Tiny, promise-based HTTP client built on fetch, with retries, timeouts, hooks, and prefix-based URL shortcuts.
Official Documentation ↗v2.2.0. Check the official docs for the latest API surface.Installation
npm install kypnpm add kyyarn add kyMethods
No methods match this filter.
Overview
ky is a small fetch wrapper that keeps the standard Request/Response model while adding ergonomic defaults: automatic JSON handling, request prefixing, timeouts, retries, and hooks. It stays close to the platform fetch API so knowledge transfers directly.
Core Methods
The instance methods mirror HTTP verbs. Every method accepts a URL and an options object that extends the standard RequestInit with ky-specific fields such as json, searchParams, prefixUrl, timeout, and retry.
Usage Notes
Create one configured instance per API and reuse it. Use hooks to attach auth headers and to normalize error responses in a single place instead of repeating logic on every call site.
Common Pitfalls
Do not pass an absolute path when prefixUrl is set. Retries should not be combined with non-idempotent requests unless the server supports idempotency keys, or duplicate writes may occur.
Errors
The following points are quoted verbatim from the official documentation (https://github.com/sindresorhus/ky). Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.
- Unlike the Body methods of window.fetch, these will throw an HTTPError if the response status is not in the range of 200…299. (https://github.com/sindresorhus/ky)
- Note that .json() will throw if the body is empty, as it cannot be parsed. (https://github.com/sindresorhus/ky)
- Invalid options, an invalid input, and an error thrown by an init hook reject the returned promise instead of throwing, the same way fetch() reports them. (https://github.com/sindresorhus/ky)
FAQ
- How does ky handle non-2xx responses?
- By default ky rejects the promise with an HTTPError for non-2xx statuses. Set throwHttpErrors: false to receive the response and inspect status yourself. Reads via .json()/text() still need the response object.
- What is the difference between prefixUrl and a base URL?
- prefixUrl is prepended and must end with a slash; the request path must not start with a slash. This avoids the classic base-URL join bug where an absolute-looking path wipes the prefix.
- Can ky be used in Node.js?
- ky relies on the global fetch API. Node 18 and later expose fetch globally, so ky works there. On older runtimes you must provide a fetch polyfill or use a different client.
Report a problem with this API docOpens a pre-filled issue on GitHub.