构建一个 Worker,接收仓库 URL 与任务描述,并使用 Sandbox SDK 运行 Claude Code 以实现你的任务。
完成时间: 5 分钟
- 注册 Cloudflare 账户 ↗。
- 安装
Node.js↗。
Node.js 版本管理器
使用 Volta ↗ 或 nvm ↗ 等 Node 版本管理器,以避免权限问题并切换 Node.js 版本。本指南后续将介绍的 Wrangler 需要 Node 版本 16.17.0 或更高。
你还需要:
- 用于 Claude Code 的 Anthropic API key ↗
- 本地运行的 Docker ↗
创建新的 Sandbox SDK 项目:
npm create cloudflare@latest -- claude-code-sandbox --template=cloudflare/sandbox-sdk/examples/claude-codeyarn create cloudflare claude-code-sandbox --template=cloudflare/sandbox-sdk/examples/claude-codepnpm create cloudflare@latest claude-code-sandbox --template=cloudflare/sandbox-sdk/examples/claude-codecd claude-code-sandbox在项目根目录创建 .dev.vars 文件用于本地开发:
echo "ANTHROPIC_API_KEY=your_api_key_here" > .dev.vars将 your_api_key_here 替换为来自 Anthropic Console ↗ 的实际 API key。
启动开发服务器:
npm run dev使用 curl 测试:
curl -X POST http://localhost:8787/ \
-d '{
"repo": "https://github.com/cloudflare/agents",
"task": "remove the emojis from the readme"
}'响应:
{
"logs": "Done! I've removed the brain emoji from the README title. The heading now reads \"# Cloudflare Agents\" instead of \"# 🧠 Cloudflare Agents\".",
"diff": "diff --git a/README.md b/README.md\nindex 9296ac9..027c218 100644\n--- a/README.md\n+++ b/README.md\n@@ -1,4 +1,4 @@\n-# 🧠 Cloudflare Agents\n+# Cloudflare Agents\n \n \n "
}部署你的 Worker:
npx wrangler deploy然后将 Anthropic API key 设为生产环境 secret:
npx wrangler secret put ANTHROPIC_API_KEY出现提示时,粘贴来自 Anthropic Console ↗ 的 API key。
你创建了一个 API,它可以:
- 接受仓库 URL 与自然语言任务描述
- 创建 Sandbox 并将仓库克隆到其中
- 启动 Claude Code 以实现给定任务
- 返回 Claude 的输出与变更
- 使用 AI 分析数据 - 添加 pandas 与 matplotlib 进行数据分析
- Code Interpreter API - 使用内置代码解释器,而不是 exec
- 流式输出 - 显示实时执行进度
- API 参考 - 探索所有可用方法
- Anthropic Claude 文档 ↗
- Workers AI - 使用 Cloudflare 的内置模型