This guide explains how to use GitHub Copilot through an API compatible with OpenAI's format. You can either use direct curl
commands or the provided automation scripts.
- GitHub account with active Copilot subscription
- Terminal/shell access
The client_id
used in this guide (Iv1.b507a08c87ecfe98
) is GitHub Copilot's public application identifier. This is the same for all users - you don't need to obtain your own. It's hardcoded in official Copilot extensions and is safe to use.
Run these commands in sequence:
curl -X POST 'https://github.com/login/device/code' \
-H 'accept: application/json' \
-H 'editor-version: Neovim/0.6.1' \
-H 'editor-plugin-version: copilot.vim/1.16.0' \
-H 'content-type: application/json' \
-H 'user-agent: GithubCopilot/1.155.0' \
--compressed \
-d '{"client_id":"Iv1.b507a08c87ecfe98","scope":"read:user"}'
Response explanation:
device_code
: Unique identifier for this authentication sessionuser_code
: 8-character code (XXXX-XXXX format) to enter on GitHubverification_uri
: URL where you'll authorize the deviceexpires_in
: Time in seconds before the codes expire (usually 15 minutes)interval
: Minimum seconds to wait between polling attempts
- Visit the
verification_uri
from the response - Log in with your GitHub account if prompted
- Enter the
user_code
(format: XXXX-XXXX) - Confirm authorization for the device
You should see a success message indicating the device is authorized
Replace YOUR_DEVICE_CODE
with the device_code
from past step response. This will generate the access token needed for the next step.
curl -X POST 'https://github.com/login/oauth/access_token' \
-H 'accept: application/json' \
-H 'content-type: application/json' \
--compressed \
-d '{"client_id":"Iv1.b507a08c87ecfe98","device_code":"YOUR_DEVICE_CODE","grant_type":"urn:ietf:params:oauth:grant-type:device_code"}'
Replace YOUR_ACCESS_TOKEN
with the token
from past step response:
curl -X GET 'https://api.github.com/copilot_internal/v2/token' \
-H 'authorization: token YOUR_ACCESS_TOKEN' \
--compressed
The response includes additional information, but we only need the token
field.
For all Copilot API requests, you need these two essential headers:
authorization: Bearer YOUR_COPILOT_TOKEN
- Authentication with the token from step 4Copilot-Integration-Id: vscode-chat
- Identifies the integration type
curl -X POST 'https://api.githubcopilot.com/chat/completions' \
-H 'authorization: Bearer YOUR_COPILOT_TOKEN' \
-H 'Copilot-Integration-Id: vscode-chat' \
-H 'content-type: application/json' \
-d '{
"messages": [
{
"role": "user",
"content": "Write a Python function to calculate the factorial of a number"
}
],
"max_tokens": 1000,
"temperature": 0.3,
"stream": false
}'
Streaming Support: Add "stream": true
to the request body if you want real-time response streaming.
Get available Copilot models:
curl -X GET 'https://api.githubcopilot.com/models' \
-H 'authorization: Bearer YOUR_COPILOT_TOKEN' \
-H 'Copilot-Integration-Id: vscode-chat'
Example response from completions endpoint:
{
"choices": [
{
"message": {
"role": "assistant",
"content": "def factorial(n):\n if n == 0 or n == 1:\n return 1\n else:\n return n * factorial(n - 1)"
},
"index": 0,
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 15,
"completion_tokens": 45,
"total_tokens": 60
}
}
Example response from models endpoint:
{
"data": [
{
"id": "gpt-4o",
"object": "model",
"created": 1234567890,
"owned_by": "github-copilot"
}
]
}
β Token Management:
- Tokens expire after 25 minutes
- Regenerate tokens as needed
β Rate Limits:
- GitHub may throttle excessive requests
- Suggested: 1 request every 2 seconds
β Legal Considerations:
- Uses unofficial GitHub endpoints
- May violate Copilot Terms of Service
- Not recommended for production use
π§ Common Issues:
401 Unauthorized
: Token expired - regenerate403 Forbidden
: Check token permissions429 Too Many Requests
: Reduce call frequency
This repository provides web servers that expose GitHub Copilot through OpenAI-compatible endpoints. You can use them with any OpenAI client library!
- Get your access token following Step 1 below (steps 1-3)
- Configure: Edit
config.json
and replaceYOUR_ACCESS_TOKEN_HERE
with your actual token - Start a server: Choose Node.js or Python - both provide the same OpenAI-compatible API!
npm install
npm start
pip install -r requirements.txt
python server.py
# Get available models
curl http://localhost:3000/v1/models
# Chat completions
curl -X POST http://localhost:3000/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"messages": [{"role": "user", "content": "Hello!"}],
"max_tokens": 1000
}'
// JavaScript
import OpenAI from 'openai';
const openai = new OpenAI({
baseURL: 'http://localhost:3000/v1',
apiKey: 'dummy-key'
});
# Python
import openai
openai.api_base = "http://localhost:5000/v1"
openai.api_key = "dummy-key"
See examples/web-api-examples.md
for detailed examples and client usage.
This repository provides multiple ways to interact with GitHub Copilot:
server.js
- Node.js/Express server with OpenAI-compatible endpointsserver.py
- Python/Flask server with OpenAI-compatible endpoints- Best for: Integration with existing applications, web services, OpenAI client libraries
- Documentation: examples/web-api-examples.md
copilot-client.js
- Node.js command-line clientcopilot_client.py
- Python command-line client- Best for: Quick queries, automation scripts, command-line workflows
- Documentation: examples/cli-usage-examples.md
config.json
- Shared configuration file for access tokenstest-servers.sh
- Testing script for web servers
File | Description |
---|---|
examples/web-api-examples.md | Complete web API usage examples with curl, OpenAI clients, and integration patterns |
examples/cli-usage-examples.md | CLI scripts examples with automation and scripting use cases |
Option 1: Web API Server
npm start # β http://localhost:3000
# or
python server.py # β http://localhost:5000
Option 2: CLI Scripts
node copilot-client.js chat "Hello world"
# or
python copilot_client.py chat "Hello world"
Testing & Health Check
./test-servers.sh # Test web servers
curl http://localhost:3000/health # Health check
Use Case | Web API Server | CLI Scripts |
---|---|---|
Web applications | β Perfect | β Not suitable |
OpenAI client libraries | β Drop-in replacement | β Not compatible |
Quick one-time queries | β Perfect | |
Automation scripts | β Simple & direct | |
Production services | β Scalable & robust | β Not recommended |
Learning & experimenting | β Quick start | |
Integration with existing tools | β Standard HTTP API |