Back to articles
Ky Has a Free API That Makes HTTP Requests in JavaScript Elegant

Ky Has a Free API That Makes HTTP Requests in JavaScript Elegant

via Dev.to JavaScriptAlex Spinov

Ky is a tiny HTTP client by Sindre Sorhus. It wraps fetch with retries, timeouts, hooks, and JSON handling — in just 3KB. Basic Usage import ky from " ky " ; // GET with JSON parsing const data = await ky . get ( " https://api.example.com/products " ). json (); // POST const created = await ky . post ( " https://api.example.com/products " , { json : { title : " Widget " , price : 29.99 }, }). json (); // PUT, PATCH, DELETE await ky . put ( " https://api.example.com/products/1 " , { json : { price : 24.99 } }); await ky . delete ( " https://api.example.com/products/1 " ); Retry: Built-In Resilience const data = await ky . get ( " https://api.example.com/data " , { retry : { limit : 3 , methods : [ " get " ], statusCodes : [ 408 , 429 , 500 , 502 , 503 , 504 ], backoffLimit : 3000 , }, timeout : 10000 , }). json (); Hooks: Intercept Requests const api = ky . create ({ prefixUrl : " https://api.example.com " , hooks : { beforeRequest : [ ( request ) => { request . headers . set ( " Author

Continue reading on Dev.to JavaScript

Opens in a new tab

Read Full Article
3 views

Related Articles