SDK & Programmatic Access
Run Codebuff agents from your own code with @codebuff/sdk.
Installation
bash
npm install @codebuff/sdk
Prerequisites
API key: codebuff.com/api-keys
Quick Start
Use the base agent (or any agent from the Agent Store) to run tasks:
typescript
import { CodebuffClient } from '@codebuff/sdk'const client = new CodebuffClient({apiKey: process.env.CODEBUFF_API_KEY,cwd: process.cwd(),})const result = await client.run({agent: 'codebuff/base@0.0.16',prompt: 'Create a simple calculator class',handleEvent: (event) => {console.log('Event:', event.type)},})
Continuing Conversations
Pass the previous run state to keep context:
typescript
// First runconst run1 = await client.run({agent: 'codebuff/base@0.0.16',prompt: 'Create a calculator class',})// Continue the conversationconst run2 = await client.run({agent: 'codebuff/base@0.0.16',prompt: 'Add unit tests for the calculator',previousRun: run1, // Maintains conversation context})
Custom Agents
typescript
import type { AgentDefinition } from '@codebuff/sdk'const myAgent: AgentDefinition = {id: 'my-custom-agent',model: 'anthropic/claude-sonnet-4.5',displayName: 'My Custom Agent',toolNames: ['read_files', 'write_file'],instructionsPrompt: 'You are a helpful coding assistant.',}await client.run({agent: 'my-custom-agent',prompt: 'Help me refactor this code',agentDefinitions: [myAgent],})
Custom Tools
typescript
import { z } from 'zod/v4'import { getCustomToolDefinition } from '@codebuff/sdk'const fetchTool = getCustomToolDefinition({toolName: 'fetch_api_data',description: 'Fetch data from an API endpoint',inputSchema: z.object({url: z.url(),method: z.enum(['GET', 'POST']).default('GET'),}),execute: async ({ url, method }) => {const response = await fetch(url, { method })const data = await response.text()return [{ type: 'json', value: { data } }]},})await client.run({agent: 'my-custom-agent',prompt: 'Fetch the latest data',customToolDefinitions: [fetchTool],})
Loading Local Agents
typescript
import { loadLocalAgents, CodebuffClient } from '@codebuff/sdk'const agents = await loadLocalAgents({ verbose: true })const client = new CodebuffClient({ apiKey: process.env.CODEBUFF_API_KEY })await client.run({agent: 'my-local-agent',agentDefinitions: Object.values(agents),prompt: 'Hello',})
API Reference
CodebuffClient
typescript
const client = new CodebuffClient({apiKey: string, // Required: Your Codebuff API keycwd?: string, // Working directory for file operations})
client.run(options)
| Parameter | Type | Description |
|---|---|---|
| agent | string | Agent ID from the store or custom agent ID |
| prompt | string | The user prompt |
| params | object | Additional parameters for the agent |
| handleEvent | function | Callback for streaming events |
| previousRun | RunState | Previous run state to continue conversation |
| projectFiles | object | Project files as { path: content } |
| knowledgeFiles | object | Knowledge files to inject into context |
| agentDefinitions | array | Custom agent definitions |
| customToolDefinitions | array | Custom tool definitions |
| maxAgentSteps | number | Maximum steps before stopping (default: ~20) |
Event Types
agent_start/agent_finish- Agent lifecycletool_call/tool_result- Tool executiontext- Text responseserror- Error events
Return Value
RunState object:
sessionState- Internal state for continuing conversationsoutput- The agent's output (text, error, or structured data)
Use Cases
- CI/CD pipelines: code review, test generation, refactoring
- Batch processing
- VS Code extensions, web apps
- Automation scripts
Examples
Test Generation
typescript
import { CodebuffClient } from '@codebuff/sdk'const CODE_TO_TEST = `function add(a: number, b: number): number {return a + b;}function divide(a: number, b: number): number {if (b === 0) throw new Error('Division by zero');return a / b;}`async function generateTests(code: string) {const client = new CodebuffClient({apiKey: process.env.CODEBUFF_API_KEY,})const { output } = await client.run({agent: 'codebuff/base@latest',prompt: `Generate unit tests for these functions using Jest:\n\n${code}`,})return output}
Code Review in CI
Use the reviewer agent for automated code review:
typescript
import { CodebuffClient } from '@codebuff/sdk'async function reviewPullRequest(diff: string) {const client = new CodebuffClient({apiKey: process.env.CODEBUFF_API_KEY,})const { output } = await client.run({agent: 'codebuff/reviewer@latest',prompt: `Review this code change for bugs, security issues, and best practices:\n\n${diff}`,})return output}
Batch Processing
typescript
import { CodebuffClient } from '@codebuff/sdk'import * as fs from 'fs'import * as path from 'path'async function addDocstringsToFiles(filePaths: string[]) {const client = new CodebuffClient({apiKey: process.env.CODEBUFF_API_KEY,})const results = []for (const filePath of filePaths) {const code = fs.readFileSync(filePath, 'utf-8')const { output } = await client.run({agent: 'codebuff/base@latest',prompt: `Add JSDoc comments to all functions in this file:\n\n${code}`,})results.push({ filePath, output })}return results}// Usageconst files = ['src/utils.ts', 'src/helpers.ts', 'src/api.ts']await addDocstringsToFiles(files)
Code Refactoring
typescript
import { CodebuffClient } from '@codebuff/sdk'const LEGACY_CODE = `function processData(data) {let result = [];for (let i = 0; i < data.length; i++) {if (data[i].active === true) {result.push({id: data[i].id,name: data[i].name.toUpperCase()});}}return result;}`async function refactorCode(code: string) {const client = new CodebuffClient({apiKey: process.env.CODEBUFF_API_KEY,})const { output } = await client.run({agent: 'codebuff/base@latest',prompt: `Refactor this code to use modern JavaScript (arrow functions,destructuring, array methods like filter/map):\n\n${code}`,})return output}
Providing Project Context
typescript
import { CodebuffClient } from '@codebuff/sdk'import * as fs from 'fs'async function generateComponentWithContext() {const client = new CodebuffClient({apiKey: process.env.CODEBUFF_API_KEY,})// Provide existing files as contextconst projectFiles = {'src/types.ts': fs.readFileSync('src/types.ts', 'utf-8'),'src/components/Button.tsx': fs.readFileSync('src/components/Button.tsx', 'utf-8'),}const { output } = await client.run({agent: 'codebuff/base@latest',prompt: 'Create a new Card component that follows the same patterns as Button',projectFiles,})return output}
GitHub Actions Example
yaml
name: AI Code Reviewon:pull_request:types: [opened, synchronize]jobs:review:runs-on: ubuntu-lateststeps:- uses: actions/checkout@v4with:fetch-depth: 0- name: Get diffid: diffrun: echo "diff=$(git diff origin/main...HEAD)" >> $GITHUB_OUTPUT- name: Run AI Reviewrun: |npx ts-node scripts/ai-review.ts "${{ steps.diff.outputs.diff }}"env:CODEBUFF_API_KEY: ${{ secrets.CODEBUFF_API_KEY }}