API Reference
Welcome to the Trae IDE API Reference. This comprehensive guide covers all available APIs, SDKs, and integration methods for extending and customizing Trae IDE.
Overview
Trae IDE provides multiple APIs and extension points to help you:
- Create custom extensions and plugins
- Integrate with external tools and services
- Automate development workflows
- Customize the IDE behavior
- Build AI-powered development tools
API Categories
Core APIs
- Editor API - Control the code editor, manage documents, and handle text operations
- Workspace API - Manage projects, files, and workspace settings
- UI API - Create custom panels, dialogs, and interface elements
- Commands API - Register custom commands and keyboard shortcuts
AI & Intelligence
- AI Chat API - Integrate with Trae's AI chat system
- Code Generation API - Leverage AI code generation capabilities
- Language Services API - Extend language support and IntelliSense
- Completion API - Customize code completion and suggestions
Development Tools
- Terminal API - Control integrated terminals and execute commands
- Debugger API - Integrate debugging capabilities
- Git API - Access version control functionality
- Build API - Manage build processes and tasks
Extensions & Plugins
- Extension API - Create and manage extensions
- Plugin System - Build plugins for specific frameworks
- Themes API - Create custom themes and color schemes
- Settings API - Manage configuration and preferences
Quick Start
Creating Your First Extension
import { TraeAPI } from '@trae/api';
export function activate(context: TraeAPI.ExtensionContext) {
// Register a command
const disposable = TraeAPI.commands.registerCommand('myExtension.helloWorld', () => {
TraeAPI.window.showInformationMessage('Hello from Trae!');
});
context.subscriptions.push(disposable);
}
export function deactivate() {
// Cleanup when extension is deactivated
}Basic API Usage
// Get the active editor
const editor = TraeAPI.window.activeTextEditor;
if (editor) {
const document = editor.document;
const selection = editor.selection;
// Get selected text
const selectedText = document.getText(selection);
console.log('Selected text:', selectedText);
}
// Show a notification
TraeAPI.window.showInformationMessage('Operation completed!');
// Execute a command
TraeAPI.commands.executeCommand('workbench.action.files.save');Authentication
Some APIs require authentication for external services:
// AI API authentication
const aiClient = new TraeAPI.AI.Client({
apiKey: process.env.TRAE_AI_API_KEY,
endpoint: 'https://api.trae.ai'
});
// GitHub integration
const githubAuth = await TraeAPI.auth.getSession('github');
if (githubAuth) {
const octokit = new Octokit({
auth: githubAuth.accessToken
});
}Error Handling
All APIs use consistent error handling patterns:
try {
const result = await TraeAPI.workspace.openFolder('/path/to/project');
console.log('Folder opened:', result);
} catch (error) {
if (error instanceof TraeAPI.FileSystemError) {
TraeAPI.window.showErrorMessage(`Failed to open folder: ${error.message}`);
} else {
console.error('Unexpected error:', error);
}
}Event System
Trae IDE uses an event-driven architecture:
// Listen to file changes
TraeAPI.workspace.onDidChangeTextDocument((event) => {
console.log('Document changed:', event.document.fileName);
});
// Listen to editor selection changes
TraeAPI.window.onDidChangeTextEditorSelection((event) => {
console.log('Selection changed in:', event.textEditor.document.fileName);
});
// Listen to AI chat events
TraeAPI.ai.onDidReceiveMessage((message) => {
console.log('AI message:', message.content);
});Configuration
Extension Manifest
Every extension needs a package.json manifest:
{
"name": "my-trae-extension",
"displayName": "My Trae Extension",
"description": "A sample extension for Trae IDE",
"version": "1.0.0",
"engines": {
"trae": "^1.0.0"
},
"categories": [
"Other"
],
"activationEvents": [
"onCommand:myExtension.helloWorld"
],
"main": "./out/extension.js",
"contributes": {
"commands": [
{
"command": "myExtension.helloWorld",
"title": "Hello World"
}
]
},
"scripts": {
"compile": "tsc -p ./",
"watch": "tsc -watch -p ./"
},
"devDependencies": {
"@trae/api": "^1.0.0",
"typescript": "^4.9.0"
}
}TypeScript Configuration
{
"compilerOptions": {
"module": "commonjs",
"target": "ES2020",
"outDir": "out",
"lib": ["ES2020"],
"sourceMap": true,
"rootDir": "src",
"strict": true
},
"exclude": ["node_modules", ".trae-test"]
}Testing
Trae provides testing utilities for extensions:
import * as assert from 'assert';
import { TraeAPI } from '@trae/api';
suite('Extension Test Suite', () => {
test('Sample test', async () => {
// Test your extension functionality
const result = await TraeAPI.commands.executeCommand('myExtension.helloWorld');
assert.strictEqual(result, true);
});
});Publishing
Publish your extension to the Trae Marketplace:
# Install the Trae CLI
npm install -g @trae/cli
# Package your extension
trae package
# Publish to marketplace
trae publishBest Practices
Performance
- Use lazy loading for heavy operations
- Dispose of resources properly
- Avoid blocking the main thread
- Cache expensive computations
User Experience
- Provide clear error messages
- Show progress for long operations
- Follow Trae's design guidelines
- Support keyboard shortcuts
Security
- Validate all user inputs
- Use secure communication protocols
- Don't store sensitive data in plain text
- Follow the principle of least privilege
Migration Guide
From VS Code Extensions
Trae IDE is designed to be compatible with many VS Code extensions:
// VS Code API
import * as vscode from 'vscode';
// Trae API (similar structure)
import { TraeAPI } from '@trae/api';
// Most VS Code patterns work directly
const editor = TraeAPI.window.activeTextEditor; // Same as vscode.window.activeTextEditorBreaking Changes
See the Migration Guide for detailed information about API changes and migration steps.
Support
- Documentation: docs.trae.ai
- Community: community.trae.ai
- GitHub: github.com/trae-ai/trae
- Discord: discord.gg/trae
Examples
Explore our collection of example extensions:
This documentation is for Trae IDE API version 1.0. For older versions, see the version archive.