Cloudflare Worker that provides secure access to Anthropic's Claude API with context validation and system prompt management for LimeSpot applications.
npm installCreate a .dev.vars file for local development (gitignored):
echo "CLAUDE_API_KEY=sk-ant-api03-your-key-here" > .dev.varsGet your API key from Anthropic Console → API Keys.
Note: .dev.vars is gitignored and never committed. Production uses Cloudflare encrypted secrets.
npm run devServer runs at http://localhost:8787
# Health check
curl http://localhost:8787/health
# Expected response:
# {"status":"ok","message":"App AI service is running","timestamp":"..."}All endpoints except /health require X-Personalizer-Context-ID header for authentication.
| Method | Endpoint | Description | Auth Required |
|---|---|---|---|
| GET | /health |
Health check | No |
| POST | /files |
Upload file to Claude | Yes |
| GET | /files |
List uploaded files | Yes |
| DELETE | /files/{id} |
Delete file | Yes |
| POST | /messages |
Send message to Claude | Yes |
X-Personalizer-Context-ID(required for protected endpoints): Authentication token validated against Brain APIX-Personalizer-System-Prompt(optional): System prompt name (e.g., "image-selection")
curl -X POST http://localhost:8787/messages \
-H "Content-Type: application/json" \
-H "X-Personalizer-Context-ID: your-context-id" \
-H "X-Personalizer-System-Prompt: image-selection" \
-d '{
"model": "claude-3-5-sonnet-20241022",
"max_tokens": 1024,
"messages": [{"role": "user", "content": "Hello"}]
}'# Start dev server
npm run dev
# Watch logs
# (logs appear in same terminal)
# Test endpoints
curl http://localhost:8787/healthapp-ai/
├── src/
│ ├── index.js # Main worker code
│ └── prompts.js # System prompts
├── wrangler.toml # Configuration (public settings)
├── .dev.vars # Local secrets (gitignored)
└── package.json
Local Development:
- Public config in
wrangler.toml[env.dev.vars]section - Secrets in
.dev.varsfile (gitignored):CLAUDE_API_KEY=sk-ant-api03-...
Production:
- Public config in
wrangler.toml[vars]section (default) - Secrets set via Cloudflare Dashboard:
- Workers > app-ai > Settings > Variables > Encrypt
- Add
CLAUDE_API_KEYas encrypted secret
System prompts are bundled in src/prompts.js.
- Edit
src/prompts.js - Test locally:
npm run dev - Commit and push to GitHub (auto-deploys to production)
Update allowed origins in src/index.js before deploying:
const allowedOrigins = [
"http://localhost:4200", // Angular dev
"https://app.limespot.com", // Production
// Add more as needed
];This project uses Cloudflare's GitHub integration for automatic deployment:
- Commits to
mainbranch → Auto-deploy to production environment
Setup:
- Connect repository to Cloudflare via dashboard
- Configure branch-to-environment mapping (
main→app-ai) - Set
CLAUDE_API_KEYsecret in Cloudflare Dashboard:- Workers > app-ai > Settings > Variables > Encrypt
- Add secret with key
CLAUDE_API_KEY
No manual deployment needed - push to GitHub and Cloudflare auto-deploys.
For testing changes before committing to GitHub:
# Test manual deploy to development environment
npm run deploy
# View logs
npm run tailNote: Manual deployment should only be used for:
- Testing deployment process
- Emergency hotfixes
- Validating changes before pushing to GitHub
All configured in wrangler.toml:
-
Local (
wrangler dev --env devornpm run dev):http://localhost:8787- Config:
wrangler.toml[env.dev.vars]section - Secrets:
.dev.varsfile (gitignored) - Brain API:
https://local.personalizer.io
- Config:
-
Production (Cloudflare auto-deploy):
https://app-ai.personalizer.io- Config:
wrangler.toml[vars]section (default) - Secrets: Cloudflare Dashboard (encrypted)
- Brain API:
https://personalizer.io - Auto-deploys from
mainbranch
- Config:
Each environment configuration includes:
ENVIRONMENT- Environment nameBRAIN_API_URL- Backend API URLCLAUDE_API_KEY- Anthropic API key
# Production logs
npm run tail-
API Keys
- Never commit API keys to git
- Local: Store in
.dev.vars(gitignored) - Production: Use Cloudflare encrypted secrets
- Rotate keys regularly
-
CORS
- Restrict allowed origins to known domains
- Never use
*in production
-
Context Validation
- All protected endpoints validate context ID against Brain API
- Invalid contexts return 401/403 errors
-
Input Validation
- File upload size limits enforced
- Request payloads validated
- Appropriate error codes returned
Some npm audit warnings exist for development dependencies (esbuild, vite). These are:
- Not in production (dev-only tools)
- Not critical (development server vulnerabilities)
- Mitigated by not exposing dev server to internet
For production: No action needed - vulnerabilities don't affect deployed workers.
Before deploying:
- API keys set as Cloudflare encrypted secrets (not in code)
-
.dev.varsis gitignored - CORS origins restricted to production domains
- No sensitive data in logs
- Context validation working
- Dependencies updated
lsof -i :8787
kill -9 <PID>- Check
.dev.varsfile exists withCLAUDE_API_KEY - Verify API key starts with
sk-ant-api03- - Ensure no extra spaces or quotes in
.dev.vars - Restart dev server after changes
- Verify app origin in allowed list (src/index.js)
- Restart dev server after changes
- Check browser console for specific error
- Verify context ID is valid in Brain API
- Check
BRAIN_API_URLenvironment variable - Review validation logs in worker output
# Check account ID in wrangler.toml
wrangler whoami
# Verify configuration
cat wrangler.toml | grep -A3 "\[vars\]"
# Check deployment logs
wrangler tail# Development
npm run dev # Start local server (port 8787)
npm run test # Run tests
# Deployment (Manual - for testing only)
npm run deploy # Deploy to default environment
# Monitoring
npm run tail # View production logs
# Maintenance
npm run update-deps # Update dependencies and run audit fix
npm audit # Check security
wrangler whoami # Check authentication
# Note: Staging and production deploy automatically via GitHub integration- Runtime: V8 Isolates (not Node.js)
- APIs: Web Standards (fetch, Request, Response, FormData)
- Limits: 50,000ms CPU time per request (configured in wrangler.toml)
- Scaling: Automatic, handles any traffic volume
- Global: Deployed to 300+ cities worldwide
- Cold start: <1ms (V8 isolates)
- Latency: 10-50ms (edge deployment)
- Throughput: 1000+ requests/second
- Uptime: 99.99% SLA
- Free tier: 100,000 requests/day
- Paid: $5/month for 10M requests
- No infrastructure: No servers to maintain
Status: Production Ready Platform: Cloudflare Workers Runtime: V8 Isolates