ccapi-server
v0.0.6
Published
Core HTTP server that wraps Claude Code CLI as an API endpoint
Maintainers
Readme
ccapi-server
ccapi 的核心 HTTP 服务包。将 Claude Code CLI 封装为 HTTP 端点 —— 接收 prompt,在 workspace 目录中运行 Claude,返回结果。
安装
该包通常作为 ccapi-cli 的依赖使用。也可以直接编程调用:
npm install ccapi-server编程调用
startServer(options?)
启动一个由 Claude Code 驱动的 HTTP 服务。
import { startServer } from 'ccapi-server';
startServer({
workspaceDir: './my-workspace', // 包含 CLAUDE.md 的目录
port: 3000,
maxTurns: 1,
timeoutMs: 60000,
});也可以通过环境变量配置:
| 选项 | 环境变量 | 默认值 |
| -------------- | --------------- | --------------- |
| workspaceDir | WORKSPACE_DIR | .(当前目录) |
| port | PORT | 3000 |
| maxTurns | MAX_TURNS | 1 |
| timeoutMs | TIMEOUT_MS | 60000 |
callClaude(options)
直接调用 Claude Code CLI 的底层函数。
import { callClaude } from 'ccapi-server';
const result = await callClaude({
prompt: 'Hello World',
cwd: './my-workspace',
maxTurns: 1,
timeoutMs: 60000,
});
console.log(result.result); // Claude 的回复
console.log(result.cost); // 费用(美元)
console.log(result.duration_ms); // 耗时(毫秒)HTTP API
POST /
请求:
{ "prompt": "Hello World" }响应:
{
"result": "你好世界",
"cost": 0.001,
"duration_ms": 1234
}GET /health
返回 { "status": "ok" }。
Workspace 目录
workspaceDir 是 Claude Code 的运行目录。在该目录下放置 CLAUDE.md 来定义 Claude 的行为(系统提示词),可选放置 .claude/settings.json 来限制可用工具��
my-workspace/
├── CLAUDE.md
└── .claude/
└── settings.json