gaxios HTTP Client

7.1.4 · active · verified Wed Apr 22

gaxios is an HTTP client library meticulously designed for seamless integration with Google APIs and services, providing a familiar `axios`-like interface built on top of `node-fetch`. It facilitates robust HTTP request management in both Node.js and browser environments. The current stable version, 7.1.4, is actively maintained as part of the broader `google-cloud-node-core` monorepo. This integration means its release cadence is often synchronized with updates to other Google client libraries, leading to frequent releases driven by bug fixes, dependency updates, and feature enhancements within the Google ecosystem. Key differentiators include its explicit optimization for Google's API patterns, a developer-friendly `axios`-like API, and an alternative `fetch`-compatible API for wider browser and modern Node.js compatibility. The package ships with comprehensive TypeScript types, ensuring a strong development experience in TypeScript projects. It officially supports Node.js environments version 18 and above.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates basic GET and POST requests using the default `request` function and a custom `Gaxios` instance, including setting base URLs, headers, and handling timeouts and errors.

import { request, Gaxios } from 'gaxios';

async function makeGaxiosRequests() {
  try {
    // --- Example 1: Basic GET request using the default `request` function ---
    console.log('--- Initiating basic GET request to Google ---');
    const googleResponse = await request({
      url: 'https://www.google.com/search',
      params: { q: 'gaxios library' },
      timeout: 10000 // 10 seconds timeout
    });
    console.log(`Google Search Status: ${googleResponse.status}`);
    console.log(`Google Search Data (partial): ${googleResponse.data.substring(0, 100)}...`);

    // --- Example 2: Using a custom Gaxios instance with base URL and headers ---
    console.log('\n--- Initiating POST request to JSONPlaceholder API ---');
    const jsonPlaceholderClient = new Gaxios({
      baseURL: 'https://jsonplaceholder.typicode.com',
      headers: {
        'Content-Type': 'application/json',
        'User-Agent': 'MyGaxiosApp/1.0'
      },
      timeout: 5000
    });

    const newPostData = {
      title: 'gaxios usage guide',
      body: 'This is a sample post demonstrating gaxios capabilities.',
      userId: 1
    };
    const postResponse = await jsonPlaceholderClient.request({
      url: '/posts',
      method: 'POST',
      data: newPostData
    });
    console.log(`JSONPlaceholder POST Status: ${postResponse.status}`);
    console.log(`Created Post ID: ${postResponse.data.id}, Title: ${postResponse.data.title}`);

  } catch (error: any) {
    console.error('\nAn error occurred during gaxios request:');
    if (error.response) {
      console.error(`Status: ${error.response.status}, Data:`, error.response.data);
    } else if (error.code === 'ERR_SOCKET_TIMEOUT') {
      console.error('Request timed out.');
    } else {
      console.error(error.message);
    }
  }
}

makeGaxiosRequests();

view raw JSON →