Ky is a tiny, elegant HTTP client for the browser and Deno. Built on fetch, it provides retry, hooks, timeout, and JSON parsing in just 3KB.
Basic Requests
import ky from "ky";
const data = await ky.get("https://api.example.com/users").json();
const created = await ky.post("https://api.example.com/users", { json: { name: "John" } }).json();
const updated = await ky.put("https://api.example.com/users/1", { json: { name: "Jane" } }).json();
await ky.delete("https://api.example.com/users/1");
Instance with Defaults
const api = ky.create({
prefixUrl: "https://api.example.com",
headers: { Authorization: `Bearer ${token}` },
timeout: 30000,
retry: { limit: 3, methods: ["get"], statusCodes: [408, 429, 500, 502, 503] }
});
const users = await api.get("users").json();
Hooks
const api = ky.create({
hooks: {
beforeRequest: [
(request) => { request.headers.set("X-Request-ID", crypto.randomUUID()); }
],
afterResponse: [
async (request, options, response) => {
if (response.status === 401) {
const token = await refreshToken();
request.headers.set("Authorization", `Bearer ${token}`);
return ky(request);
}
}
]
}
});
Key Features
- 3KB gzipped
- Built on native fetch
- Automatic retries
- Request/response hooks
- Timeout support
- JSON shorthand
Need to scrape or monitor web data at scale? Check out my web scraping actors on Apify or email spinov001@gmail.com for custom solutions.
Top comments (0)