API Res: Node.js HTTP(S) Request Library

1.0.3 · maintenance · verified Sun Apr 19

API Res is a lightweight Node.js HTTP(S) request library specifically designed for interacting with Nodal API services. Currently at version 1.0.3, it provides a simple interface for making RESTful API calls within a Node.js environment. While the exact release cadence is not specified, its minimalistic design suggests a focus on stability rather than frequent feature additions. Key differentiators include its explicit integration with Nodal APIs and a straightforward request syntax, aiming for ease of use in projects that leverage the Nodal framework. It serves as a foundational utility for consuming APIs, particularly those built with Nodal, by abstracting standard HTTP request complexities.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates basic GET and POST requests, including sending JSON payloads, custom headers, and robust error handling for API responses.

const apiRes = require('api-res');

async function fetchData() {
  try {
    // Example GET request
    const getResponse = await apiRes.get('http://localhost:3000/api/users');
    console.log('GET Response Body:', getResponse.body);
    console.log('GET Status Code:', getResponse.statusCode);

    // Example POST request with data and headers
    const payload = { name: 'Alice', email: 'alice@example.com' };
    const postOptions = {
      headers: {
        'Content-Type': 'application/json',
        'Authorization': `Bearer ${process.env.API_KEY ?? ''}` // Use env var for sensitive data
      },
      json: true // Instructs library to send and expect JSON
    };
    const postResponse = await apiRes.post('http://localhost:3000/api/users', payload, postOptions);
    console.log('POST Response Body:', postResponse.body);
    console.log('POST Status Code:', postResponse.statusCode);

  } catch (error) {
    console.error('An error occurred during API request:', error.message);
    if (error.response) {
      console.error('Error Response Body:', error.response.body);
      console.error('Error Status Code:', error.response.statusCode);
    }
  }
}

fetchData();

view raw JSON →