CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-ky

Tiny and elegant HTTP client based on the Fetch API

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

http-methods.mddocs/

HTTP Methods

Core HTTP request methods with convenient shortcuts and full TypeScript support for all common HTTP operations.

Capabilities

Main Request Function

The primary ky function for making HTTP requests with full customization options.

/**
 * Fetch the given URL with optional configuration
 * @param url - Request target (string, URL, or Request object)
 * @param options - Request configuration options
 * @returns Promise with response body methods
 */
function ky<T>(url: Input, options?: Options): ResponsePromise<T>;

Usage Examples:

import ky from "ky";

// Basic request
const response = await ky("https://api.example.com/data");

// With options
const data = await ky("https://api.example.com/users", {
  method: "POST",
  json: { name: "Alice", email: "alice@example.com" },
  timeout: 5000
}).json();

// With TypeScript generics
interface User {
  id: number;
  name: string;
  email: string;
}

const user = await ky<User>("https://api.example.com/user/123").json();

GET Method

Fetch data using the GET HTTP method.

/**
 * Fetch the given URL using GET method
 * @param url - Request target (string, URL, or Request object)
 * @param options - Request configuration options
 * @returns Promise with response body methods
 */
function get<T>(url: Input, options?: Options): ResponsePromise<T>;

Usage Examples:

import ky from "ky";

// Simple GET request
const users = await ky.get("https://api.example.com/users").json();

// GET with query parameters
const filteredUsers = await ky.get("https://api.example.com/users", {
  searchParams: { status: "active", limit: 10 }
}).json();

// GET with headers
const data = await ky.get("https://api.example.com/protected", {
  headers: { "Authorization": "Bearer token123" }
}).json();

POST Method

Send data using the POST HTTP method.

/**
 * Fetch the given URL using POST method
 * @param url - Request target (string, URL, or Request object)  
 * @param options - Request configuration options
 * @returns Promise with response body methods
 */
function post<T>(url: Input, options?: Options): ResponsePromise<T>;

Usage Examples:

import ky from "ky";

// POST with JSON data
const newUser = await ky.post("https://api.example.com/users", {
  json: { name: "Bob", email: "bob@example.com" }
}).json();

// POST with form data
const formData = new FormData();
formData.append("name", "Charlie");
formData.append("file", fileInput.files[0]);

const result = await ky.post("https://api.example.com/upload", {
  body: formData
}).json();

// POST with custom headers
const response = await ky.post("https://api.example.com/data", {
  json: { value: 42 },
  headers: { "Content-Type": "application/json" }
}).json();

PUT Method

Update resources using the PUT HTTP method.

/**
 * Fetch the given URL using PUT method
 * @param url - Request target (string, URL, or Request object)
 * @param options - Request configuration options
 * @returns Promise with response body methods
 */
function put<T>(url: Input, options?: Options): ResponsePromise<T>;

Usage Examples:

import ky from "ky";

// PUT to update a resource
const updatedUser = await ky.put("https://api.example.com/users/123", {
  json: { name: "Alice Updated", email: "alice.new@example.com" }
}).json();

// PUT with full resource replacement
const resource = await ky.put("https://api.example.com/documents/456", {
  json: {
    title: "New Title",
    content: "Full content replacement",
    version: 2
  }
}).json();

PATCH Method

Partially update resources using the PATCH HTTP method.

/**
 * Fetch the given URL using PATCH method
 * @param url - Request target (string, URL, or Request object)
 * @param options - Request configuration options
 * @returns Promise with response body methods
 */
function patch<T>(url: Input, options?: Options): ResponsePromise<T>;

Usage Examples:

import ky from "ky";

// PATCH for partial updates
const updatedUser = await ky.patch("https://api.example.com/users/123", {
  json: { email: "newemail@example.com" }
}).json();

// PATCH with JSON Patch format
const result = await ky.patch("https://api.example.com/documents/456", {
  json: [
    { op: "replace", path: "/title", value: "Updated Title" },
    { op: "add", path: "/tags", value: ["important"] }
  ],
  headers: { "Content-Type": "application/json-patch+json" }
}).json();

DELETE Method

Delete resources using the DELETE HTTP method.

/**
 * Fetch the given URL using DELETE method
 * @param url - Request target (string, URL, or Request object)
 * @param options - Request configuration options
 * @returns Promise with response body methods
 */
function delete<T>(url: Input, options?: Options): ResponsePromise<T>;

Usage Examples:

import ky from "ky";

// Simple DELETE request
await ky.delete("https://api.example.com/users/123");

// DELETE with response processing
const result = await ky.delete("https://api.example.com/documents/456").json();

// DELETE with authorization
await ky.delete("https://api.example.com/posts/789", {
  headers: { "Authorization": "Bearer token123" }
});

HEAD Method

Get resource headers using the HEAD HTTP method.

/**
 * Fetch the given URL using HEAD method
 * @param url - Request target (string, URL, or Request object)
 * @param options - Request configuration options
 * @returns Promise with response (no body methods)
 */
function head(url: Input, options?: Options): ResponsePromise;

Usage Examples:

import ky from "ky";

// Check if resource exists
const response = await ky.head("https://api.example.com/users/123");
console.log(response.status); // 200 if exists, 404 if not

// Get content info without downloading
const fileInfo = await ky.head("https://api.example.com/files/large.zip");
console.log(fileInfo.headers.get("content-length"));
console.log(fileInfo.headers.get("content-type"));

// Check last modified date
const docInfo = await ky.head("https://api.example.com/documents/456");
console.log(docInfo.headers.get("last-modified"));

Types

type Input = string | URL | Request;

type HttpMethod = 'get' | 'post' | 'put' | 'patch' | 'head' | 'delete';

interface ResponsePromise<T = unknown> extends Promise<KyResponse<T>> {
  arrayBuffer(): Promise<ArrayBuffer>;
  blob(): Promise<Blob>;
  formData(): Promise<FormData>;
  bytes(): Promise<Uint8Array>;
  json<J = T>(): Promise<J>;
  text(): Promise<string>;
}

docs

configuration.md

errors.md

hooks.md

http-methods.md

index.md

instances.md

responses.md

retry.md

tile.json