GitHub Copilot API + CLI
Project description
GitHub Copilot API
An unofficial Python wrapper for interacting with the GitHub Copilot Chat API.
Disclaimer
This is an unofficial API wrapper and is not endorsed by or affiliated with GitHub or Microsoft. By using this package, you agree to comply with GitHub's terms of service and Copilot's usage policies. The maintainers of this package are not responsible for any misuse or violations. GitHub may change their API at any time which could break functionality.
Use at your own risk and responsibility.
Installation
You can install the package from the local directory:
pip install .
Or using the PyPi build:
pip install copilotapi
Requirements
- Python 3.6 or higher
- A valid GitHub account with Copilot access
- Chrome browser (for authentication)
- Required dependencies:
requests,selenium,rich,inquirer
Features
- ✅ Authentication with GitHub Copilot
- ✅ Token management (saving/loading)
- ✅ Chat thread creation and management
- ✅ Streaming message responses
- ✅ Model selection
- ✅ Rich interactive CLI interface
Basic Usage
from copilotapi.api import GithubCopilotClient
# Initialize the client
client = GithubCopilotClient()
# Authenticate (opens browser for login)
if not client.load_token_from_file():
cookies = client.get_cookies()
client.authenticate(cookies)
client.save_token_to_file()
# Get available models
models = client.get_models()
model_id = models[0]["id"] # Use first available model
# Create a thread
thread_id = client.create_new_thread()
# Send a message and get streaming response
for message in client.send_message("Write a Python function to calculate Fibonacci numbers", model_id, thread_id):
if message["type"] == "content":
print(message["text"], end="")
CLI Usage
The package includes a command-line interface for interactive chat:
python -m copilotapi.cli
Or after installation:
copilot
CLI Commands
exit- Quit the applicationnew thread- Start a fresh conversationclear- Clear the screenhelp- Show available commands
API Reference
GithubCopilotClient
Main client for interacting with the GitHub Copilot API.
Methods
| Method | Description |
|---|---|
authenticate(cookies) |
Authenticate with GitHub Copilot |
get_cookies(headless=False, timeout=300) |
Get GitHub cookies via automated browser login |
load_token_from_file(filepath="copilot_token.txt", timeout_period=30) |
Load auth token from file |
save_token_to_file(filepath="copilot_token.txt") |
Save auth token to file |
get_models() |
Get available Copilot models |
create_new_thread() |
Create a new chat thread |
get_latest_thread() |
Get ID of the latest thread |
delete_thread(thread_id) |
Delete a chat thread |
send_message(message, model_id, thread_id=None, streaming=True) |
Send a message and stream the response |
get_inline_completion(prompt, language='python', max_tokens=1000, temperature=0) |
Get inline code completion |
Exceptions
CopilotAuthError- Raised for authentication errorsCopilotAPIError- Raised for API errors
Example: Simple Chat Application
from copilotapi.api import GithubCopilotClient, CopilotAuthError, CopilotAPIError
client = GithubCopilotClient()
try:
# Authentication
if not client.load_token_from_file():
print("No token found. Authenticating...")
cookies = client.get_cookies()
client.authenticate(cookies)
client.save_token_to_file()
print("Authentication successful!")
# Get models and create thread
models = client.get_models()
print(f"Available models: {[m['name'] for m in models]}")
model_id = models[0]["id"]
thread_id = client.create_new_thread()
# Chat loop
while True:
prompt = input("\nYou: ")
if prompt.lower() == "exit":
break
print("\nCopilot:", end=" ")
for message in client.send_message(prompt, model_id, thread_id):
if message["type"] == "content":
print(message["text"], end="")
print("\n" + "-" * 50)
except (CopilotAuthError, CopilotAPIError) as e:
print(f"Error: {e}")
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ghcopilot-0.1.0.tar.gz.
File metadata
- Download URL: ghcopilot-0.1.0.tar.gz
- Upload date:
- Size: 15.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8939f304a182916c4855781ffb281dc605cd396328be4bbb8d9e3c19b2b35354
|
|
| MD5 |
f572e62685bd439cef65e8f802462fcc
|
|
| BLAKE2b-256 |
e18d828f42b20c177352debaafe75a9de7b2bb555eac7ed11f36bb1b8a7e783f
|
File details
Details for the file ghcopilot-0.1.0-py3-none-any.whl.
File metadata
- Download URL: ghcopilot-0.1.0-py3-none-any.whl
- Upload date:
- Size: 13.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ae63d353deb990acc04c6e9eecd195ee447aab74785b482bc6f61bdf0bdd5acd
|
|
| MD5 |
b5cb0cf36dd901dcad576e6b2c3854a8
|
|
| BLAKE2b-256 |
1b3a15b1f9a01985fcb1ad528bcae3002c7af004a1b36028f32f82d4e7b1180c
|