got is a human-friendly and powerful HTTP request library for Node.js. It supports retries, streams, pagination, caching, and more — making it ideal for building robust API integrations.
Installation
npm install got
Basic Requests
import got from "got";
// GET with JSON parsing
const data = await got("https://api.github.com/repos/sindresorhus/got").json();
console.log(`${data.full_name}: ${data.stargazers_count} stars`);
// POST with JSON body
const response = await got.post("https://httpbin.org/post", {
json: { tool: "got", version: 14 }
}).json();
Instances with Defaults
const api = got.extend({
prefixUrl: "https://api.example.com/v2",
headers: { "Authorization": "Bearer token" },
retry: { limit: 3 },
timeout: { request: 10000 },
responseType: "json"
});
const users = await api.get("users").json();
const user = await api.post("users", { json: { name: "New" } }).json();
Pagination — Auto-Fetch All Pages
const allItems = [];
for await (const page of got.paginate("https://api.example.com/items", {
responseType: "json",
pagination: {
transform: (response) => response.body.items,
paginate: ({ response }) => {
const next = response.body.nextCursor;
if (!next) return false;
return { searchParams: { cursor: next } };
}
}
})) {
allItems.push(page);
}
console.log(`Fetched ${allItems.length} items across all pages`);
Streams
import { createWriteStream } from "node:fs";
import { pipeline } from "node:stream/promises";
// Download a file with progress
await pipeline(
got.stream("https://example.com/large-file.zip"),
createWriteStream("output.zip")
);
Hooks
const api = got.extend({
hooks: {
beforeRequest: [
(options) => { console.log(`Requesting: ${options.url}`); }
],
afterResponse: [
(response) => { console.log(`Status: ${response.statusCode}`); return response; }
]
}
});
Why got?
- Built-in pagination — auto-fetch all pages
- Stream support — download large files efficiently
- HTTP/2 support — out of the box
- Caching — RFC-compliant with cacheable-request
- Retry with backoff — configurable per status code
Need to extract or automate web content at scale? Check out my web scraping tools on Apify — no coding required. Or email me at spinov001@gmail.com for custom solutions.
Top comments (0)