|
| 1 | +--- |
| 2 | +title: Run Claude Code on a Sandbox |
| 3 | +pcx_content_type: tutorial |
| 4 | +sidebar: |
| 5 | + order: 1 |
| 6 | +description: Use Claude Code to implement a task in your GitHub repository. |
| 7 | +--- |
| 8 | + |
| 9 | +import { Render, PackageManagers } from "~/components"; |
| 10 | + |
| 11 | +Build a Worker that takes a repository URL and a task description and uses Sandbox SDK to run Claude Code to implement your task. |
| 12 | + |
| 13 | +**Time to complete:** 5 minutes |
| 14 | + |
| 15 | +## Prerequisites |
| 16 | + |
| 17 | +<Render file="prereqs" product="workers" /> |
| 18 | + |
| 19 | +You'll also need: |
| 20 | +- An [Anthropic API key](https://console.anthropic.com/) for Claude Code |
| 21 | +- [Docker](https://www.docker.com/) running locally |
| 22 | + |
| 23 | +## 1. Create your project |
| 24 | + |
| 25 | +Create a new Sandbox SDK project: |
| 26 | + |
| 27 | +<PackageManagers |
| 28 | + type="create" |
| 29 | + pkg="cloudflare@latest" |
| 30 | + args={"claude-code-sandbox --template=cloudflare/sandbox-sdk/examples/claude-code"} |
| 31 | +/> |
| 32 | + |
| 33 | +```sh |
| 34 | +cd claude-code-sandbox |
| 35 | +``` |
| 36 | + |
| 37 | +## 2. Set up local environment variables |
| 38 | + |
| 39 | +Create a `.dev.vars` file in your project root for local development: |
| 40 | + |
| 41 | +```sh |
| 42 | +echo "ANTHROPIC_API_KEY=your_api_key_here" > .dev.vars |
| 43 | +``` |
| 44 | + |
| 45 | +Replace `your_api_key_here` with your actual API key from the [Anthropic Console](https://console.anthropic.com/). |
| 46 | + |
| 47 | +:::note |
| 48 | +The `.dev.vars` file is automatically gitignored and only used during local development with `npm run dev`. |
| 49 | +::: |
| 50 | + |
| 51 | +## 3. Test locally |
| 52 | + |
| 53 | +Start the development server: |
| 54 | + |
| 55 | +```sh |
| 56 | +npm run dev |
| 57 | +``` |
| 58 | + |
| 59 | +:::note |
| 60 | +First run builds the Docker container (2-3 minutes). Subsequent runs are much faster. |
| 61 | +::: |
| 62 | + |
| 63 | +Test with curl: |
| 64 | + |
| 65 | +```sh |
| 66 | +curl -X POST http://localhost:8787/ \ |
| 67 | + -d '{ |
| 68 | + "repo": "https://github.com/cloudflare/agents", |
| 69 | + "task": "remove the emojis from the readme" |
| 70 | + }' |
| 71 | +``` |
| 72 | + |
| 73 | +Response: |
| 74 | + |
| 75 | +```json |
| 76 | +{ |
| 77 | + "logs": "Done! I've removed the brain emoji from the README title. The heading now reads \"# Cloudflare Agents\" instead of \"# 🧠 Cloudflare Agents\".", |
| 78 | + "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 " |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +## 4. Deploy |
| 83 | + |
| 84 | +Deploy your Worker: |
| 85 | + |
| 86 | +```sh |
| 87 | +npx wrangler deploy |
| 88 | +``` |
| 89 | + |
| 90 | +Then set your Anthropic API key as a production secret: |
| 91 | + |
| 92 | +```sh |
| 93 | +npx wrangler secret put ANTHROPIC_API_KEY |
| 94 | +``` |
| 95 | + |
| 96 | +Paste your API key from the [Anthropic Console](https://console.anthropic.com/) when prompted. |
| 97 | + |
| 98 | +:::caution |
| 99 | +After first deployment, wait 2-3 minutes for container provisioning. Check status with `npx wrangler containers list`. |
| 100 | +::: |
| 101 | + |
| 102 | +## What you built |
| 103 | + |
| 104 | +You created an API that: |
| 105 | +- Accepts a repository URL and natural language task descriptions |
| 106 | +- Creates a Sandbox and clones the repository into it |
| 107 | +- Kicks off Claude Code to implement the given task |
| 108 | +- Returns Claude's output and changes |
| 109 | + |
| 110 | +## Next steps |
| 111 | + |
| 112 | +- [Analyze data with AI](/sandbox/tutorials/analyze-data-with-ai/) - Add pandas and matplotlib for data analysis |
| 113 | +- [Code Interpreter API](/sandbox/api/interpreter/) - Use the built-in code interpreter instead of exec |
| 114 | +- [Streaming output](/sandbox/guides/streaming-output/) - Show real-time execution progress |
| 115 | +- [API reference](/sandbox/api/) - Explore all available methods |
| 116 | + |
| 117 | +## Related resources |
| 118 | + |
| 119 | +- [Anthropic Claude documentation](https://docs.anthropic.com/) |
| 120 | +- [Workers AI](/workers-ai/) - Use Cloudflare's built-in models |
0 commit comments