Build a Custom GitHub Copilot Extension
In this article
Building a custom GitHub Copilot extension allows developers to extend the AI-powered coding assistant with domain-specific logic, custom commands, or integration into internal tools. These extensions run within VS Code and interact with Copilot’s suggestion engine or user interface to enhance productivity. This guide walks through creating a functional extension using official APIs and tools.
What You'll Need
- Visual Studio Code (latest stable version)
- Node.js (version 16 or higher)
- npm (Node Package Manager)
- GitHub Copilot enabled on your account
- Basic knowledge of TypeScript or JavaScript
- Access to the GitHub Copilot API (via preview program or partner access)
- VS Code Extension Development extension (recommended)
Step-by-Step
- Set up your development environment: Install VS Code and Node.js. Verify installation by running
node --versionandnpm --versionin your terminal. Ensure both return valid versions. - Create a new extension project: Open VS Code and run the command palette (Ctrl+Shift+P). Search for "Yeoman: Generate Code" and select "Yo Code". Choose "New Extension (TypeScript)" when prompted. Follow the prompts to name your extension, author, and description.
- Install required dependencies: Navigate to your project folder in the terminal and run
npm install @github-copilot/common @github-copilot/sdk. These packages provide access to Copilot’s APIs for sending requests and handling responses. - Configure package.json: In your extension’s
package.json, add"extensionDependencies": ["GitHub.copilot"]under the main extension object. This ensures your extension only loads when Copilot is active. - Register a command in package.json: Under the
contributessection, add a command like:
This adds a menu item users can click to invoke your extension."commands": [ { "command": "myextension.triggerCopilot", "title": "Ask Custom Copilot" } ] - Implement the command in your main extension file: Open
src/extension.ts. Import the Copilot SDK:
Then, inside theimport * as copilot from '@github-copilot/sdk';activatefunction, register your command:
This captures selected text, sends it to Copilot, and displays the top suggestion.context.subscriptions.push( vscode.commands.registerCommand('myextension.triggerCopilot', async () => { const editor = vscode.window.activeTextEditor; if (!editor) return; const prompt = editor.document.getText(editor.selection); const response = await copilot.getSuggestions({ prompt, language: editor.document.languageId }); vscode.window.showInformationMessage(`Copilot says: ${response[0]?.text}`); }) ); - Test the extension locally: Press F5 in VS Code to launch a new VS Code instance with your extension loaded. Open a code file, select a snippet, and run your command from the Command Palette. You should see a Copilot-generated suggestion.
- Enhance with context or custom logic: Modify the prompt or add pre-processing. For example, prepend domain-specific instructions:
const enhancedPrompt = `// As a senior React developer, optimize this: \n${prompt}`;. This steers Copilot’s output. - Handle errors and timeouts: Wrap API calls in try/catch blocks and set timeouts. Copilot may not respond instantly. Show user feedback during processing.
- Package and publish (optional): Run
vsce packageto create a.vsixfile. You can install it manually or publish to the VS Code Marketplace if you meet GitHub’s publishing guidelines.
Common Mistakes
- Forgetting to declare
extensionDependencies– your extension may load before Copilot is ready. - Not handling empty selections or null responses – can cause runtime errors.
- Using synchronous calls instead of
async/await– blocks the UI. - Hardcoding prompts without user customization – reduces flexibility.
- Ignoring rate limits – excessive API calls may trigger throttling.
Tips
- Use TypeScript interfaces from the SDK to ensure correct API usage.
- Log responses during development to inspect Copilot’s output format.
- Add configuration options in
settings.jsonto let users customize behavior. - Use
vscode.window.withProgressto show a progress bar during long requests. - Test across multiple languages (e.g., Python, JavaScript) to ensure broad compatibility.
Troubleshooting
If the extension fails to activate, check the developer console (Help > Toggle Developer Tools). Common issues include missing dependencies or syntax errors in extension.ts. If Copilot returns no suggestions, verify your account has Copilot access and the API is enabled. Network issues or firewall settings may block requests. If the command does not appear, ensure it’s correctly listed in package.json under contributes.commands. For authentication errors, sign in to GitHub in VS Code and restart.