SuperAgent
Small progressive HTTP client for Node.js with a chainable API, plugin-style request methods, and multipart uploads.
Official Documentation ↗v9. Check the official docs for the latest API surface.Installation
npm install superagentpnpm add superagentMethods
No methods match this filter.
Small progressive HTTP client for Node.js with a chainable API, plugin-style request methods, and multipart uploads.
Overview
SuperAgent is a chainable HTTP client that has aged well: request verbs produce a Request, middleware-looking methods set headers, query, body, and behavior, and the same object resolves as a promise or through a callback.
It includes practical defaults teams want: automatic JSON serialization and parsing, redirects with trust decisions, retries, timeouts, and multipart uploads built from attach and field. The plugin mechanism, .use(fn), lets projects share auth or tracing additions without forking the client.
Core Methods
The most frequently used entry points, with signatures and runnable examples:
superagent.get— Starts a GET request; awaiting the Request resolves with a Response holding body, status, and headers.superagent.post— Starts a POST; send serializes the payload as JSON and set adds headers, both chainable.query— Appends query string parameters to the URL, serializing objects and arrays for GET requests.attach / field— Builds a multipart/form-data request: attach streams a file part and field adds a plain form value.retry / timeout— Adds automatic retries for transient failures and bounds wait time per attempt and per whole request.
Usage Notes
- Reuse request plumbing through .use(fn) plugins so every call site inherits auth and logging consistently.
- Prefer .ok(res => res.status < 500) when 404 is a legitimate answer rather than an exception.
- Keep the deadline timeout larger than response plus expected transfer time, or large downloads abort mid-body.
Common Pitfalls
- Calling .end() with a callback and also awaiting the same request double-consumes the resolution.
- Default redirects follow cross-host POSTs; review .redirects behavior when calling untrusted URLs.
- res.body is parsed only when the response Content-Type is JSON; HTML endpoints leave body as a string.
Errors
The following points are quoted verbatim from the official documentation (https://github.com/ladjs/superagent). Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.
- Our breaking changes are mostly in rarely used functionality and from stricter error handling. (https://github.com/ladjs/superagent)
- Invalid calls to .send() will now throw instead of sending garbage. (https://github.com/ladjs/superagent)
FAQ
- How do callbacks and promises mix in SuperAgent?
- Every Request is thenable: await it or call .then/.catch for promises, or pass a callback to .end((err, res) => {}) for the callback style. Do not mix both on one call; a callback consumes the resolution.
- How do I stream a download to a file?
- Call .pipe(writeStream) on the Request. The response body flows through without buffering, and pipe returns the destination stream so you can await its finish event.
- Why did my 4xx response throw?
- Promise-mode SuperAgent rejects on non-2xx statuses with an error that carries status, response, and the parsed body. Inspect err.status in catch blocks to distinguish client errors from network failures.
Report a problem with this API docOpens a pre-filled issue on GitHub.