YouTube Data API Setup Guide
This guide will help you get a YouTube Data API key to fetch video metadata.
Step 1: Create a Google Cloud Projectโ
- Go to Google Cloud Console
- Sign in with your Google account
- Click Select a project (top left, next to "Google Cloud")
- Click NEW PROJECT
- Enter project details:
- Project name:
Agentification Academy(or any name) - Organization: Leave as default
- Project name:
- Click CREATE
- Wait for project to be created (~30 seconds)
Step 2: Enable YouTube Data API v3โ
- Make sure your new project is selected (check top left)
- Go to APIs & Services > Library
- Search for:
YouTube Data API v3 - Click on YouTube Data API v3
- Click ENABLE
- Wait for it to enable (~10 seconds)
Step 3: Create API Keyโ
- Go to APIs & Services > Credentials
- Click + CREATE CREDENTIALS (top of page)
- Select API key
- A dialog will appear with your new API key
- Copy the API key (looks like:
AIzaSyD...) - Click CLOSE
Step 4: Restrict the API Key (Recommended for Security)โ
- Find your newly created API key in the list
- Click the Edit icon (pencil) next to it
- Under API restrictions:
- Select Restrict key
- Check YouTube Data API v3
- Uncheck all others
- Under Application restrictions:
- Select HTTP referrers (web sites)
- Click + ADD AN ITEM
- Add your domain(s):
http://localhost:*https://localhost:*https://your-domain.com/*https://*.vercel.app/*
- Click SAVE
Step 5: Add API Key to Environment Variablesโ
For Local Development (.env.local or .env)โ
Add to your .env or .env.local file:
VITE_YOUTUBE_API_KEY=AIzaSyD_YOUR_ACTUAL_KEY_HERE
Replace AIzaSyD_YOUR_ACTUAL_KEY_HERE with your actual API key!
For Production (Vercel)โ
If deploying to Vercel:
- Go to your Vercel project dashboard
- Navigate to Settings > Environment Variables
- Add new variable:
- Name:
VITE_YOUTUBE_API_KEY - Value: Your API key
- Environments: Check Production, Preview, Development
- Name:
- Click Save
- Redeploy your application for changes to take effect
Step 6: Verify API Key Worksโ
Run this test in your browser console or use curl:
# Replace YOUR_API_KEY with your actual key
# Replace VIDEO_ID with any YouTube video ID (e.g., dQw4w9WgXcQ)
curl "https://www.googleapis.com/youtube/v3/videos?id=dQw4w9WgXcQ&part=snippet,contentDetails&key=YOUR_API_KEY"
Expected response: JSON with video title, duration, thumbnail, etc.
If you get an error:
- Check API key is correct
- Verify YouTube Data API v3 is enabled
- Check restrictions aren't blocking your request
API Quota Informationโ
Free Tier Limits:โ
- 10,000 quota units per day (FREE)
- 1 video metadata request = 1 unit
- Quota resets daily at midnight Pacific Time
Quota Calculation:โ
10,000 units/day รท 1 unit per request = 10,000 video metadata fetches/day
This is more than enough! Even if you add 100 lessons/day, you'll only use 100 units.
If You Need More:โ
- Most unlikely, but you can request quota increase in Google Cloud Console
- Or upgrade to paid plan (costs vary)
Security Best Practicesโ
โ DO:โ
- Use environment variables (never hardcode)
- Restrict API key to YouTube Data API v3 only
- Add domain restrictions
- Store in
.envfiles (add to.gitignore) - Use separate keys for dev/production
โ DON'T:โ
- Commit API keys to Git
- Share API keys publicly
- Use same key across multiple projects
- Leave key unrestricted
Common Issues & Solutionsโ
Issue: "API key not valid"โ
Solution:
- Double-check you copied the entire key
- Ensure no extra spaces
- Verify API key hasn't been deleted
Issue: "The request cannot be completed because you have exceeded your quota"โ
Solution:
- Check Google Cloud Console quota usage
- Wait for daily reset (midnight PT)
- Request quota increase if needed
Issue: "Access Not Configured"โ
Solution:
- Make sure YouTube Data API v3 is enabled
- Wait a few minutes after enabling
- Try creating a new API key
Issue: API key works in curl but not in appโ
Solution:
- Check domain restrictions match your site
- Verify environment variable name:
VITE_YOUTUBE_API_KEY - Restart dev server after adding env variable
- Check variable is being loaded:
console.log(import.meta.env.VITE_YOUTUBE_API_KEY)
Environment Variable Checklistโ
Before proceeding to use the YouTube service:
- Created Google Cloud project
- Enabled YouTube Data API v3
- Generated API key
- Restricted API key (optional but recommended)
- Added
VITE_YOUTUBE_API_KEYto.envor.env.local - Restarted development server
- Tested API key with curl
- Verified environment variable loads in app
Test Your Setupโ
After adding the API key to your environment:
// Add this to any component temporarily
console.log('YouTube API Key:', import.meta.env.VITE_YOUTUBE_API_KEY);
// Should output: YouTube API Key: AIzaSyD...
// If undefined, restart your dev server!
Next Stepsโ
Once you have your API key set up:
- โ
API key added to
.env - โ Dev server restarted
- โ Environment variable confirmed
You're ready to use the YouTube service!
The service will automatically:
- Extract video IDs from YouTube URLs
- Fetch video metadata (title, duration, thumbnail)
- Parse duration from ISO 8601 format
- Validate video IDs
Additional Resourcesโ
API Key Ready? Let's start building the YouTube integration! ๐ฅ