Module · (root)
ky
ky is a small HTTP client built on top of the Fetch API. The package entry point source/index.ts composes the public KyInstance from the core Ky class, the HTTP method shortcuts in requestMethods, the stop and retry constants, and the option-merging helper validateAndMerge. Calling ky(input, options?) returns a Response object augmented with body-method shortcuts that throw on non-2xx status codes.
The root README (readme.md) is the canonical reference: it documents the call signature, every supported option, the error hierarchy, retry/cancellation behavior, and Node.js-specific features (proxy and HTTP/2). The declarations below are headings within that document plus the top-level exports surfaced from source/index.ts.
Core call signature
ky(input, options?)— Takes the sameinput/optionsasfetch, with extra Ky-specific options. Returns aResponsewhose.json(),.text(),.formData(),.arrayBuffer(),.blob(), and.bytes()shortcuts throw anHTTPErroroutside the200...299range;.json()additionally throws on empty bodies or status204. An appropriateAcceptheader is set based on the body method used.
Benefits over plain fetch
The Benefits over plain fetch section enumerates the value-add: method shortcuts, treating non-2xx as errors, retries, JSON option, timeout, upload/download progress, base URL, instances with custom defaults, hooks, Standard Schema response validation, and TypeScript niceties such as .json() defaulting to unknown.
Method shortcuts
ky.get, ky.post, ky.put, ky.patch, ky.head, and ky.delete are thin wrappers around ky(input, options?) that set the corresponding HTTP method. The README's ky.delete heading is split across 14 ranges (part 1 … part 14), under which most option documentation lives (e.g. parseJson, stringifyJson, fetch).
Notable options documented under the method sections
json— Plain object or value stringified viaJSON.stringify()with the appropriateContent-Typeheader.parseJson— Custom JSON parser receiving(text, {request, response}); defaults toJSON.parse(). Used for e.g.@hapijs/bourneprototype-pollution protection.stringifyJson— Custom replacer; defaults toJSON.stringify().fetch— User-supplied Fetch-compatible function for SSR wrappers or instrumentation. Defaults to the globalfetch.onUploadProgress— Callback(progress, chunk: Uint8Array) => voidinvoked with transferred/total byte counts.
Instances and defaults
ky.extend(defaultOptions)— Returns a newKyInstancethat inherits and merges defaults from the parent.ky.create(defaultOptions)— Returns a freshKyInstancewith no inherited defaults.replaceOption— Sentinel used indefaultOptionsto fully replace (rather than merge) a particular option.
Control flow constants
ky.stop— Sentinel returned from abeforeRetryhook to abort retrying.ky.retry(options?)— Configures retry behavior (count, methods, status codes, backoff, delay).
Errors
The error hierarchy exported from the package root:
KyError— Base class.HTTPError— Thrown for non-2xx responses (subject tothrowHttpErrors; can be thrown manually fromafterResponsehooks, as shown in the FAQ).SchemaValidationError— Thrown when a Standard Schema validation fails.TimeoutError— Thrown when the request exceeds the configured timeout.NetworkError— Thrown for underlying transport failures.
Tips
The Tips section covers practical recipes:
- Sending form data
- Setting a custom
Content-Type - Cancellation (via
AbortController) - Proxy support (Node.js)
- HTTP/2 support (Node.js)
Declarations
| doc | Benefits over plain `fetch` | :43 |
| doc | Install | :58 |
| doc | Usage | :73 |
| doc | API | :113 |
| doc | ky(input, options?) | :115 |
| doc | ky.get(input, options?) | :170 |
| doc | ky.post(input, options?) | :171 |
| doc | ky.put(input, options?) | :172 |
| doc | ky.patch(input, options?) | :173 |
| doc | ky.head(input, options?) | :174 |
| doc | ky.delete(input, options?) (part 1) | :175 |
| doc | ky.delete(input, options?) (part 2) | :232 |
| doc | ky.delete(input, options?) (part 3) | :289 |
| doc | ky.delete(input, options?) (part 4) | :346 |
| doc | ky.delete(input, options?) (part 5) | :403 |
| doc | ky.delete(input, options?) (part 6) | :460 |
| doc | ky.delete(input, options?) (part 7) | :517 |
| doc | ky.delete(input, options?) (part 8) | :574 |
| doc | ky.delete(input, options?) (part 9) | :631 |
| doc | ky.delete(input, options?) (part 10) | :688 |
| doc | ky.delete(input, options?) (part 11) | :745 |
| doc | ky.delete(input, options?) (part 12) | :802 |
| doc | ky.delete(input, options?) (part 13) | :859 |
| doc | ky.delete(input, options?) (part 14) | :916 |
| doc | ky.extend(defaultOptions) | :940 |
| doc | ky.create(defaultOptions) | :1026 |
| doc | ky.stop | :1048 |
| doc | ky.retry(options?) | :1079 |
| doc | KyError | :1218 |
| doc | HTTPError | :1239 |
| doc | SchemaValidationError | :1284 |
| doc | TimeoutError | :1306 |
| doc | NetworkError | :1322 |
| doc | replaceOption | :1344 |
| doc | Tips | :1362 |
| doc | Sending form data | :1364 |
| doc | Setting a custom `Content-Type` | :1421 |
| doc | Cancellation | :1441 |
| doc | Proxy support (Node.js) | :1468 |
| doc | HTTP/2 support (Node.js) | :1519 |
| doc | Streaming request bodies | :1557 |
| doc | Consuming Server-Sent Events (SSE) | :1580 |
| doc | Pagination | :1595 |
| doc | Extending types | :1610 |
| doc | FAQ (part 1) | :1641 |
| doc | FAQ (part 2) | :1715 |
| doc | FAQ (part 3) | :1789 |
| doc | Browser support | :1850 |
| doc | Node.js support | :1854 |
| doc | Related | :1858 |
| doc | Maintainers | :1863 |