The CodeGuide class is the main entry point for interacting with the CodeGuide API. It provides access to all available services and handles authentication automatically.
The CodeGuide class initializes and manages all service instances, providing a unified interface to interact with the CodeGuide API.
npm install @codeguide/coreimport { CodeGuide } from '@codeguide/core'
const codeguide = new CodeGuide({
baseUrl: 'https://api.codeguide.ai',
databaseApiKey: 'sk_your_database_api_key',
})constructor(config: APIServiceConfig, options?: CodeGuideOptions)The API service configuration object:
interface APIServiceConfig {
baseUrl: string // Required: API base URL
databaseApiKey?: string // Recommended: Database API key (format: sk_...)
apiKey?: string // Legacy: Legacy API key
userId?: string // Legacy: User ID (required with apiKey)
jwtToken?: string // Alternative: Clerk JWT token
timeout?: number // Optional: Request timeout in milliseconds (default: 3600000)
}Additional options for the CodeGuide client:
interface CodeGuideOptions {
language?: string // Default language for requests
context?: string // Default context for requests
verbose?: boolean // Enable verbose logging
}const codeguide = new CodeGuide(
{
baseUrl: 'https://api.codeguide.ai',
databaseApiKey: 'sk_your_database_api_key',
timeout: 30000,
},
{
language: 'typescript',
verbose: true,
}
)The CodeGuide client provides access to the following services:
| Service | Property | Description |
|---|---|---|
| Projects | codeguide.projects |
Project management and repository connections |
| Codespace | codeguide.codespace |
AI-powered coding tasks and workflows |
| Security Keys | codeguide.securityKeys |
Provider API keys and GitHub token management |
| Usage | codeguide.usage |
Usage tracking and authorization |
| Generation | codeguide.generation |
AI-powered code and document generation |
| Repository Analysis | codeguide.repositoryAnalysis |
Repository analysis and insights |
| Tasks | codeguide.tasks |
Task group and project task management |
| API Key Enhanced | codeguide.apiKeyEnhanced |
Enhanced API key management |
| Subscription | codeguide.subscription |
Subscription management |
| Cancellation Funnel | codeguide.cancellationFunnel |
Subscription cancellation process |
| External Tokens | codeguide.externalTokens |
External token management (GitHub, GitLab, etc.) |
| Users | codeguide.users |
User management |
| Starter Kits | codeguide.starterKits |
Retrieve starter kits for bootstrapping projects |
Convenience method for backward compatibility. Generates guidance from a prompt.
async getGuidance(prompt: string): Promise<any>prompt(string, required): The user prompt
{
id: string
response: string
timestamp: string
language?: string
}const guidance = await codeguide.getGuidance(
'How do I implement user authentication?'
)
console.log(guidance.response)
console.log(guidance.timestamp)Check if the API service is healthy and accessible.
async isHealthy(): Promise<boolean>boolean:trueif the service is healthy,falseotherwise
const healthy = await codeguide.isHealthy()
if (healthy) {
console.log('API is healthy')
} else {
console.log('API is not responding')
}Helper method to create a task group with codespace task integration.
async createTaskGroupWithCodespace(request: {
name: string
description?: string
project_id: string
include_codespace_task?: boolean
project_description?: string
}): Promise<any>request.name(string, required): Task group namerequest.description(string, optional): Task group descriptionrequest.project_id(string, required): Project IDrequest.include_codespace_task(boolean, optional): Include codespace taskrequest.project_description(string, optional): Project description
const taskGroup = await codeguide.createTaskGroupWithCodespace({
name: 'Authentication Implementation',
description: 'Implement user authentication system',
project_id: 'proj_123456',
include_codespace_task: true
})Update the CodeGuide client options.
setOptions(options: Partial<CodeGuideOptions>): voidoptions(Partial): Options to update
codeguide.setOptions({
language: 'python',
verbose: false,
})The baseUrl should point to your CodeGuide API endpoint:
const codeguide = new CodeGuide({
baseUrl: 'https://api.codeguide.ai', // Production
// baseUrl: 'https://api.codeguide.dev', // Development
databaseApiKey: 'sk_your_key',
})Configure the request timeout (in milliseconds). The default is 1 hour (3600000ms).
const codeguide = new CodeGuide({
baseUrl: 'https://api.codeguide.ai',
databaseApiKey: 'sk_your_key',
timeout: 1800000, // 30 minutes
})Enable verbose logging to see detailed request and response information:
const codeguide = new CodeGuide(
{
baseUrl: 'https://api.codeguide.ai',
databaseApiKey: 'sk_your_key',
},
{
verbose: true, // Enable detailed logging
}
)import { CodeGuide } from '@codeguide/core'
// Initialize the client
const codeguide = new CodeGuide({
baseUrl: 'https://api.codeguide.ai',
databaseApiKey: process.env.CODEGUIDE_API_KEY!,
timeout: 30000,
}, {
language: 'typescript',
verbose: process.env.NODE_ENV === 'development',
})
// Check health
const isHealthy = await codeguide.isHealthy()
if (!isHealthy) {
throw new Error('API is not healthy')
}
// Get all projects
const projects = await codeguide.projects.getAllProjects()
console.log(`Found ${projects.length} projects`)
// Create a new project
const newProject = await codeguide.projects.createProject({
title: 'My New Project',
description: 'Project description',
})
// Create a codespace task
const task = await codeguide.codespace.createCodespaceTaskV2({
project_id: newProject.id,
task_description: 'Implement user authentication',
execution_mode: 'implementation',
})
console.log(`Created task: ${task.task_id}`)All methods throw errors for various failure conditions. It's recommended to wrap API calls in try-catch blocks:
try {
const projects = await codeguide.projects.getAllProjects()
} catch (error) {
if (error.message.includes('401')) {
console.error('Authentication failed:', error.message)
} else if (error.message.includes('403')) {
console.error('Permission denied:', error.message)
} else if (error.message.includes('429')) {
console.error('Rate limited:', error.message)
} else {
console.error('API error:', error.message)
}
}The package exports all necessary types:
import type {
CodeGuide,
APIServiceConfig,
CodeGuideOptions,
} from '@codeguide/core'- Use Environment Variables: Store API keys in environment variables
- Handle Errors: Always wrap API calls in try-catch blocks
- Check Health: Use
isHealthy()before making critical requests - Set Appropriate Timeouts: Adjust timeout based on expected operation duration
- Enable Verbose Logging: Use verbose mode during development for debugging
- Authentication - Authentication methods and configuration
- Projects Service - Project management
- Codespace Service - Codespace tasks
- Security Keys Service - Security key management