Skip to content

An OpenAI-compatible API server that wraps the GitHub Copilot AI SDK provider

Notifications You must be signed in to change notification settings

mhingston/github-copilot-openai-compatible-api

Repository files navigation

GitHub Copilot OpenAI-Compatible API

An OpenAI-compatible API server that wraps the GitHub Copilot AI SDK provider, allowing you to use GitHub Copilot models with any OpenAI-compatible client. Depending on what you're trying to do you may be better off just using the Copilot SDK.

✨ Interactive Swagger/OpenAPI documentation at /api-docs

API Documentation OpenAI Compatible GitHub Copilot

Features

  • ✅ Full OpenAI API compatibility
  • Interactive Swagger/OpenAPI documentation at /api-docs
  • ✅ Streaming and non-streaming responses
  • ✅ Chat completions (/v1/chat/completions)
  • ✅ Legacy completions (/v1/completions)
  • ✅ Model listing (/v1/models)
  • ✅ Optional Bearer token authentication
  • ✅ Auto-managed GitHub Copilot authentication
  • ✅ CORS enabled

Prerequisites

  • Node.js 18+
  • GitHub Copilot subscription
  • No manual authentication required! The server will:
    • Use existing GitHub Copilot CLI credentials if available
    • Automatically trigger device flow if no credentials found

Installation

npm install

Usage

First Time Setup

When you start the server for the first time, it will check for existing GitHub Copilot credentials:

If credentials exist (from GitHub Copilot CLI, VS Code, etc.):

npm run dev
# ✓ GitHub Copilot provider initialized with existing credentials
# 🚀 Server starts immediately

If no credentials found:

npm run dev
# ⚠ No existing GitHub Copilot credentials found
# 🔐 Starting device flow authentication...
# 
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
#   GitHub Copilot Authentication Required
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
# 
#   1. Visit: https://github.com/login/device
#   2. Enter code: XXXX-XXXX
# 
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
# ⏳ Waiting for authentication...

Just visit the URL, enter the code, and the server will automatically continue once authenticated!

Start the server (development)

npm run dev

Start the server (production)

npm run build
npm start

Environment Variables

  • PORT - Server port (default: 3000)
  • API_KEY - Optional API key for Bearer token authentication. If not set, authentication is disabled.

Example:

PORT=8080 API_KEY=your-secret-key npm run dev

API Documentation

Once the server is running, visit http://localhost:3000/api-docs for interactive Swagger/OpenAPI documentation.

The Swagger UI provides:

  • 📖 Complete API reference
  • 🧪 Interactive "Try it out" functionality
  • 📋 Request/response examples
  • 🔐 Authentication testing

API Endpoints

Health Check

GET /health

Or visit the interactive docs: http://localhost:3000/api-docs

List Models

GET /v1/models
Authorization: Bearer your-api-key  # Optional if API_KEY is set

Chat Completions

POST /v1/chat/completions
Authorization: Bearer your-api-key  # Optional if API_KEY is set
Content-Type: application/json

{
  "model": "gpt-4o",
  "messages": [
    { "role": "user", "content": "Hello!" }
  ],
  "stream": false,
  "temperature": 0.7,
  "max_tokens": 1000
}

Streaming Chat Completions

POST /v1/chat/completions
Authorization: Bearer your-api-key  # Optional if API_KEY is set
Content-Type: application/json

{
  "model": "gpt-4o",
  "messages": [
    { "role": "user", "content": "Write a long story" }
  ],
  "stream": true
}

Legacy Completions

POST /v1/completions
Authorization: Bearer your-api-key  # Optional if API_KEY is set
Content-Type: application/json

{
  "model": "gpt-4o",
  "prompt": "Once upon a time",
  "stream": false
}

Available Models

All models available through your GitHub Copilot subscription:

  • gpt-4o
  • gpt-4o-mini
  • gpt-4.1
  • gpt-5
  • gpt-5.1
  • gpt-5.2
  • claude-3.5-sonnet
  • claude-3.7-sonnet
  • claude-sonnet-4
  • claude-sonnet-4.5
  • gemini-2.0-flash-001
  • gemini-3-pro-preview
  • o1-preview
  • o1-mini
  • o3-mini

Model availability depends on your Copilot subscription tier.

Using with OpenAI SDKs

Python

from openai import OpenAI

client = OpenAI(
    base_url="http://localhost:3000/v1",
    api_key="your-api-key"  # or omit if no API_KEY set
)

response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Hello!"}]
)

print(response.choices[0].message.content)

Node.js

import OpenAI from 'openai';

const client = new OpenAI({
  baseURL: 'http://localhost:3000/v1',
  apiKey: 'your-api-key', // or omit if no API_KEY set
});

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

console.log(response.choices[0].message.content);

cURL

curl http://localhost:3000/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer your-api-key" \
  -d '{
    "model": "gpt-4o",
    "messages": [{"role": "user", "content": "Hello!"}]
  }'

Authentication

The server supports optional Bearer token authentication:

  • Development mode (no API_KEY): All requests allowed
  • Production mode (API_KEY set): Requires Authorization: Bearer <API_KEY> header

Set the API_KEY environment variable to enable authentication:

API_KEY=my-secret-key npm run dev

How It Works

  1. On startup, the server checks for GitHub Copilot credentials
  2. If found, uses existing credentials from ~/.config/github-copilot/apps.json
  3. If not found, automatically initiates device flow authentication
  4. Once authenticated, exchanges OAuth token for Copilot API token
  5. Auto-refreshes tokens before expiration (30 min lifetime)
  6. All requests are translated to OpenAI-compatible format

Limitations

  • Embeddings (/v1/embeddings) are not supported (GitHub Copilot doesn't provide embedding models)
  • Token usage statistics depend on what the GitHub Copilot API returns
  • Some OpenAI-specific parameters may not be supported by all models

License

MIT

About

An OpenAI-compatible API server that wraps the GitHub Copilot AI SDK provider

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •