talira

Tiny & elegant JavaScript HTTP client based on the Fetch API

open sourceMITself-hostablefree

Ky is a small HTTP client for JavaScript and TypeScript that wraps the standard Fetch API, so the same code runs in browsers, Node.js, Bun and Deno, with no dependencies. On top of fetch it adds method shortcuts, errors for non-2xx responses, retries with backoff, per-attempt and total timeouts, upload and download progress, a base URL, instances with shared defaults, and hooks before and after each request. Responses can be validated against a Standard Schema validator such as Zod, and .json() is typed unknown by default rather than any. Sindre Sorhus, Seth Holladay and Szymon Marczak maintain it, and the README of Got, their Node.js-only client, recommends Ky instead. Its FAQ contrasts it with axios, which predates fetch.

Problems it solves

  • plain fetch doesn't throw on 404 or 500 responses and I keep forgetting to check response.ok
  • retry failed API calls automatically with backoff from browser and Node.js code
  • put a timeout on fetch calls without wiring up AbortController by hand every time
  • I want a tiny HTTP client with zero dependencies that works in the browser, Node.js, Bun and Deno
  • create a preconfigured API client with a base URL, default headers and an auth token on every request
  • refresh an expired access token and replay the request when the API answers 401
  • validate the JSON an API returns against a Zod schema and get a typed result
  • show a progress bar while uploading or downloading a file with fetch
  • stop a huge or malicious response body from being read into memory: set a maximum response size
  • give a whole request, retries included, one overall deadline

Features

  • built on the Fetch API, no dependencies
  • method shortcuts (ky.get, ky.post, ky.put, ky.patch, ky.delete, ky.head, ky.query)
  • HTTPError for non-2xx responses, configurable per status
  • retries with backoff, Retry-After support and custom retry rules
  • per-attempt timeout and an overall totalTimeout
  • maximum response size
  • upload and download progress callbacks
  • baseUrl and prefix options, searchParams
  • instances with shared defaults (ky.create, ky.extend)
  • hooks: init, beforeRequest, beforeRetry, beforeError, afterResponse
View 3 more
  • response validation with Standard Schema (Zod, Valibot)
  • TypeScript types, .json() typed unknown by default
  • undici dispatcher pass-through for proxies and HTTP/2 in Node.js

Limitations

  • Doesn't run on Node.js versions before 22: it needs Node.js 22 or later and the latest Chrome, Firefox or Safari1

Capabilities

Has an APIplan not known

Alternatives

The same job

These do what Ky does.

  1. Got Human-friendly and powerful HTTP request library for Node.js
  2. undici An HTTP/1.1 client, written from scratch for Node.js
  3. ofetch A better fetch API. Works on node, browser, and workers.
  4. axios Promise based HTTP client for the browser and node.js
  5. SuperAgent Ajax for Node.js and browsers (JS HTTP client)
  6. node-fetch A light-weight module that brings the Fetch API to Node.js

Compare Ky with the top 3