Edge Functions Overview
The platform includes 7 edge functions:analyze-sentiment
Analyzes post sentiment using Gemini AI or keyword fallback
analyze-topic
Orchestrates topic creation and data fetching workflow
fetch-twitter
Scrapes X (Twitter) posts via Scrape.do
fetch-reddit
Scrapes Reddit posts via Scrape.do JSON API
fetch-youtube
Fetches YouTube video comments via YouTube Data API
generate-insights
Generates AI-powered insights and summaries
scheduled-monitor
Background job for monitoring topics and generating alerts
Prerequisites
Supabase project created
Create a project at supabase.com
Environment secrets ready
See Environment & Secrets for required API keys
Function Configuration
Edge functions are configured insupabase/config.toml:
supabase/config.toml
Deploying Edge Functions
Deploy All Functions
Deploy all edge functions at once:Deploy Individual Functions
Deploy specific functions:Deployment with Secrets
Set secrets before deploying:Secrets are encrypted and stored securely in Supabase. They are accessible via
Deno.env.get() within edge functions.Function Architecture
analyze-sentiment
Purpose: Analyzes sentiment and emotions of social media posts Flow:- Fetches unanalyzed posts for a topic
- Sends posts to Gemini AI for sentiment analysis
- Falls back to keyword-based analysis if Gemini fails
- Updates posts with sentiment scores
- Computes aggregate statistics
- Generates AI summary and key takeaways
- Creates crisis alerts if negative sentiment is high
GEMINI_API_KEY(optional, falls back to keywords)SUPABASE_URLSUPABASE_SERVICE_ROLE_KEY
supabase/functions/analyze-sentiment/index.ts
fetch-twitter
Purpose: Scrapes X (Twitter) posts and Reddit discussions via Scrape.do Flow:- Receives topic query
- Builds Scrape.do API request for X search
- Parses rendered HTML to extract tweet text
- Fetches Reddit posts via JSON API
- Falls back to Parallel.ai, then YouTube, then algorithmic generation
- Persists posts to database
SCRAPE_DO_TOKEN(required for live data)PARALLEL_API_KEY(optional fallback)YOUTUBE_API_KEY(optional fallback)SUPABASE_URLSUPABASE_SERVICE_ROLE_KEY
analyze-topic
Purpose: Orchestrates the entire topic analysis workflow Flow:- Creates or retrieves topic from database
- Calls
fetch-twitterto scrape X and Reddit - Calls
fetch-redditfor additional Reddit data - Calls
fetch-youtubefor video comments - Calls
analyze-sentimentto analyze all posts - Returns aggregated results
- All dependencies from called functions
SUPABASE_URLSUPABASE_SERVICE_ROLE_KEY
supabase/functions/analyze-topic/index.ts
Testing Edge Functions
Local Testing
Run functions locally with Supabase CLI:Production Testing
Test deployed functions:Monitoring & Logs
View Function Logs
View real-time logs:Check Function Status
List all deployed functions:Error Handling
All edge functions include comprehensive error handling:CORS Configuration
All functions include CORS headers for cross-origin requests:Performance Optimization
Caching Strategies
- Database-level caching: Use Supabase’s built-in query caching
- Function-level caching: Implement TTL-based caching for API responses
- Client-side caching: Use React Query’s stale-while-revalidate pattern
Parallel Processing
Functions use parallel fetching where possible:Troubleshooting
Function returns 500 error
Function returns 500 error
Cause: Missing environment variables or invalid API keysSolution:
- Check function logs:
supabase functions logs <function-name> - Verify all secrets are set:
supabase secrets list - Test API keys manually
Scrape.do returns empty results
Scrape.do returns empty results
Cause: X.com blocking requests or rate limitingSolution:
- Enable
super: truefor residential proxies - Add
waitUntil: 'networkidle0'to ensure full page load - Check Scrape.do dashboard for quota usage
Gemini API timeout
Gemini API timeout
Cause: Large batch of posts or API latencySolution:
- Reduce batch size in
analyze-sentiment(currently 50) - Implement retry logic with exponential backoff
- Use keyword fallback if Gemini consistently fails
Function deployment fails
Function deployment fails
Cause: Syntax error or missing dependenciesSolution:
- Test function locally first
- Check Deno import URLs are valid
- Review deployment logs for specific errors
Security Best Practices
CI/CD Integration
Automate edge function deployment with GitHub Actions:.github/workflows/deploy-functions.yml
Next Steps
Configure Secrets
Set up all required API keys and secrets
Deploy Frontend
Deploy the React frontend to connect to edge functions