Octokit Netrc Authentication Plugin

4.0.0 · active · verified Wed Apr 22

The `octokit-auth-netrc` package provides a plugin for Octokit that enables authentication using credentials stored in a user's `.netrc` file. This is particularly useful for command-line tools and scripts that need to interact with GitHub without hardcoding tokens or using environment variables directly. The current stable version is 4.0.0, which was released on January 30, 2026. This library's release cadence is generally tied to updates in the Octokit ecosystem and Node.js LTS version changes. Its key differentiator is simplifying authentication against GitHub (including GitHub Enterprise Server) by leveraging the standard `.netrc` file format, providing a robust and familiar mechanism for developers who already use this method for other tools like `curl`. It handles both `api.github.com` and custom GitHub Enterprise domains.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to authenticate with GitHub using `octokit-auth-netrc` and then make a simple API request to fetch the authenticated user's details, including handling for a missing .netrc entry.

import { Octokit } from '@octokit/core';
import { createNetrcAuth } from 'octokit-auth-netrc';

async function authenticateAndFetchUser(domain = 'api.github.com') {
  try {
    const auth = createNetrcAuth({ domain });
    const authResult = await auth();
    console.log(`Successfully authenticated to ${domain} using .netrc file.`);

    const octokit = new Octokit({
      authStrategy: createNetrcAuth,
      auth: { domain }
    });

    const { data: user } = await octokit.request('GET /user');
    console.log(`Authenticated as: ${user.login} (${user.id}) on ${domain}`);
  } catch (error) {
    if (error.code === 'ENONETRCTOKEN') {
      console.error(`Error: No token found in .netrc for ${domain}. Please add an entry like:\n\nmachine ${domain}\n  login <your_personal_access_token>\n`);
    } else {
      console.error(`Authentication failed for ${domain}:`, error.message);
    }
  }
}

(async () => {
  console.log('Attempting authentication for api.github.com...');
  await authenticateAndFetchUser('api.github.com');

  // Example for GitHub Enterprise Server (replace with your actual domain)
  // console.log('\nAttempting authentication for github.acme-inc.com...');
  // await authenticateAndFetchUser('github.acme-inc.com');
})();

view raw JSON →