npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@coopah/copilot-auth

v0.1.1

Published

GitHub Copilot OAuth device flow + OpenAI-compatible client with auto-refreshing tokens

Downloads

183

Readme

@coopah/copilot-auth

GitHub Copilot OAuth device flow + OpenAI-compatible client config with auto-refreshing tokens.

npm

Install

npm install @coopah/copilot-auth

Prerequisites

Quick Start

1. Implement token storage

Store the GitHub OAuth token however you like — database, file, env var, etc.

import type { TokenStorage } from '@coopah/copilot-auth';

const storage: TokenStorage = {
  async getToken() {
    return process.env.GITHUB_TOKEN ?? null;
  },
  async setToken(token) {
    process.env.GITHUB_TOKEN = token;
  },
};

2. Login and use

import { CopilotAuth } from '@coopah/copilot-auth';
import OpenAI from 'openai';

const auth = new CopilotAuth({ storage });

// One call — prints the code/URL to console, polls until authorized
await auth.login();

// Get an auto-refreshing OpenAI-compatible config
const config = await auth.getOpenAIClientConfig();
const openai = new OpenAI(config);

const completion = await openai.chat.completions.create({
  model: 'gpt-4o',
  messages: [{ role: 'user', content: 'Hello!' }],
});

login() skips the flow if a token already exists. getSessionCredentials() caches and auto-refreshes the Copilot session token.

You can also use startDeviceFlow() and pollDeviceFlow() directly if you need lower-level control.

Refreshing credentials

The session token expires periodically. Use getCredentialsIfChanged() to rebuild your client only when needed:

const newCreds = await auth.getCredentialsIfChanged();
if (newCreds) {
  openai = new OpenAI(await auth.getOpenAIClientConfig());
}

Express Integration

Mount two POST endpoints for the device flow on any Express router:

import express from 'express';
import { mountCopilotRoutes } from '@coopah/copilot-auth/express';

const app = express();
app.use(express.json());

const router = express.Router();
mountCopilotRoutes({ router, storage });
app.use('/api', router);

// Exposes:
//   POST /api/copilot/device-code  → starts the device flow
//   POST /api/copilot/device-poll  → polls for completion (body: { device_code })

app.listen(3000);

mountCopilotRoutes returns the CopilotAuth instance, so you can reuse it:

const auth = mountCopilotRoutes({ router, storage });
const config = await auth.getOpenAIClientConfig();

API Reference

@coopah/copilot-auth

| Export | Type | Description | |--------|------|-------------| | CopilotAuth | class | Core auth — device flow + session token management | | CopilotAuthOptions | type | Options: { storage, extraHeaders? } | | TokenStorage | type | Interface for pluggable token persistence | | DeviceFlowResponse | type | Response from startDeviceFlow() | | SessionCredentials | type | { apiKey, baseURL } for the Copilot API | | OpenAIClientConfig | type | { apiKey, baseURL, defaultHeaders } for new OpenAI(...) | | DevicePollResult | type | Result from pollDeviceFlow() |

@coopah/copilot-auth/express

| Export | Type | Description | |--------|------|-------------| | mountCopilotRoutes | function | Mounts device flow POST endpoints on a router | | CopilotRoutesOptions | type | Options: { router, storage, auth? } |

License

MIT