跳转到内容
搜索文档

在 Sandbox 上运行 Claude Code

最后更新 查看 MarkdownAgent 设置

构建一个 Worker,接收仓库 URL 与任务描述,并使用 Sandbox SDK 运行 Claude Code 以实现你的任务。

完成时间: 5 分钟

前提条件

  1. 注册 Cloudflare 账户
  2. 安装 Node.js

Node.js 版本管理器

使用 Voltanvm 等 Node 版本管理器,以避免权限问题并切换 Node.js 版本。本指南后续将介绍的 Wrangler 需要 Node 版本 16.17.0 或更高。

你还需要:

1. 创建项目

创建新的 Sandbox SDK 项目:

npm create cloudflare@latest -- claude-code-sandbox --template=cloudflare/sandbox-sdk/examples/claude-code
cd claude-code-sandbox

2. 设置本地环境变量

在项目根目录创建 .dev.vars 文件用于本地开发:

echo "ANTHROPIC_API_KEY=your_api_key_here" > .dev.vars

your_api_key_here 替换为来自 Anthropic Console 的实际 API key。

3. 本地测试

启动开发服务器:

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 ![npm install agents 命令](assets/npm-install-agents.svg)\n "
}

4. 部署

部署你的 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 的输出与变更

后续步骤

相关资源

这篇文档对您有帮助吗?