Đang tải…
Đang tải…
npx claude-code-templates@latest --sandbox cloudflare/READMEExecute Claude Code in isolated Cloudflare Workers sandboxes with AI-powered code generation.
npm install
# For local development, create .dev.vars:
echo "ANTHROPIC_API_KEY=your-api-key-here" > .dev.vars
# For production, use wrangler secrets:
npx wrangler secret put ANTHROPIC_API_KEY
# Start development server (requires Docker)
npm run dev
# In another terminal, test the endpoint:
curl -X POST http://localhost:8787/execute \
-H "Content-Type: application/json" \
-d '{"question": "What is the 10th Fibonacci number?"}'
# Deploy worker
npx wrangler deploy
# Wait 2-3 minutes for container provisioning
npx wrangler containers list
# Test production endpoint
curl -X POST https://your-worker.your-subdomain.workers.dev/execute \
-H "Content-Type: application/json" \
-d '{"question": "Calculate factorial of 5"}'
This sandbox combines three powerful technologies:
User Question → Cloudflare Worker → Claude AI → Generated Code → Sandbox → Results
Execute a natural language question as code.
Request:
{
"question": "What is the 100th Fibonacci number?",
"maxTokens": 2048, // Optional: Max tokens for code generation
"timeout": 30000, // Optional: Execution timeout in ms
"language": "python" // Optional: "python" or "javascript"
}
Response:
{
"success": true,
"question": "What is the 100th Fibonacci number?",
"code": "def fibonacci(n):\n if n <= 1:\n return n\n return fibonacci(n-1) + fibonacci(n-2)\n\nprint(fibonacci(100))",
"output": "354224848179261915075\n",
"error": "",
"sandboxId": "user-1234567890-abc123",
"executionTime": 2147
}
Check worker health status.
Response:
{
"status": "healthy",
"timestamp": "2025-10-19T12:00:00.000Z",
"worker": "cloudflare-claude-sandbox"
}
Execute prompts directly from command line:
# Basic usage
node launcher.ts "Calculate factorial of 5"
# With custom worker URL
node launcher.ts "Fibonacci 10" "" your_api_key https://your-worker.workers.dev
# With components
node launcher.ts "Create a React app" "--agent frontend-developer" your_api_key
Monitor execution with detailed metrics:
# Monitor execution
node monitor.ts "Calculate factorial of 5" your_api_key
# Monitor production worker
node monitor.ts "Sum array" your_api_key https://your-worker.workers.dev
curl -X POST http://localhost:8787/execute \
-H "Content-Type: application/json" \
-d '{"question": "What is the factorial of 20?"}'
curl -X POST http://localhost:8787/execute \
-H "Content-Type: application/json" \
-d '{"question": "Reverse the string Hello World"}'
curl -X POST http://localhost:8787/execute \
-H "Content-Type: application/json" \
-d '{"question": "Calculate the mean of [10, 20, 30, 40, 50]"}'
curl -X POST http://localhost:8787/execute \
-H "Content-Type: application/json" \
-d '{
"question": "Sort an array of numbers",
"language": "javascript"
}'
Local Development (.dev.vars):
ANTHROPIC_API_KEY=sk-ant-your-key-here
Production (Wrangler Secrets):
npx wrangler secret put ANTHROPIC_API_KEY
Edit wrangler.toml to customize:
# Worker name (must be unique)
name = "my-custom-sandbox"
# Resource limits
[limits]
cpu_ms = 100 # CPU time per request
# Environment-specific configuration
[env.production]
vars = { ENVIRONMENT = "production" }
After first deployment, wait 2-3 minutes:
npx wrangler containers list
Ensure Docker is running:
docker ps
For local development:
echo "ANTHROPIC_API_KEY=your-key" > .dev.vars
For production:
npx wrangler secret put ANTHROPIC_API_KEY
# Real-time logs
npx wrangler tail
# Pretty formatted
npx wrangler tail --format=pretty
Cloudflare Workers:
Anthropic API:
Example costs for 10,000 requests/month:
cloudflare-claude-sandbox/
├── src/
│ └── index.ts # Worker source code
├── launcher.ts # CLI launcher tool
├── monitor.ts # Monitoring tool
├── wrangler.toml # Cloudflare configuration
├── package.json # Dependencies
├── tsconfig.json # TypeScript config
└── README.md # This file
npm run dev - Start local development servernpm run deploy - Deploy to Cloudflarenpm run tail - View real-time logsnpm run launch - Run launcher toolnpm run monitor - Run monitoring toolnpm run type-check - Check TypeScript typesnpm test - Run testsMIT License - See LICENSE file for details
For issues and questions:
npx wrangler tailBuilt with ❤️ using Cloudflare Workers, Claude AI, and the Sandbox SDK