Huge thanks to
for sponsoring me!
> Ky is a tiny and elegant HTTP client based on the browser [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch)
[](https://travis-ci.com/sindresorhus/ky) [](https://codecov.io/gh/sindresorhus/ky) [](https://bundlephobia.com/result?p=ky)
Ky targets [modern browsers](#browser-support) and [Deno](https://github.com/denoland/deno). For older browsers, you will need to transpile and use a [`fetch` polyfill](https://github.com/github/fetch). For Node.js, check out [Got](https://github.com/sindresorhus/got). For isomorphic needs (like SSR), check out [`ky-universal`](https://github.com/sindresorhus/ky-universal).
It's just a tiny file with no dependencies.
## Benefits over plain `fetch`
- Simpler API
- Method shortcuts (`ky.post()`)
- Treats non-2xx status codes as errors
- Retries failed requests
- JSON option
- Timeout support
- URL prefix option
- Instances with custom defaults
- Hooks
## Install
```
$ npm install ky
```
###### Download
- [Normal](https://cdn.jsdelivr.net/npm/ky/index.js)
- [Minified](https://cdn.jsdelivr.net/npm/ky/index.min.js)
###### CDN
- [jsdelivr](https://www.jsdelivr.com/package/npm/ky)
- [unpkg](https://unpkg.com/ky)
## Usage
```js
import ky from 'ky';
(async () => {
const parsed = await ky.post('https://example.com', {json: {foo: true}}).json();
console.log(parsed);
//=> `{data: '🦄'}`
})();
```
With plain `fetch`, it would be:
```js
(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 parsed = await response.json();
console.log(parsed);
//=> `{data: '🦄'}`
})();
```
If you are using [Deno](https://github.com/denoland/deno), import Ky from a URL. For example, using a CDN:
```js
import ky from 'https://unpkg.com/ky/index.js';
```
In environments that do not support `import`, you can load `ky` in [UMD format](https://medium.freecodecamp.org/anatomy-of-js-module-systems-and-building-libraries-fadcd8dbd0e). For example, using `require()`:
```js
const ky = require('ky/umd').default;
```
With the UMD version, it's also easy to use `ky` [without a bundler](#how-do-i-use-this-without-a-bundler-like-webpack) or module system.
## API
### ky(input, [options])
The `input` and `options` are the same as [`fetch`](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/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](https://developer.mozilla.org/en-US/docs/Web/API/Response) with [`Body` methods](https://developer.mozilla.org/en-US/docs/Web/API/Body#Methods) added for convenience. So you can, for example, call `ky.get(input).json()` directly without having to await the `Response` first. When called like that, proper `Accept` header will be set depending on body method used. 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.
#### options
Type: `object`
##### method
Type: `string`