Cloudflare Workers Integration
Spyglasses integrates seamlessly with Cloudflare Workers to provide edge-based bot detection and AI referrer tracking. This integration is perfect for Webflow sites, e-commerce platforms, and any application that needs to understand and control bot traffic at the edge.
Quick Start with Template
🚀 New to Cloudflare Workers? Use our ready-to-deploy template:
📖 Complete example repository: github.com/orchestra-code/spyglasses-cloudflare-worker-example
✅ No file editing required - all configuration is done in the Cloudflare dashboard!
Manual Installation
For custom implementations, follow these steps:
-
Install the package:
with yarn:
with pnpm:
-
Create your Worker: Create a new
src/index.ts
file: -
Configure your Worker: Create a
wrangler.toml
file:
Configuration
Environment Variables
Variable | Required | Description | Default |
---|---|---|---|
ORIGIN_URL | ✅ | Your origin server URL | - |
SPYGLASSES_API_KEY | ❌ | API key for analytics and updates | - |
SPYGLASSES_DEBUG | ❌ | Enable debug logging | false |
SPYGLASSES_COLLECTOR_ENDPOINT | ❌ | Custom analytics endpoint | Spyglasses default |
SPYGLASSES_PATTERNS_ENDPOINT | ❌ | Custom endpoint for loading AI and bot patterns | Spyglasses default |
SPYGLASSES_CACHE_TTL | ❌ | Pattern cache TTL in seconds | 3600 |
Setting Your API Key
For analytics and enhanced patterns, set your API key as a secret:
Deployment
Deploy your Worker to Cloudflare:
Advanced Configuration
For custom configuration, create your own worker:
Use Cases
Webflow Sites
Perfect for Webflow sites that need bot protection:
E-commerce Protection
Protect product pages from aggressive scrapers:
Multiple Origins
Use different origins for different paths:
Understanding Detection Results
The worker adds custom headers to help you understand what's happening:
Detection Types
- 🤖 Bot Detection: Identifies crawlers, scrapers, and AI trainers
- 🧠 AI Referrers: Tracks human visitors from AI platforms
- ✅ Legitimate Traffic: Passes through unmodified
Blocking Behavior
- Blocked bots receive a
403 Forbidden
response - AI referrers are logged but never blocked (they're human visitors)
- Regular visitors pass through normally
Debugging
Enable debug mode to see detailed logging:
Check your Worker logs:
You'll see detailed information about:
- Which patterns matched
- Why requests were blocked or allowed
- Logging success/failure
- Performance metrics
Custom Routing
Set up custom routing in your Cloudflare dashboard:
- Go to your domain in Cloudflare
- Navigate to Workers Routes
- Add a route pattern like
*your-domain.com/*
- Select your Spyglasses Worker
Performance
- Sub-millisecond detection using compiled regex patterns
- Minimal memory footprint with smart caching
- Background logging to avoid blocking responses
- Edge distribution for global low latency
- Bundle size: ~66KB total, ~13KB gzipped
Verifying Installation
After deploying your Worker, you can verify the installation by:
- Check logs: Use
npx wrangler tail
to see real-time logs - Test with debug: Enable debug mode to see detailed detection info
- Dashboard data: Check your Spyglasses dashboard for incoming data
- Headers: Look for
X-Spyglasses-Processed: true
in responses
Troubleshooting
Worker Not Triggering
- Verify your route pattern in Cloudflare dashboard
- Check that the Worker is deployed to the correct zone
- Ensure your domain is using Cloudflare nameservers
Origin Connection Issues
- Verify
ORIGIN_URL
is set correctly - Check that your origin server is accessible
- Ensure SSL certificates are properly configured
No Data in Dashboard
- Verify your API key is set correctly
- Check Worker logs for any errors
- Ensure your domain is properly configured in Spyglasses
Performance Issues
- Enable debug mode to identify bottlenecks
- Check your exclude paths configuration
- Verify your origin server response times
Security Best Practices
-
Use Secrets: Store your API key as a secret, not a variable
-
Limit Scope: Use path exclusions to avoid processing sensitive routes
-
Monitor Logs: Enable debug mode in staging to verify behavior
-
Test Thoroughly: Use
--dry-run
to test deployments
Pricing
Cloudflare Workers usage is included in most Cloudflare plans:
- Free Plan: 100,000 requests/day
- Paid Plans: 10M+ requests/month included
Need help? Contact support@spyglasses.io