Tiny and elegant HTTP client based on the browser Fetch API
Switch branches/tags
Nothing to show
Clone or download
Type Name Latest commit message Commit time
Failed to load latest commit information.
cypress Fix relative URL resolution (#59) Nov 6, 2018
media Init Sep 4, 2018
test Remove moot ESLint disable comment Nov 30, 2018
.editorconfig Init Sep 4, 2018
.gitattributes Init Sep 4, 2018
.gitignore Init Sep 4, 2018
.npmrc Init Sep 4, 2018
.travis.yml Init Sep 4, 2018
cypress.json Set up integration testing with Cypress Oct 31, 2018
index.d.ts Minor tweaks to type definition tweaks and docs Nov 5, 2018
index.js Fix relative URL resolution (#59) Nov 6, 2018
index.test-d.ts Fixup #57 Nov 1, 2018
license Init Sep 4, 2018
package.json 0.5.1 Nov 6, 2018
readme.md Minor tweaks to type definition tweaks and docs Nov 5, 2018



Ky is a tiny and elegant HTTP client based on the browser Fetch API

Build Status codecov

Ky targets modern browsers. For older browsers, you will need to transpile and use a fetch polyfill. For Node.js, check out Got.

1 KB (minified & gzipped), one file, and no dependencies.

Benefits over plain fetch

  • Simpler API
  • Method shortcuts (ky.post())
  • Treats non-200 status codes as errors
  • Retries failed requests
  • JSON option
  • Timeout support
  • URL prefix option
  • Instances with custom defaults
  • Hooks


$ npm install ky


import ky from 'ky';

(async () => {
	const json = await ky.post('https://example.com', {json: {foo: true}}).json();

	//=> `{data: '🦄'}`

With plain fetch, it would be:

(async () => {
	class HTTPError extends Error {}

	const response = await fetch('https://example.com', {
		method: 'POST',
		body: JSON.stringify({foo: true}),
		headers: {
			'content-type': 'application/json'

	if (!response.ok) {
		throw new HTTPError('Fetch error:', response.statusText);

	const json = await response.json();

	//=> `{data: '🦄'}`


ky(input, [options])

The input and options are the same as fetch, with some exceptions:

  • The credentials option is same-origin by default, which is the default in the spec too, but not all browsers have caught up yet.
  • Adds some more options. See below.

Returns a Response object with Body methods added for convenience. So you can, for example, call ky.json() directly on the Response without having to await it first. Unlike the Body methods of window.Fetch; these will throw an HTTPError if the response status is not in the range 200...299.

ky.get(input, [options])

ky.post(input, [options])

ky.put(input, [options])

ky.patch(input, [options])

ky.head(input, [options])

ky.delete(input, [options])

Sets options.method to the method name and makes a request.


Type: Object


Type: Object

Shortcut for sending JSON. Use this instead of the body option. Accepts a plain object which will be JSON.stringify()'d and the correct header will be set for you.


Type: string Object<string, string|number> URLSearchParams
Default: ''

Search parameters to include in the request URL. Setting this will override all existing search parameters in the input URL.


Type: string URL

When specified, prefixUrl will be prepended to input. The prefix can be any valid URL, either relative or absolute. A trailing slash / is optional, one will be added automatically, if needed, when joining prefixUrl and input. The input argument cannot start with a / when using this option.

Useful when used with ky.extend() to create niche-specific Ky-instances.

import ky from 'ky';

// On https://example.com

(async () => {
	await ky('unicorn', {prefixUrl: '/api'});
	//=> 'https://example.com/api/unicorn'

	await ky('unicorn', {prefixUrl: 'https://cats.com'});
	//=> 'https://cats.com/unicorn'

Type: number
Default: 2

Retry failed requests made with one of the below methods that result in a network error or one of the below status codes.

Status codes: 408 413 429 500 502 503 504

It adheres to the Retry-After response header.


Type: number
Default: 10000

Timeout in milliseconds for getting a response.


Type: Object<string, Function[]>
Default: {beforeRequest: []}

Hooks allow modifications during the request lifecycle. Hook functions may be async and are run serially.


Type: Function[]
Default: []

This hook enables you to modify the request right before it is sent. Ky will make no further changes to the request after this. The hook function receives the normalized options as the first argument. You could, for example, modify options.headers here.


Type: Function[]
Default: []

This hook enables you to read and optionally modify the response. The hook function receives a clone of the response as the first argument. The return value of the hook function will be used by Ky as the response object if it's an instance of Response.

ky.get('https://example.com', {
	hooks: {
		afterResponse: [
			response => {
				// You could do something with the response, for example, logging.

				// Or return a `Response` instance to overwrite the response.
				return new Response('A different response', {status: 200});

Type: boolean
Default: true

Throw a HTTPError for error responses (non-2xx status codes).

Setting this to false may be useful if you are checking for resource availability and are expecting error responses.


Create a new ky instance with some defaults overridden with your own.

import ky from 'ky';

// On https://my-site.com

const api = ky.extend({prefixUrl: 'https://example.com/api'});

(async () => {
	await api.get('/users/123');
	//=> 'https://example.com/api/users/123'

	await api.get('/status', {prefixUrl: ''});
	//=> 'https://my-site.com/status'


Type: Object


Exposed for instanceof checks. The error has a response property with the Response object.


The error thrown when the request times out.



Fetch (and hence Ky) has built-in support for request cancelation through the AbortController API. Read more.


import ky from 'ky';

const controller = new AbortController();
const {signal} = controller;

setTimeout(() => controller.abort(), 5000);

(async () => {
	try {
		console.log(await ky(url, {signal}).text());
	} catch (error) {
		if (error.name === 'AbortError') {
			console.log('Fetch aborted');
		} else {
			console.error('Fetch error:', error);


How is it different from got

See my answer here. Got is maintained by the same people as Ky.

How is it different from axios?

See my answer here.

How is it different from r2?

See my answer in #10.

What does ky mean?

It's just a random short npm package name I managed to get. It does, however, have a meaning in Japanese:

A form of text-able slang, KY is an abbreviation for 空気読めない (kuuki yomenai), which literally translates into “cannot read the air.” It's a phrase applied to someone who misses the implied meaning.

Browser support

The latest version of Chrome, Firefox, and Safari.


  • got - Simplified HTTP requests for Node.js