Skip to content
Cloudflare Docs

Run Claude Code on a Sandbox

Build a Worker that takes a repository URL and a task description and uses Sandbox SDK to run Claude Code to implement your task.

Time to complete: 5 minutes

Prerequisites

  1. Sign up for a Cloudflare account.
  2. Install Node.js.

Node.js version manager

Use a Node version manager like Volta or nvm to avoid permission issues and change Node.js versions. Wrangler, discussed later in this guide, requires a Node version of 16.17.0 or later.

You'll also need:

1. Create your project

Create a new Sandbox SDK project:

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

2. Set up local environment variables

Create a .dev.vars file in your project root for local development:

Terminal window
echo "ANTHROPIC_API_KEY=your_api_key_here" > .dev.vars

Replace your_api_key_here with your actual API key from the Anthropic Console.

3. Test locally

Start the development server:

Terminal window
npm run dev

Test with curl:

Terminal window
curl -X POST http://localhost:8787/ \
-d '{
"repo": "https://github.com/cloudflare/agents",
"task": "remove the emojis from the readme"
}'

Response:

{
"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. Deploy

Deploy your Worker:

Terminal window
npx wrangler deploy

Then set your Anthropic API key as a production secret:

Terminal window
npx wrangler secret put ANTHROPIC_API_KEY

Paste your API key from the Anthropic Console when prompted.

What you built

You created an API that:

  • Accepts a repository URL and natural language task descriptions
  • Creates a Sandbox and clones the repository into it
  • Kicks off Claude Code to implement the given task
  • Returns Claude's output and changes

Next steps