Overview
The YouTube integration uses the official YouTube Data API v3 to search for relevant videos and collect their comment threads. Unlike X and Reddit which require web scraping, YouTube provides a stable, well-documented API.YouTube data collection is implemented in
supabase/functions/fetch-youtube/index.ts and serves as a fallback when X/Reddit scraping fails.How It Works
The YouTube fetcher follows a two-step process:Step 1: Search for Videos
Order: date ensures the most recent videos appear first, providing real-time sentiment data.
Step 2: Fetch Comments for Each Video
API Endpoints
Search Endpoint
URL:https://www.googleapis.com/youtube/v3/search
Parameters:
| Parameter | Value | Description |
|---|---|---|
part | snippet | Returns title, description, thumbnails |
q | {topic.query} | Search query |
type | video | Only return videos (not channels/playlists) |
maxResults | 5 | Number of videos to retrieve |
order | date | Sort by upload date (newest first) |
key | {API_KEY} | YouTube Data API key |
Comments Endpoint
URL:https://www.googleapis.com/youtube/v3/commentThreads
Parameters:
| Parameter | Value | Description |
|---|---|---|
part | snippet | Returns comment metadata |
videoId | {videoId} | Video to fetch comments from |
maxResults | 20 | Comments per video |
order | relevance | Sort by relevance (top comments) |
key | {API_KEY} | YouTube Data API key |
Data Extraction
Comment Structure
YouTube comments are nested inside the response:Comment Ordering
The API supports two ordering strategies:Why use 'relevance' instead of 'time'?
Why use 'relevance' instead of 'time'?
Relevance-sorted comments are better for sentiment analysis because:
- Higher engagement = more representative of viewer opinions
- Top comments are more likely to be substantive (not spam)
- Controversial opinions rise to the top (important for sentiment diversity)
Error Handling
API Error Detection
Common HTTP Status Codes
| Status | Meaning | Solution |
|---|---|---|
200 | Success | Process data |
400 | Bad Request | Check query parameters |
403 | Forbidden | API key invalid or quota exceeded |
404 | Not Found | Video doesn’t exist or has comments disabled |
Rate Limits & Quota
YouTube Data API v3 Quota
Free Tier: 10,000 units/day
Paid Tier: Request quota increase via Google Cloud Console
Paid Tier: Request quota increase via Google Cloud Console
Quota Costs
| Operation | Cost | Notes |
|---|---|---|
search | 100 units | Per request |
commentThreads | 1 unit | Per video |
videos | 1 unit | Not used in SENTi-radar |
Daily Usage Calculation
To analyze more topics, reduce
maxResults for search or skip videos with comments disabled.No Results Handling
Why return success: true when no videos found?
Why return success: true when no videos found?
This prevents the orchestrator from treating it as a failure. No videos is a valid outcome (e.g., query is too new or niche), not an error.Errors should only be returned for:
- API authentication failures
- Network timeouts
- Malformed requests
Database Persistence
Response Format
Response Fields
success:trueif API calls succeeded (even if no comments found)fetched: Total comments retrieved from YouTubeinserted: Comments successfully saved (may be less than fetched due to duplicates)
Environment Setup
1. Create YouTube API Key
Go to Google Cloud Console
Visit console.cloud.google.com
Enable YouTube Data API v3
APIs & Services → Enable APIs and Services → Search “YouTube Data API v3” → Enable
2. Configure Environment
Testing
Common Issues
403 Forbidden: quotaExceeded
403 Forbidden: quotaExceeded
Cause: Daily quota limit (10,000 units) exceededSolutions:
- Wait until quota resets (midnight Pacific Time)
- Request quota increase in Google Cloud Console
- Reduce
maxResultsin search and commentThreads - Implement caching to avoid redundant API calls
403 Forbidden: commentsDisabled
403 Forbidden: commentsDisabled
Cause: Video creator disabled commentsSolution: The code already handles this gracefully:
API returns empty items array
API returns empty items array
Causes:
- Query has no YouTube videos (check youtube.com/results manually)
- All videos have comments disabled
- Videos are age-restricted or private
textDisplay contains HTML tags
textDisplay contains HTML tags
Optimization Tips
Reduce Quota Usage
Reduce Quota Usage
Parallelize Comment Fetching
Parallelize Comment Fetching
Cache Video IDs
Cache Video IDs
Next Steps
Data Sources Overview
Understand the complete data pipeline
Sentiment Analysis
How YouTube comments are analyzed