DEV Community

Alex Spinov
Alex Spinov

Posted on

SuperAgent Has a Free API — Here's How to Use This Flexible HTTP Client

SuperAgent is a progressive HTTP client for Node.js and browsers. Its chainable API makes building complex requests intuitive and readable.

Installation

npm install superagent
Enter fullscreen mode Exit fullscreen mode

Basic Requests

import request from "superagent";

// GET with query parameters
const { body } = await request
  .get("https://api.github.com/search/repositories")
  .query({ q: "web scraping", sort: "stars", per_page: 5 })
  .set("User-Agent", "superagent-example");

body.items.forEach(repo => console.log(`${repo.full_name}: ${repo.stargazers_count} stars`));
Enter fullscreen mode Exit fullscreen mode

POST and PUT

// POST JSON
const response = await request
  .post("https://httpbin.org/post")
  .send({ name: "SuperAgent", type: "http-client" })
  .set("Content-Type", "application/json");

console.log(response.body.json);

// PUT with auth
await request
  .put("https://api.example.com/users/1")
  .auth("username", "password")
  .send({ name: "Updated Name" });
Enter fullscreen mode Exit fullscreen mode

File Upload

const response = await request
  .post("https://httpbin.org/post")
  .attach("file", "path/to/document.pdf")
  .field("description", "My document");

console.log(response.body.files);
Enter fullscreen mode Exit fullscreen mode

Retry Logic

const { body } = await request
  .get("https://api.example.com/data")
  .retry(3) // Retry up to 3 times on network errors
  .timeout({ response: 5000, deadline: 30000 });
Enter fullscreen mode Exit fullscreen mode

Plugins

// Create a reusable plugin
function authPlugin(req) {
  req.set("Authorization", `Bearer ${getToken()}`);
  req.set("X-Request-ID", crypto.randomUUID());
  return req;
}

const data = await request
  .get("https://api.example.com/me")
  .use(authPlugin)
  .then(res => res.body);
Enter fullscreen mode Exit fullscreen mode

Chainable Response Handling

request
  .get("https://api.example.com/stream")
  .on("progress", (event) => {
    console.log(`${event.percent}% complete`);
  })
  .pipe(createWriteStream("output.dat"));
Enter fullscreen mode Exit fullscreen mode

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)