got
Powerful HTTP request library for Node.js with retries, pagination, streams, hooks, and instances that bake in shared defaults.
Official Documentation ↗v14 (ESM only). Check the official docs for the latest API surface.Installation
npm install gotpnpm add gotMethods
No methods match this filter.
Powerful HTTP request library for Node.js with retries, pagination, streams, hooks, and instances that bake in shared defaults.
Overview
got is a batteries-included HTTP client for Node.js. Around a promise core it layers retries with backoff, cache hooks, decompression, streaming uploads and downloads, and a pagination iterator that walks cursor-style APIs.
got.extend lets a team bake the boring parts, base URLs, auth headers, timeouts, retry policy, into one instance so call sites stay small. Every request and response passes through beforeRequest and afterResponse hooks, which is where to inject logging, tracing, or token refresh.
Core Methods
The most frequently used entry points, with signatures and runnable examples:
got— Performs a request and resolves with a Response holding body, statusCode, headers, and the final redirect URL.got.extend— Freezes a set of defaults such as prefixUrl, headers, timeout, and retry into a reusable instance.got.post— Sends a request with a body; the json option serializes the payload and parses the response automatically.retry option— Configures automatic retries with exponential backoff for failed methods and retryable status codes, off by default.got.paginate— Walks paginated APIs with a transform and paginate function that derives the next URL from each response.
Usage Notes
- Build one extended instance per upstream API and reuse it; defaults only protect code that actually goes through them.
- Set timeout: { request } on every instance so stalled sockets fail fast instead of pinning the event loop.
- Treat retry as opt-in: only enable it for idempotent methods and add jitter through calculateDelay when many workers share one backend.
Common Pitfalls
- prefixUrl requires the url argument to be relative; passing an absolute URL throws, which surprises callers mixing both styles.
- By default got does not retry; assuming automatic retries exist is a common source of flaky-production confusion.
- responseType “json” trusts the payload shape; validate it with a schema library before it reaches business logic.
FAQ
- Why does require("got") fail on got v12 and later?
- From v12 onward got is a pure ESM package. Use import got from "got" in an .mjs file or a project with "type": "module". If you are stuck on CommonJS, pin got@11, which still receives limited maintenance.
- How do I cancel an in-flight request?
- Pass an AbortSignal through the signal option: create a controller, pass controller.signal, and call controller.abort(). The promise rejects with a timeout or cancelation error that you can classify and handle.
- How is the response body typed?
- body is a string unless responseType is set. responseType: "json" parses the body into an object, "buffer" returns a Buffer, and "text" forces a string. Combine parseJson with a custom reviver or use a typed wrapper for stricter types.
Report a problem with this API docOpens a pre-filled issue on GitHub.