Skip to content
D
Documentation

JavaScript quick start

tutorial
1 min readUpdated

Install Ky and make a JSON request with the default client.

Ky is a Fetch-based HTTP client with a simpler request and response interface. Use the ky default export with Fetch-compatible inputs and options; its response provides direct body shortcuts such as .json().

Prerequisites

  • npm.
  • A JavaScript or TypeScript project that can use ES modules.

Ky targets modern browsers, Node.js, Bun, and Deno.

1. Install Ky

Run this command in your project:

bash
npm install ky

The command adds Ky to the project.

Handle an HTTP failure

Non-2xx responses throw an HTTPError when HTTP errors are enabled. Use the isHTTPError type guard when the caller needs to inspect the status:

ts
import ky, {isHTTPError} from 'ky';

const main = async (): Promise<void> => {
	try {
		const user = await ky.get('https://jsonplaceholder.typicode.com/users/1').json();
		console.log(user);
	} catch (error) {
		if (isHTTPError(error)) {
			console.error(`Request failed with status ${error.response.status}`);
		} else {
			throw error;
		}
	}
};

void main();

The successful path receives parsed JSON. The failure path receives an HTTPError with the response status; network failures do not have a response because no response was received.

What you have now

Your project can install Ky, send a request with the default client, and receive parsed JSON through .json(). For the request model and response shortcuts, continue to Requests and responses. To reuse URL and request defaults, see Instances and defaults.

Was this page helpful?