Deploy intelligent AI agents powered by Swarms API on Cloudflare Workers edge network. Build production-ready cron agents that run automatically, fetch real-time data, perform AI analysis, and execute actions across 330+ cities worldwide.
This repository demonstrates how to combine Swarms API multi-agent intelligence with Cloudflare Workers edge computing to create autonomous AI systems that:
- ⚡ Execute automatically on predefined schedules (cron jobs)
- 📊 Fetch real-time data from external APIs (Yahoo Finance, news feeds)
- 🤖 Perform intelligent analysis using specialized Swarms AI agents
- 📧 Take automated actions (email alerts, reports, notifications)
- 🌍 Scale globally on Cloudflare's edge network with sub-100ms latency
This repository provides two complete implementations of stock analysis agents:
The original implementation using JavaScript/TypeScript on Cloudflare Workers.
A Python Workers implementation using Cloudflare's beta Python runtime with Pyodide.
Both implementations demonstrate a complete system that:
- Runs automated stock analysis every 3 hours using Cloudflare Workers cron
- Fetches real-time market data from Yahoo Finance API (no API key needed)
- Collects market news from Financial Modeling Prep API (optional)
- Deploys multiple Swarms AI agents for technical and fundamental analysis
- Sends comprehensive email reports via Mailgun
- Provides a web interface for manual triggers and monitoring
| Feature | JavaScript (stock-agent/) |
Python (python-stock-agent/) |
|---|---|---|
| Runtime | V8 JavaScript Engine | Pyodide Python Runtime |
| Language | JavaScript/TypeScript | Python 3.x |
| Status | Production Ready | Beta (Python Workers) |
| Performance | Optimized V8 execution | Good, with Python stdlib support |
| Syntax | fetch(), JSON.stringify() |
await fetch(), json.dumps() |
| Error Handling | try/catch |
try/except |
| Libraries | Built-in Web APIs | Python stdlib + select packages |
| Development | Mature tooling | Growing ecosystem |
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ Cloudflare │ │ Data Sources │ │ Swarms API │
│ Workers Runtime │ │ │ │ │
│ "0 */3 * * *" │───▶│ Yahoo Finance │───▶│ Technical Agent │
│ JS | Python │ │ News APIs │ │ Fundamental │
│ scheduled() │ │ Market Data │ │ Agent Analysis │
│ Global Edge │ │ │ │ │
└─────────────────┘ └─────────────────┘ └─────────────────┘
Choose your preferred implementation:
# Clone the repository
git clone https://github.com/The-Swarm-Corporation/Swarms-CloudFlare-Deployment.git
cd Swarms-CloudFlare-Deployment/stock-agent
# Install dependencies
npm install# Clone the repository
git clone https://github.com/The-Swarm-Corporation/Swarms-CloudFlare-Deployment.git
cd Swarms-CloudFlare-Deployment/python-stock-agent
# Install dependencies (Wrangler CLI)
npm installCreate a .dev.vars file in the stock-agent/ directory:
# Required: Swarms API key
SWARMS_API_KEY=your-swarms-api-key-here
# Optional: Market news (free tier available)
FMP_API_KEY=your-fmp-api-key
# Optional: Email notifications
MAILGUN_API_KEY=your-mailgun-api-key
MAILGUN_DOMAIN=your-domain.com
RECIPIENT_EMAIL=your-email@example.comThe cron schedule is configured in wrangler.jsonc:
Common cron patterns:
"0 9 * * 1-5"- 9 AM weekdays only"0 */6 * * *"- Every 6 hours"0 0 * * *"- Daily at midnight
# Start local development server
npm run dev
# Visit http://localhost:8787 to test# Deploy to production
npm run deploy
# Your agent will be live at: https://stock-agent.your-subdomain.workers.devThe stock agent uses two specialized AI agents:
-
Technical Analyst Agent:
- Calculates technical indicators (RSI, MACD, Moving Averages)
- Identifies support/resistance levels
- Provides trading signals and price targets
-
Fundamental Analyst Agent:
- Analyzes market conditions and sentiment
- Evaluates news and economic indicators
- Provides investment recommendations
- Yahoo Finance API: Free real-time stock data (no API key required)
- Financial Modeling Prep: Market news and additional data (free tier: 250 requests/day)
- Mailgun: Email delivery service (free tier: 5,000 emails/month)
- Real-time status monitoring
- Manual analysis triggers
- Progress tracking with visual feedback
- Analysis results display
- Scheduled cron job execution
- Error handling and recovery
- Cost tracking and monitoring
- Email report generation
- Comprehensive error handling
- Timeout protection
- Rate limiting compliance
- Security best practices
Edit the symbols array in src/index.js:
const symbols = ['SPY', 'QQQ', 'AAPL', 'MSFT', 'TSLA', 'NVDA', 'AMZN', 'GOOGL'];Modify the agent configuration:
const swarmConfig = {
agents: [
{
agent_name: "Risk Assessment Agent",
system_prompt: "Analyze portfolio risk and provide recommendations...",
model_name: "gpt-4o-mini",
max_tokens: 2000,
temperature: 0.1
}
]
};- Cloudflare Workers: Free tier includes 100,000 requests/day
- Swarms API: Monitor usage in dashboard, use gpt-4o-mini for cost efficiency
- External APIs: Leverage free tiers and implement intelligent caching
- Store API keys as Cloudflare Workers secrets
- Implement request validation and rate limiting
- Audit AI decisions and maintain compliance logs
- Use HTTPS for all external API calls
- Cloudflare Workers analytics dashboard
- Real-time performance metrics
- Error tracking and alerting
- Cost monitoring and optimization
- API Key Errors: Verify environment variables are set correctly
- Cron Not Triggering: Check cron syntax and Cloudflare Workers limits
- Email Not Sending: Verify Mailgun configuration and domain setup
- Data Fetch Failures: Check external API status and rate limits
Enable detailed logging by setting:
console.log('Debug mode enabled');- Cloudflare Workers Documentation
- Swarms API Documentation
- Cron Expression Generator
- Financial Modeling Prep API
This project is open source and available under the MIT License.
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
- Issues: GitHub Issues
- Discussions: GitHub Discussions
- Discord: Join our community
Built with Swarms framework for production-grade agentic applications
{ "triggers": { "crons": [ "0 */3 * * *" // Every 3 hours ] } }