Overview
Thefetch-youtube function collects YouTube video comments related to a topic using the official YouTube Data API v3. It searches for relevant videos and extracts comments to capture public sentiment.
Endpoint
This function is designed to be called internally by the
analyze-topic orchestrator.Request
UUID of the topic to fetch YouTube comments for
Example Request
Response
Whether YouTube comments were successfully fetched and stored
Total number of comments retrieved across all videos
Number of comments successfully inserted into the database
Optional description of any errors or warnings
Success Response
No Videos Found
API Error Response
Exception Response
API Workflow
Step 1: Video Search
Searches for videos matching the topic query:Requests video metadata (title, description, channel)
Restricts results to videos only (excludes playlists, channels)
Number of videos to retrieve (max 50 per API limits)
Sorts by upload date (newest first)
Step 2: Comment Extraction
For each video, fetches top-level comments:Number of comments per video (max 100 per API limits)
Sorts by YouTube’s relevance algorithm (prioritizes engagement)
Comment Schema
Extracted comments are stored in theposts table:
HTML Stripping
YouTube comments may contain HTML tags (links, formatting). These are stripped:Check out my channel <a href="...">here</a>!After:
Check out my channel here!
Deduplication
Comments are upserted using the composite unique constraint:Error Handling
Common API Errors
| Error | Cause | Response |
|---|---|---|
| HTTP 403 | Invalid/expired API key or quota exceeded | info: "YouTube API returned 403" |
| HTTP 400 | Invalid video ID or parameter | Function skips video, continues |
| HTTP 404 | Video deleted or private | Function skips video, continues |
| Disabled comments | Video has comments disabled | Function skips video, continues |
Graceful Degradation
If some videos fail to fetch comments, the function continues with remaining videos:Performance
Typical execution time: 4-8 seconds- Video search: ~1-2s
- Comment fetching (5 videos): ~3-6s
- Database inserts: <1s
Slow path (many comments): ~8-12s
Rate Limits
Quota Exceeded Response
When quota is exhausted, YouTube returns HTTP 403:Environment Variables
YouTube Data API v3 key from Google Cloud Console. Create one at https://console.cloud.google.com/apis/credentials
Auto-injected by Supabase
Auto-injected by Supabase
Best Practices
Monitor YouTube API quota usage in Google Cloud Console
Increase
maxResults for videos/comments if you need more data (costs more quota)Use
order=relevance for comments to get higher quality sentiment dataConsider increasing video search to 10-15 results for broader coverage
Handle
success: false gracefully - YouTube is a supplementary data sourceQuota Optimization
To maximize data within quota limits:The current implementation uses Option 1 (5 videos × 20 comments = 100 comments).
Related Functions
- analyze-topic - Orchestrator calling this function
- fetch-twitter - Uses YouTube as a tertiary fallback
- analyze-sentiment - Processes collected YouTube comments