User Agents

Web browsers and everyday web tech, explained

Build a Custom GitHub Copilot Extension

· 4 min read · Extensions

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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

  1. Set up your development environment: Install VS Code and Node.js. Verify installation by running node --version and npm --version in your terminal. Ensure both return valid versions.
  2. 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.
  3. 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.
  4. 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.
  5. Register a command in package.json: Under the contributes section, add a command like:
    "commands": [
      {
        "command": "myextension.triggerCopilot",
        "title": "Ask Custom Copilot"
      }
    ]
    This adds a menu item users can click to invoke your extension.
  6. Implement the command in your main extension file: Open src/extension.ts. Import the Copilot SDK:
    import * as copilot from '@github-copilot/sdk';
    Then, inside the activate function, register your command:
    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}`);
      })
    );
    This captures selected text, sends it to Copilot, and displays the top suggestion.
  7. 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.
  8. 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.
  9. Handle errors and timeouts: Wrap API calls in try/catch blocks and set timeouts. Copilot may not respond instantly. Show user feedback during processing.
  10. Package and publish (optional): Run vsce package to create a .vsix file. 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.json to let users customize behavior.
  • Use vscode.window.withProgress to 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.

Frequently Asked Questions

Can I build a Copilot extension without joining a preview program?

No. Access to the Copilot SDK requires enrollment in GitHub’s early access or partner program.

Do I need to pay to use the Copilot API?

Usage may be subject to rate limits or costs depending on your GitHub plan and API tier.

Can my extension modify Copilot’s inline suggestions?

Not directly. Extensions can request suggestions but cannot alter the default inline completion behavior.

Is TypeScript required?

No, but it’s strongly recommended for type safety and better integration with the SDK.

How do I debug my extension?

Use the F5 debug mode in VS Code and inspect the Debug Console for logs and errors.

Can I distribute my extension publicly?

Yes, if you comply with GitHub’s publishing policies and include proper attribution.