Overview
Theanalyze-topic function is the main orchestrator that:
- Creates or retrieves a topic from the database
- Calls
fetch-twitter,fetch-reddit, andfetch-youtubein parallel - Runs
analyze-sentimenton collected posts - Returns a comprehensive status report
Endpoint
Request
The search query to analyze (e.g., “iPhone 16”, “climate change”)
Display title for the topic. Defaults to the query value if not provided.
Associated hashtag for the topic. Auto-generated from query if not provided (e.g., “#iPhone16”).
Example Request
Response
Whether the orchestration completed successfully
UUID of the created or existing topic
Result from the Twitter/X fetch operation
Result from the Reddit fetch operation
Result from the YouTube fetch operation
Result from the sentiment analysis operation
Success Response Example
Partial Success Example
The orchestrator is designed for graceful degradation. Even if some data sources fail, the function will continue with available sources and return
success: true as long as at least one source provides data.Error Response
Behavior
Topic Deduplication
The function checks if a topic with the samequery already exists. If found, it reuses the existing topic ID instead of creating a duplicate.
Fallback Chain
Twitter/X Data Collection:- Scrape.do (X/Twitter + Reddit) - Primary
- Parallel AI Social Search - Secondary
- YouTube API - Tertiary
- Algorithmic generation - Final fallback
- Scrape.do (Reddit HTML scraping)
- YouTube Data API v3 (official)
Parallel Execution
All data fetching functions are called in parallel to minimize latency:Error Handling
Common Errors
| Error | Cause | HTTP Status |
|---|---|---|
query is required | Missing query parameter | 500 |
Topic not found | Invalid topic_id in database | 500 |
| Database connection errors | Supabase credentials invalid | 500 |
| Sub-function failures | Scraping/API errors | 200 (soft fail) |
Rate Limits
This function triggers multiple downstream API calls:- Scrape.do: 2-3 requests per call (X, Reddit, possible retries)
- YouTube API: ~100-200 quota units per call
- Gemini API: 1-2 requests per call (sentiment analysis + summary)
Performance
Typical execution time: 15-45 seconds- Fast path (all sources available): ~15-20s
- Fallback path (scraping blocked): ~25-35s
- Full fallback chain: ~40-45s
Best Practices
Use specific, focused queries for better data quality (“iPhone 16 Pro Max battery” vs “phones”)
Provide custom
title and hashtag for better topic organizationHandle partial success scenarios in your client code
Store the returned
topic_id to query results later via the database or APIRelated Functions
- fetch-twitter - X/Twitter data collection
- fetch-reddit - Reddit data collection
- fetch-youtube - YouTube comment collection
- analyze-sentiment - AI sentiment analysis