Skip to content
D
Documentation

Ky quick start

tutorial
1 min readUpdated

Install Ky, send a JSON request with a custom header, and read the JSON response body.

Ky is an HTTP client based on the Fetch API: pass a URL and options, then use a method shortcut and a body method to make a request and read its response.

Prerequisites

Ky targets modern browsers, Node.js, Bun, and Deno. The package requires Node.js 22 or newer.

Install

bash
npm install ky

Send a JSON request

  1. Import the default ky export. Use json for the request body and headers for the custom request header. The post() shortcut sends a POST request; json() reads and parses the response body.

    ts
    import ky from 'ky';
    
    const main = async (): Promise<void> => {
      const result = await ky.post('https://httpbin.org/anything', {
        json: {name: 'Ada'},
        headers: {
          'x-client': 'ky-quick-start',
        },
      }).json();
    
      console.log(result);
    };
    
    void main();
    

    Inspect result in the console to see the parsed JSON response body. Its type is unknown by default; use a type argument when you know the response shape, or validate the response as described in Send and read JSON.

What you have

Your code sends a POST request with a JSON body and the x-client header, then logs the parsed response body. Ky sets the JSON content type for the json option unless you provide a Content-Type header yourself. A non-2xx response throws an HTTPError by default, and .json() also throws if the response body is empty.

Where to go next

Was this page helpful?

Ky quick start — ky · GPT-6 Luna