http-call

5.3.0 · active · verified Tue Apr 21

http-call is a lightweight, promise-based HTTP client designed for making RESTful API requests. Maintained by Heroku, it provides a fluent interface for common HTTP methods (GET, POST, PUT, DELETE) and simplifies interaction with JSON APIs by automatically converting responses. The current stable version is 5.5.1, with a release cadence that includes regular bug fixes and minor feature enhancements. Key differentiators include built-in TypeScript support, automatic JSON parsing, and a focus on clean, asynchronous API consumption, making it suitable for modern Node.js applications that require robust HTTP communication without extensive configuration. It is widely used within the Heroku ecosystem for interacting with platform services.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to make a GET request to the GitHub API, handling authentication, automatic JSON parsing, and TypeScript type inference for the response body. It includes error handling and shows how to use environment variables for sensitive data.

import { HTTP } from 'http-call';

interface GitHubUser {
  login: string;
  id: number;
  node_id: string;
  avatar_url: string;
  name: string;
  email: string | null;
  bio: string | null;
}

async function getGitHubUser(username: string): Promise<GitHubUser> {
  try {
    const token = process.env.GITHUB_TOKEN ?? ''; // Use environment variable for auth
    if (!token) {
      console.warn('GITHUB_TOKEN environment variable is not set. Requests may be unauthenticated or rate-limited.');
    }

    const { body: user } = await HTTP.get<GitHubUser>(`https://api.github.com/users/${username}`, {
      headers: { authorization: token ? `token ${token}` : '' }
    });
    console.log(`Successfully fetched user ${user.name || user.login}. ID: ${user.id}, Bio: ${user.bio || 'N/A'}`);
    return user;
  } catch (error: any) {
    console.error(`Error fetching GitHub user ${username}:`, error.message);
    throw error;
  }
}

// Example usage:
(async () => {
  try {
    const myUser = await getGitHubUser('octocat');
    console.log('GitHub User Details:', myUser);
  } catch (e) {
    // Error handled in getGitHubUser
  }
  try {
    // Example with a non-existent user to demonstrate error handling
    await getGitHubUser('nonexistentuser12345');
  } catch (e) {
    // Expected error, already logged
  }
})();

view raw JSON →