0
# HTTP Methods
1
2
Core HTTP request methods with convenient shortcuts and full TypeScript support for all common HTTP operations.
3
4
## Capabilities
5
6
### Main Request Function
7
8
The primary ky function for making HTTP requests with full customization options.
9
10
```typescript { .api }
11
/**
12
* Fetch the given URL with optional configuration
13
* @param url - Request target (string, URL, or Request object)
14
* @param options - Request configuration options
15
* @returns Promise with response body methods
16
*/
17
function ky<T>(url: Input, options?: Options): ResponsePromise<T>;
18
```
19
20
**Usage Examples:**
21
22
```typescript
23
import ky from "ky";
24
25
// Basic request
26
const response = await ky("https://api.example.com/data");
27
28
// With options
29
const data = await ky("https://api.example.com/users", {
30
method: "POST",
31
json: { name: "Alice", email: "alice@example.com" },
32
timeout: 5000
33
}).json();
34
35
// With TypeScript generics
36
interface User {
37
id: number;
38
name: string;
39
email: string;
40
}
41
42
const user = await ky<User>("https://api.example.com/user/123").json();
43
```
44
45
### GET Method
46
47
Fetch data using the GET HTTP method.
48
49
```typescript { .api }
50
/**
51
* Fetch the given URL using GET method
52
* @param url - Request target (string, URL, or Request object)
53
* @param options - Request configuration options
54
* @returns Promise with response body methods
55
*/
56
function get<T>(url: Input, options?: Options): ResponsePromise<T>;
57
```
58
59
**Usage Examples:**
60
61
```typescript
62
import ky from "ky";
63
64
// Simple GET request
65
const users = await ky.get("https://api.example.com/users").json();
66
67
// GET with query parameters
68
const filteredUsers = await ky.get("https://api.example.com/users", {
69
searchParams: { status: "active", limit: 10 }
70
}).json();
71
72
// GET with headers
73
const data = await ky.get("https://api.example.com/protected", {
74
headers: { "Authorization": "Bearer token123" }
75
}).json();
76
```
77
78
### POST Method
79
80
Send data using the POST HTTP method.
81
82
```typescript { .api }
83
/**
84
* Fetch the given URL using POST method
85
* @param url - Request target (string, URL, or Request object)
86
* @param options - Request configuration options
87
* @returns Promise with response body methods
88
*/
89
function post<T>(url: Input, options?: Options): ResponsePromise<T>;
90
```
91
92
**Usage Examples:**
93
94
```typescript
95
import ky from "ky";
96
97
// POST with JSON data
98
const newUser = await ky.post("https://api.example.com/users", {
99
json: { name: "Bob", email: "bob@example.com" }
100
}).json();
101
102
// POST with form data
103
const formData = new FormData();
104
formData.append("name", "Charlie");
105
formData.append("file", fileInput.files[0]);
106
107
const result = await ky.post("https://api.example.com/upload", {
108
body: formData
109
}).json();
110
111
// POST with custom headers
112
const response = await ky.post("https://api.example.com/data", {
113
json: { value: 42 },
114
headers: { "Content-Type": "application/json" }
115
}).json();
116
```
117
118
### PUT Method
119
120
Update resources using the PUT HTTP method.
121
122
```typescript { .api }
123
/**
124
* Fetch the given URL using PUT method
125
* @param url - Request target (string, URL, or Request object)
126
* @param options - Request configuration options
127
* @returns Promise with response body methods
128
*/
129
function put<T>(url: Input, options?: Options): ResponsePromise<T>;
130
```
131
132
**Usage Examples:**
133
134
```typescript
135
import ky from "ky";
136
137
// PUT to update a resource
138
const updatedUser = await ky.put("https://api.example.com/users/123", {
139
json: { name: "Alice Updated", email: "alice.new@example.com" }
140
}).json();
141
142
// PUT with full resource replacement
143
const resource = await ky.put("https://api.example.com/documents/456", {
144
json: {
145
title: "New Title",
146
content: "Full content replacement",
147
version: 2
148
}
149
}).json();
150
```
151
152
### PATCH Method
153
154
Partially update resources using the PATCH HTTP method.
155
156
```typescript { .api }
157
/**
158
* Fetch the given URL using PATCH method
159
* @param url - Request target (string, URL, or Request object)
160
* @param options - Request configuration options
161
* @returns Promise with response body methods
162
*/
163
function patch<T>(url: Input, options?: Options): ResponsePromise<T>;
164
```
165
166
**Usage Examples:**
167
168
```typescript
169
import ky from "ky";
170
171
// PATCH for partial updates
172
const updatedUser = await ky.patch("https://api.example.com/users/123", {
173
json: { email: "newemail@example.com" }
174
}).json();
175
176
// PATCH with JSON Patch format
177
const result = await ky.patch("https://api.example.com/documents/456", {
178
json: [
179
{ op: "replace", path: "/title", value: "Updated Title" },
180
{ op: "add", path: "/tags", value: ["important"] }
181
],
182
headers: { "Content-Type": "application/json-patch+json" }
183
}).json();
184
```
185
186
### DELETE Method
187
188
Delete resources using the DELETE HTTP method.
189
190
```typescript { .api }
191
/**
192
* Fetch the given URL using DELETE method
193
* @param url - Request target (string, URL, or Request object)
194
* @param options - Request configuration options
195
* @returns Promise with response body methods
196
*/
197
function delete<T>(url: Input, options?: Options): ResponsePromise<T>;
198
```
199
200
**Usage Examples:**
201
202
```typescript
203
import ky from "ky";
204
205
// Simple DELETE request
206
await ky.delete("https://api.example.com/users/123");
207
208
// DELETE with response processing
209
const result = await ky.delete("https://api.example.com/documents/456").json();
210
211
// DELETE with authorization
212
await ky.delete("https://api.example.com/posts/789", {
213
headers: { "Authorization": "Bearer token123" }
214
});
215
```
216
217
### HEAD Method
218
219
Get resource headers using the HEAD HTTP method.
220
221
```typescript { .api }
222
/**
223
* Fetch the given URL using HEAD method
224
* @param url - Request target (string, URL, or Request object)
225
* @param options - Request configuration options
226
* @returns Promise with response (no body methods)
227
*/
228
function head(url: Input, options?: Options): ResponsePromise;
229
```
230
231
**Usage Examples:**
232
233
```typescript
234
import ky from "ky";
235
236
// Check if resource exists
237
const response = await ky.head("https://api.example.com/users/123");
238
console.log(response.status); // 200 if exists, 404 if not
239
240
// Get content info without downloading
241
const fileInfo = await ky.head("https://api.example.com/files/large.zip");
242
console.log(fileInfo.headers.get("content-length"));
243
console.log(fileInfo.headers.get("content-type"));
244
245
// Check last modified date
246
const docInfo = await ky.head("https://api.example.com/documents/456");
247
console.log(docInfo.headers.get("last-modified"));
248
```
249
250
## Types
251
252
```typescript { .api }
253
type Input = string | URL | Request;
254
255
type HttpMethod = 'get' | 'post' | 'put' | 'patch' | 'head' | 'delete';
256
257
interface ResponsePromise<T = unknown> extends Promise<KyResponse<T>> {
258
arrayBuffer(): Promise<ArrayBuffer>;
259
blob(): Promise<Blob>;
260
formData(): Promise<FormData>;
261
bytes(): Promise<Uint8Array>;
262
json<J = T>(): Promise<J>;
263
text(): Promise<string>;
264
}
265
```