YouTube API Video Parameters
March 8, 2025 ยท AI Conversations
parameters
### **Endpoint:**
GET https://www.googleapis.com/youtube/v3/videos
### **Required Parameters:**
| Parameter | Type | Description |
|———–|——|————-|
| `part` | string | Specifies the resource properties to include in the response (e.g., `snippet,contentDetails,statistics`). |
| `id` | string | A comma-separated list of video IDs to retrieve details for. |
| `key` | string | Your API key for authentication. |
### **Optional Parameters:**
| Parameter | Type | Description |
|———–|——|————-|
| `chart` | string | Specifies the chart type (`mostPopular`) to retrieve trending videos. |
| `myRating` | string | Returns videos rated by the authenticated user (`like` or `dislike`). |
| `regionCode` | string | Restricts results to a specific country (ISO 3166-1 alpha-2 code). |
| `hl` | string | Sets the language for video metadata. |
| `maxResults` | integer | Limits the number of results per request (default: `5`, max: `50`). |
| `videoCategoryId` | string | Filters results by a specific video category ID. |
### **Example Request:**
GET https://www.googleapis.com/youtube/v3/videos?part=snippet,statistics&id=VIDEO_ID&key=YOUR_API_KEY
Would you like help with generating an API request in a specific programming language? ๐
### **Usage**
– When fetching videos, you can pass `videoCategoryId` along with the `chart=mostPopular` parameter to get trending videos for a specific category.
– You can retrieve a list of all available categories using the `videoCategories` endpoint.
—
### **Example Request:**
GET https://www.googleapis.com/youtube/v3/videos?part=snippet&chart=mostPopular&videoCategoryId=10®ionCode=US&key=YOUR_API_KEY
– This request fetches the most popular videos in category `10` (Music) in the US.
—
### **List of Video Category IDs (Common Categories)**
To get all available categories, use:
GET https://www.googleapis.com/youtube/v3/videoCategories?part=snippet®ionCode=US&key=YOUR_API_KEY
Here are some common category IDs:
| ID | Category Name |
|—-|————–|
| 1 | Film & Animation |
| 2 | Autos & Vehicles |
| 10 | Music |
| 15 | Pets & Animals |
| 17 | Sports |
| 18 | Short Movies |
| 19 | Travel & Events |
| 20 | Gaming |
| 22 | People & Blogs |
| 23 | Comedy |
| 24 | Entertainment |
| 25 | News & Politics |
| 26 | How-to & Style |
| 27 | Education |
| 28 | Science & Technology |
| 29 | Nonprofits & Activism |
Let me know if you need further customization or help with fetching data! ๐
—
### **API Endpoint:**
GET https://www.googleapis.com/youtube/v3/videoCategories
### **Required Parameters:**
| Parameter | Type | Description |
|———–|——|————-|
| `part` | string | Specifies which resource properties to include in the response (`snippet`). |
| `regionCode` | string | Specifies the country code (ISO 3166-1 alpha-2) to filter categories available in that region. |
| `key` | string | Your API key for authentication. |
—
### **Example Request (Using cURL or Direct URL)**
GET https://www.googleapis.com/youtube/v3/videoCategories?part=snippet®ionCode=US&key=YOUR_API_KEY
This fetches all video categories available in the **United States (US)**.
—
### **Example Response:**
{
"kind": "youtube#videoCategoryListResponse",
"etag": "xyz123",
"items": [
{
"kind": "youtube#videoCategory",
"etag": "xyz123",
"id": "1",
"snippet": {
"title": "Film & Animation",
"assignable": true
}
},
{
"kind": "youtube#videoCategory",
"etag": "xyz456",
"id": "10",
"snippet": {
"title": "Music",
"assignable": true
}
}
]
}
Each item contains:
– **`id`**: The category ID (used in `videoCategoryId`).
– **`snippet.title`**: The category name.
—
### **Fetching Categories with JavaScript (Axios Example)**
const axios = require('axios');
const API_KEY = 'YOUR_API_KEY';
const REGION_CODE = 'US';
axios.get(`https://www.googleapis.com/youtube/v3/videoCategories`, {
params: {
part: 'snippet',
regionCode: REGION_CODE,
key: API_KEY
}
})
.then(response => {
console.log(response.data.items);
})
.catch(error => {
console.error(error);
});
—
### **Fetching Categories with PHP (cURL Example)**
<?php
$apiKey = "YOUR_API_KEY";
$regionCode = "US";
$url = "https://www.googleapis.com/youtube/v3/videoCategories?part=snippet®ionCode=$regionCode&key=$apiKey";
$response = file_get_contents($url);
$data = json_decode($response, true);
foreach ($data['items'] as $category) {
echo "ID: " . $category['id'] . " - " . $category['snippet']['title'] . "n";
}
?>
—
### **Key Notes:**
– The `regionCode` parameter ensures that the returned categories are available for that country.
– The `id` from the response can be used in `videoCategoryId` when fetching videos.
Would you like me to help with a specific implementation in Laravel, Vue, or any other framework? ๐
—
## **Option 1: Make Multiple Requests (Recommended)**
Since `videoCategoryId` only accepts a **single** category ID per request, you need to make separate requests for each category and then merge the results in your application.
### **Example: Fetch Videos from Categories 10 (Music) & 20 (Gaming) Separately**
GET https://www.googleapis.com/youtube/v3/videos?part=snippet&chart=mostPopular&videoCategoryId=10®ionCode=US&key=YOUR_API_KEY
GET https://www.googleapis.com/youtube/v3/videos?part=snippet&chart=mostPopular&videoCategoryId=20®ionCode=US&key=YOUR_API_KEY
After fetching results, merge them in your code.
—
## **Option 2: Fetch All Trending Videos & Filter Locally**
If you want to retrieve trending videos from **multiple categories**, you can fetch all trending videos (`chart=mostPopular`) **without specifying `videoCategoryId`** and then filter them based on category IDs in your application.
### **Example Request: Fetch All Popular Videos**
GET https://www.googleapis.com/youtube/v3/videos?part=snippet&chart=mostPopular®ionCode=US&key=YOUR_API_KEY
Then, filter the results based on the `snippet.categoryId`.
—
## **Implementation Example in JavaScript (Axios)**
const axios = require('axios');
const API_KEY = 'YOUR_API_KEY';
const REGION_CODE = 'US';
const CATEGORY_IDS = ['10', '20']; // Music & Gaming
async function fetchVideosByCategory(categoryId) {
const url = `https://www.googleapis.com/youtube/v3/videos`;
const params = {
part: 'snippet',
chart: 'mostPopular',
videoCategoryId: categoryId,
regionCode: REGION_CODE,
key: API_KEY
};
const response = await axios.get(url, { params });
return response.data.items;
}
async function fetchMultipleCategories() {
const videoPromises = CATEGORY_IDS.map(id => fetchVideosByCategory(id));
const results = await Promise.all(videoPromises);
const mergedVideos = results.flat(); // Merge results into one array
console.log(mergedVideos);
}
fetchMultipleCategories();
– This script makes separate API calls for each category.
– It merges all results into a single list.
—
## **Implementation Example in PHP (cURL)**
<?php
$apiKey = "YOUR_API_KEY";
$regionCode = "US";
$categoryIds = ["10", "20"]; // Music & Gaming
$videos = [];
foreach ($categoryIds as $categoryId) {
$url = "https://www.googleapis.com/youtube/v3/videos?part=snippet&chart=mostPopular&videoCategoryId=$categoryId®ionCode=$regionCode&key=$apiKey";
$response = file_get_contents($url);
$data = json_decode($response, true);
if (isset($data['items'])) {
$videos = array_merge($videos, $data['items']);
}
}
print_r($videos); // Output all videos from selected categories
?>
– This loops through multiple category IDs, fetches videos, and merges the results.
—
### **Summary**
– **YouTube API does NOT allow multiple `videoCategoryId` values in one request.**
– **Solution:** Make multiple API calls and merge results.
– **Alternative:** Fetch all trending videos and filter locally.
Would you like me to refine this approach for Vue.js, Laravel, or another framework? ๐
<script>
import { ref, watch } from 'vue';
import axios from "axios";
import IconLoading from './icons/IconLoading.vue';
import TwoColumn from '@/layouts/TwoColumn.vue';
const expandedPosts = ref({});
const sources = ref([
{ label: "India", value: "IN" },
{ label: "United States", value: "US" },
{ label: "United Kingdom", value: "GB" },
{ label: "France", value: "FR" }
]);
const videosCategories = ref([
{label: "Sports", value: "17"},
{label: "Travel & Events", value: "19"},
{label:'Science & Technology', value: "28"},
{label:'Music', value: "10"},
{label:'Education', value: "27"},
]); // Default country selection
const countryCode = ref(sources.value[0].value); // Default country selection
const category = ref(videosCategories.value[0].value); // Default category selection
console.log(category.value);
const videos = ref([]);
const loading = ref(true);
const error = ref(null);
const apiKey = "AIzaSyAp2pUhmJYqGIiv91p50Ss1Vjhc8Qv4Eu0"; // Replace with a secure method to store your key
// Fetch trending videos from YouTube
const fetchVideos = async (cCode) => {
loading.value = true;
error.value = null;
try {
const response = await axios.get("https://www.googleapis.com/youtube/v3/videos", {
params: {
part: "snippet,statistics",
chart: "mostPopular",
maxResults: 20,
regionCode: cCode,
videoCategoryId: category.value,
key: apiKey
}
});
console.log(response.data.items);
expandedPosts.value = {};
videos.value = response.data.items;
} catch (err) {
console.error("Error fetching videos:", err);
error.value = "Failed to load videos.";
} finally {
loading.value = false;
}
};
// // Watch for country selection changes
watch(countryCode, (newCode) => {
fetchVideos(newCode);
}, { immediate: true }); // Run immediately on mount
//fetchVideos(countryCode.value);
const toggleExpand = (postId) => {
expandedPosts.value[postId] = !expandedPosts.value[postId];
};
</script>
{{ error }}
{{ video.snippet.title }}
now also adjust category
### **Fix: Watch `category` Along with `countryCode`**
Modify your `watch` function to track changes in both `countryCode` and `category`.
### **Updated Code**
<script setup>
import { ref, watch } from 'vue';
import axios from "axios";
import IconLoading from './icons/IconLoading.vue';
import TwoColumn from '@/layouts/TwoColumn.vue';
const expandedPosts = ref({});
const sources = ref([
{ label: "India", value: "IN" },
{ label: "United States", value: "US" },
{ label: "United Kingdom", value: "GB" },
{ label: "France", value: "FR" }
]);
const videosCategories = ref([
{ label: "Sports", value: "17" },
{ label: "Travel & Events", value: "19" },
{ label: "Science & Technology", value: "28" },
{ label: "Music", value: "10" },
{ label: "Education", value: "27" },
]);
// Default selections
const countryCode = ref(sources.value[0].value);
const category = ref(videosCategories.value[0].value);
const videos = ref([]);
const loading = ref(false);
const error = ref(null);
const apiKey = "YOUR_API_KEY"; // Use a secure method to store your API key
// Fetch trending videos from YouTube
const fetchVideos = async () => {
loading.value = true;
error.value = null;
try {
const response = await axios.get("https://www.googleapis.com/youtube/v3/videos", {
params: {
part: "snippet,statistics",
chart: "mostPopular",
maxResults: 20,
regionCode: countryCode.value,
videoCategoryId: category.value,
key: apiKey
}
});
expandedPosts.value = {};
videos.value = response.data.items;
} catch (err) {
console.error("Error fetching videos:", err);
error.value = "Failed to load videos.";
} finally {
loading.value = false;
}
};
// Watch for both country and category changes
watch([countryCode, category], () => {
fetchVideos();
}, { immediate: true });
const toggleExpand = (postId) => {
expandedPosts.value[postId] = !expandedPosts.value[postId];
};
</script>
<template>
<TwoColumn md1=3 md2=9>
<template #content1>
<v-radio-group v-model="countryCode">
<v-radio v-for="source in sources" :key="source.value" :label="source.label" :value="source.value"></v-radio>
</v-radio-group>
<v-radio-group v-model="category">
<v-radio v-for="cat in videosCategories" :key="cat.value" :label="cat.label" :value="cat.value"></v-radio>
</v-radio-group>
</template>
<template #content2>
<IconLoading :loading="loading" />
<p v-if="error">{{ error }}</p>
<v-row>
<v-col cols="12" sm="6" md="4" v-for="video in videos" :key="video.id">
<v-card>
<a :href="`https://www.youtube.com/watch?v=${video.id}`" target="_blank" rel="noopener noreferrer" style="text-decoration: none; color: inherit;">
<v-img height="200px" :src="video.snippet.thumbnails.high.url" cover></v-img>
</a>
<v-card-title>{{ video.snippet.title }}</v-card-title>
<v-card-actions>
<v-btn color="orange-lighten-2" text="Explore"></v-btn>
<v-spacer></v-spacer>
<v-btn :icon="expandedPosts[video.id] ? 'mdi-chevron-up' : 'mdi-chevron-down'" @click="toggleExpand(video.id)"></v-btn>
</v-card-actions>
<v-expand-transition>
<div v-show="expandedPosts[video.id]">
<v-divider></v-divider>
<v-card-text v-html="video.snippet.description"></v-card-text>
</div>
</v-expand-transition>
</v-card>
</v-col>
</v-row>
</template>
</TwoColumn>
</template>
—
### **Key Changes & Fixes**
โ
**Now watching `category` and `countryCode` together** โ When either changes, `fetchVideos` is called.
โ
**Fixed `v-for` scope in category selection** โ Changed `category` variable inside `v-for` to `cat` to avoid conflicts.
โ
**`fetchVideos` is executed immediately on mount** using `{ immediate: true }`.
Now, whenever the user changes the country or category, the videos update dynamically. ๐
<script>
import { ref, watch } from 'vue';
import axios from "axios";
import IconLoading from './icons/IconLoading.vue';
import TwoColumn from '@/layouts/TwoColumn.vue';
const expandedPosts = ref({});
const sources = ref([
{ label: "India", value: "IN" },
{ label: "United States", value: "US" },
{ label: "United Kingdom", value: "GB" },
{ label: "France", value: "FR" }
]);
const videosCategories = ref([
{label: "Sports", value: 17},
{label: "Travel & Events", value: 19},
{label:'Science & Technology', value: 28},
{label:'Music', value: 10},
{label:'Education', value: 27},
]); // Default country selection
const countryCode = ref(sources.value[0].value); // Default country selection
const category = ref(videosCategories.value[0].value); // Default category selection
console.log(category.value);
const videos = ref([]);
const loading = ref(true);
const error = ref(null);
const apiKey = "AIzaSyAp2pUhmJYqGIiv91p50Ss1Vjhc8Qv4Eu0"; // Replace with a secure method to store your key
// Fetch trending videos from YouTube
const fetchVideos = async () => {
loading.value = true;
error.value = null;
try {
const response = await axios.get("https://www.googleapis.com/youtube/v3/videos", {
params: {
part: "snippet,statistics",
chart: "mostPopular",
maxResults: 20,
regionCode: countryCode.value,
videoCategoryId: category.value,
key: apiKey
}
});
console.log(response.data.items);
expandedPosts.value = {};
videos.value = response.data.items;
} catch (err) {
console.error("Error fetching videos:", err);
error.value = "Failed to load videos.";
} finally {
loading.value = false;
}
};
// // Watch for country selection changes
watch([countryCode, category], () => {
fetchVideos();
}, { immediate: true });
//fetchVideos(countryCode.value);
const toggleExpand = (postId) => {
expandedPosts.value[postId] = !expandedPosts.value[postId];
};
</script>
{{ error }}
{{ video.snippet.title }}
now I want to run video category api first based on default country then based on first category show videos
### Steps:
1. **Fetch video categories** from YouTube API based on the selected country.
2. **Set the first category** from the API response.
3. **Fetch videos** based on the default country and first category.
### Updated Code:
<script setup>
import { ref, watch } from 'vue';
import axios from "axios";
import IconLoading from './icons/IconLoading.vue';
import TwoColumn from '@/layouts/TwoColumn.vue';
const expandedPosts = ref({});
const sources = ref([
{ label: "India", value: "IN" },
{ label: "United States", value: "US" },
{ label: "United Kingdom", value: "GB" },
{ label: "France", value: "FR" }
]);
const videosCategories = ref([]);
const countryCode = ref(sources.value[0].value); // Default country selection
const category = ref(null); // Will be set dynamically
const videos = ref([]);
const loading = ref(true);
const error = ref(null);
const apiKey = "YOUR_YOUTUBE_API_KEY"; // Replace with a secure method to store your key
// Fetch video categories from YouTube API
const fetchCategories = async () => {
try {
const response = await axios.get("https://www.googleapis.com/youtube/v3/videoCategories", {
params: {
part: "snippet",
regionCode: countryCode.value,
key: apiKey
}
});
if (response.data.items.length > 0) {
videosCategories.value = response.data.items.map(cat => ({
label: cat.snippet.title,
value: cat.id
}));
// Set the first category dynamically
category.value = videosCategories.value[0].value;
// Fetch videos based on the first category
fetchVideos();
}
} catch (err) {
console.error("Error fetching categories:", err);
error.value = "Failed to load categories.";
}
};
// Fetch trending videos from YouTube
const fetchVideos = async () => {
if (!category.value) return; // Ensure category is set before fetching
loading.value = true;
error.value = null;
try {
const response = await axios.get("https://www.googleapis.com/youtube/v3/videos", {
params: {
part: "snippet,statistics",
chart: "mostPopular",
maxResults: 20,
regionCode: countryCode.value,
videoCategoryId: category.value,
key: apiKey
}
});
expandedPosts.value = {};
videos.value = response.data.items;
} catch (err) {
console.error("Error fetching videos:", err);
error.value = "Failed to load videos.";
} finally {
loading.value = false;
}
};
// Watch for country selection changes and update categories
watch(countryCode, () => {
fetchCategories();
}, { immediate: true });
// Watch for category selection changes and fetch videos
watch(category, () => {
fetchVideos();
});
const toggleExpand = (postId) => {
expandedPosts.value[postId] = !expandedPosts.value[postId];
};
</script>
<template>
<TwoColumn md1=3 md2=9>
<template #content1>
<v-radio-group v-model="countryCode">
<v-radio v-for="source in sources" :key="source.value" :label="source.label" :value="source.value"></v-radio>
</v-radio-group>
<v-radio-group v-model="category" v-if="videosCategories.length">
<v-radio v-for="category in videosCategories" :key="category.value" :label="category.label" :value="category.value"></v-radio>
</v-radio-group>
</template>
<template #content2>
<IconLoading :loading="loading" />
<p v-if="error">{{ error }}</p>
<v-row>
<v-col cols="12" sm="6" md="4" v-for="video in videos" :key="video.id">
<v-card>
<a :href="`https://www.youtube.com/watch?v=${video.id}`" target="_blank" rel="noopener noreferrer" style="text-decoration: none; color: inherit;">
<v-img height="200px" :src="video.snippet.thumbnails.high.url" cover></v-img>
</a>
<v-card-title>
{{ video.snippet.title }}
</v-card-title>
<v-card-actions>
<v-btn color="orange-lighten-2" text="Explore"></v-btn>
<v-spacer></v-spacer>
<v-btn
:icon="expandedPosts[video.id] ? 'mdi-chevron-up' : 'mdi-chevron-down'"
@click="toggleExpand(video.id)"
></v-btn>
</v-card-actions>
<v-expand-transition>
<div v-show="expandedPosts[video.id]">
<v-divider></v-divider>
<v-card-text v-html="video.snippet.description"></v-card-text>
</div>
</v-expand-transition>
</v-card>
</v-col>
</v-row>
</template>
</TwoColumn>
</template>
### Key Updates:
1. **Fetch categories dynamically** when the country changes.
2. **Set the first category as default** and fetch videos accordingly.
3. **Watch for category changes** and update videos when a new category is selected.
This will ensure:
– When the user selects a country, categories load dynamically.
– The first available category is selected automatically.
– Videos are fetched based on the selected category.
Let me know if you need any further improvements! ๐
### **1. Basic Charts**
– **Bar Chart** ๐ โ Used for comparing categories.
– **Column Chart** ๐ โ Similar to a bar chart but with vertical bars.
– **Line Chart** ๐ โ Shows trends over time.
– **Area Chart** ๐ โ A filled version of the line chart, useful for cumulative trends.
– **Pie Chart** ๐ฅง โ Represents parts of a whole.
– **Doughnut Chart** ๐ฉ โ A variation of a pie chart with a hole in the middle.
### **2. Statistical & Distribution Charts**
– **Histogram** ๐ โ Shows frequency distribution of numerical data.
– **Box Plot (Box-and-Whisker Plot)** ๐ฆ โ Displays data spread and outliers.
– **Scatter Plot** ๐ด โ Shows relationships between two variables.
– **Bubble Chart** ๐ต โ Similar
—
### **1. Fetch Trending Videos (`videos` endpoint)**
**URL:**
`https://www.googleapis.com/youtube/v3/videos`
**Common Parameters:**
| Parameter | Description |
|—————-|————|
| `part` | Specifies the details to retrieve (e.g., `snippet`, `statistics`, `contentDetails`). |
| `chart` | Set to `"mostPopular"` for trending videos. |
| `regionCode` | Specifies the country (e.g., `IN` for India, `US` for the United States). |
| `videoCategoryId` | Filters videos by category (e.g., `10` for Music). |
| `maxResults` | Number of results per request (default: `5`, max: `50`). |
| `key` | Your API key. |
**Example Request:**
https://www.googleapis.com/youtube/v3/videos?part=snippet,statistics&chart=mostPopular®ionCode=US&videoCategoryId=10&maxResults=10&key=YOUR_API_KEY
—
### **2. Get Video Categories (`videoCategories` endpoint)**
**URL:**
`https://www.googleapis.com/youtube/v3/videoCategories`
**Common Parameters:**
| Parameter | Description |
|—————-|————|
| `part` | Should be `"snippet"` to get category details. |
| `regionCode` | Filters categories by country (e.g., `IN`, `US`). |
| `key` | Your API key. |
**Example Request:**
https://www.googleapis.com/youtube/v3/videoCategories?part=snippet®ionCode=US&key=YOUR_API_KEY
—
### **3. Search Videos (`search` endpoint)**
**URL:**
`https://www.googleapis.com/youtube/v3/search`
**Common Parameters:**
| Parameter | Description |
|—————-|————|
| `part` | `"snippet"` (Required). |
| `q` | Search query (e.g., `"technology news"`). |
| `type` | Filter results by type (`video`, `channel`, `playlist`). |
| `maxResults` | Number of results (default `5`, max `50`). |
| `regionCode` | Filters by country. |
| `videoCategoryId` | Filters by category. |
| `order` | Sorting order (`date`, `rating`, `relevance`, `title`, `videoCount`, `viewCount`). |
| `key` | Your API key. |
**Example Request:**
https://www.googleapis.com/youtube/v3/search?part=snippet&q=technology&type=video&maxResults=10®ionCode=IN&key=YOUR_API_KEY
—
### **4. Get Video Details (`videos` endpoint)**
**URL:**
`https://www.googleapis.com/youtube/v3/videos`
**Common Parameters:**
| Parameter | Description |
|—————-|————|
| `part` | `"snippet,contentDetails,statistics"` for full details. |
| `id` | The video ID (`e.g., "dQw4w9WgXcQ"`). |
| `key` | Your API key. |
**Example Request:**
https://www.googleapis.com/youtube/v3/videos?part=snippet,statistics,contentDetails&id=dQw4w9WgXcQ&key=YOUR_API_KEY
—
### **5. Get Playlist Videos (`playlistItems` endpoint)**
**URL:**
`https://www.googleapis.com/youtube/v3/playlistItems`
**Common Parameters:**
| Parameter | Description |
|—————-|————|
| `part` | `"snippet,contentDetails"`. |
| `playlistId` | ID of the playlist. |
| `maxResults` | Number of results per request. |
| `key` | Your API key. |
**Example Request:**
https://www.googleapis.com/youtube/v3/playlistItems?part=snippet,contentDetails&playlistId=PLFgquLnL59amN1EnoVnoUjI9AS9LqYKiP&maxResults=10&key=YOUR_API_KEY
—
Would you like me to integrate this into your Vue app logic? ๐
### **Available Chart Values**
| Chart Value | Description |
|—————–|————|
| `mostPopular` | Returns the most popular videos in the specified `regionCode` and `videoCategoryId`. |
**Example Request Using `chart=mostPopular`**
https://www.googleapis.com/youtube/v3/videos?part=snippet,statistics&chart=mostPopular®ionCode=US&videoCategoryId=10&maxResults=10&key=YOUR_API_KEY
### **Alternative Sorting Without `chart`**
If you're looking for **different ways to sort videos**, you can use the `order` parameter in the `search` endpoint instead:
| Order Value | Description |
|——————|————|
| `date` | Newest videos first. |
| `rating` | Highest-rated videos first. |
| `relevance` | Most relevant videos based on query. *(Default for `search` endpoint)* |
| `title` | Alphabetical order. |
| `videoCount` | Channels sorted by video count. |
| `viewCount` | Most viewed videos first. |
**Example Request Using `order=viewCount`**
https://www.googleapis.com/youtube/v3/search?part=snippet&q=technology&type=video&maxResults=10&order=viewCount®ionCode=IN&key=YOUR_API_KEY
### **Summary**
– The `chart` parameter only supports `mostPopular`.
– To sort videos in different ways, use the `order` parameter in the `search` endpoint.
Let me know if you need a Vue.js implementation with these filters! ๐
params: {
part: "snippet,statistics",
order: "viewCount",
maxResults: 20,
regionCode: countryCode.value,
videoCategoryId: category.value,
key: apiKey
}
above not working
### **Fix: Use `/search` Instead**
Since `chart=mostPopular` is the **only** valid sorting method for `/videos`, if you need to sort videos by `viewCount`, you **must** use the `/search` endpoint.
### **Updated Code (Fixing `order` Issue)**
Replace your API call with this:
const response = await axios.get("https://www.googleapis.com/youtube/v3/search", {
params: {
part: "snippet",
order: "viewCount", // Sort by most viewed videos
maxResults: 20,
regionCode: countryCode.value,
videoCategoryId: category.value, // Doesn't work in search, use query instead
type: "video", // Ensures only videos are fetched
key: apiKey
}
});
### **Important Notes:**
1. **`order=viewCount` only works with the `/search` endpoint.**
– The `/videos` endpoint does **not** support `order=viewCount`.
– `/videos` only allows `chart=mostPopular`.
2. **`videoCategoryId` is NOT supported in `/search`.**
– The `/search` endpoint does **not** have a `videoCategoryId` filter.
– Instead, you must use a keyword-based `q` (query) filter.
**Example Fix:** If you want videos from the "Technology" category, use:
q: "technology"
3. **YouTube recommends using `/videos` for trending videos** and `/search` for custom sorting.
Would you like me to adjust your Vue.js implementation for this? ๐
### **Updated Code:**
<v-expansion-panels>
<v-expansion-panel>
<v-expansion-panel-title>Country</v-expansion-panel-title>
<v-expansion-panel-text>
<v-radio-group v-model="countryCode">
<v-radio
v-for="source in sources"
:key="source.value"
:label="source.label"
:value="source.value"
></v-radio>
</v-radio-group>
</v-expansion-panel-text>
</v-expansion-panel>
</v-expansion-panels>
### **Explanation:**
– `v-expansion-panel-title` โ Sets the title of the panel.
– `v-expansion-panel-text` โ Contains the radio button group inside the expandable section.
– `v-radio-group` โ Binds to `countryCode` and updates it when a user selects an option.
This will allow users to expand the panel, view, and select a country via radio buttons inside it. Let me know if you need modifications! ๐
### **Solution 1: Using `mb-4` for Margin**
<v-expansion-panels>
<v-expansion-panel class="mb-4">
<v-expansion-panel-title>Country</v-expansion-panel-title>
<v-expansion-panel-text>
<v-radio-group v-model="countryCode">
<v-radio v-for="source in sources" :key="source.value" :label="source.label" :value="source.value"></v-radio>
</v-radio-group>
</v-expansion-panel-text>
</v-expansion-panel>
<v-expansion-panel>
<v-expansion-panel-title>Category</v-expansion-panel-title>
<v-expansion-panel-text>
<v-radio-group v-model="category">
<v-radio v-for="category in videosCategories" :key="category.value" :label="category.label" :value="category.value"></v-radio>
</v-radio-group>
</v-expansion-panel-text>
</v-expansion-panel>
</v-expansion-panels>
โ This adds a **margin-bottom (gap) between expansion panels**.
—
### **Solution 2: Using `v-container` and `v-row`**
<v-container>
<v-row>
<v-col cols="12">
<v-expansion-panels>
<v-expansion-panel>
<v-expansion-panel-title>Country</v-expansion-panel-title>
<v-expansion-panel-text>
<v-radio-group v-model="countryCode">
<v-radio v-for="source in sources" :key="source.value" :label="source.label" :value="source.value"></v-radio>
</v-radio-group>
</v-expansion-panel-text>
</v-expansion-panel>
</v-expansion-panels>
</v-col>
<v-col cols="12" class="mt-4"> <!-- Adds margin-top for spacing -->
<v-expansion-panels>
<v-expansion-panel>
<v-expansion-panel-title>Category</v-expansion-panel-title>
<v-expansion-panel-text>
<v-radio-group v-model="category">
<v-radio v-for="category in videosCategories" :key="category.value" :label="category.label" :value="category.value"></v-radio>
</v-radio-group>
</v-expansion-panel-text>
</v-expansion-panel>
</v-expansion-panels>
</v-col>
</v-row>
</v-container>
โ This method adds more **flexible spacing control** using `v-col` and `class="mt-4"` for a larger gap.
—
Let me know which works best for your UI! ๐
—
### **Stepper with Radio Buttons in Vuetify**
<template>
<v-container>
<v-stepper v-model="step">
<!-- Step 1: Select Country -->
<v-stepper-header>
<v-stepper-item step="1" :complete="step > 1"> Select Country </v-stepper-item>
<v-divider></v-divider>
<v-stepper-item step="2" :complete="step > 2"> Select Category </v-stepper-item>
</v-stepper-header>
<v-stepper-window>
<!-- Step 1 -->
<v-stepper-window-item value="1">
<v-card>
<v-card-title>Select Country</v-card-title>
<v-card-text>
<v-radio-group v-model="countryCode">
<v-radio v-for="source in sources" :key="source.value" :label="source.label" :value="source.value"></v-radio>
</v-radio-group>
</v-card-text>
<v-card-actions>
<v-btn color="primary" @click="step = 2">Next</v-btn>
</v-card-actions>
</v-card>
</v-stepper-window-item>
<!-- Step 2 -->
<v-stepper-window-item value="2">
<v-card>
<v-card-title>Select Category</v-card-title>
<v-card-text>
<v-radio-group v-model="category">
<v-radio v-for="category in videosCategories" :key="category.value" :label="category.label" :value="category.value"></v-radio>
</v-radio-group>
</v-card-text>
<v-card-actions>
<v-btn color="primary" @click="fetchVideos">Fetch Videos</v-btn>
<v-btn variant="text" @click="step = 1">Back</v-btn>
</v-card-actions>
</v-card>
</v-stepper-window-item>
</v-stepper-window>
</v-stepper>
</v-container>
</template>
<script setup>
import { ref } from 'vue';
const step = ref(1);
const sources = ref([
{ label: "India", value: "IN" },
{ label: "United States", value: "US" },
{ label: "United Kingdom", value: "GB" },
{ label: "France", value: "FR" }
]);
const videosCategories = ref([
{ label: "Sports", value: 17 },
{ label: "Travel & Events", value: 19 },
{ label: "Science & Technology", value: 28 },
{ label: "Music", value: 10 },
{ label: "Education", value: 27 },
]);
const countryCode = ref(sources.value[0].value);
const category = ref(videosCategories.value[0].value);
const fetchVideos = () => {
console.log("Fetching videos for:", countryCode.value, category.value);
};
</script>
—
### **How This Works**
1. The stepper starts at **Step 1 (Select Country)**.
2. When the user selects a country and clicks **Next**, it moves to **Step 2 (Select Category)**.
3. After selecting a category, clicking **"Fetch Videos"** will trigger the `fetchVideos` function.
4. The **Back** button allows users to return to the previous step.
โ This approach ensures a guided, user-friendly flow for selecting YouTube video filters. Let me know if you need further modifications! ๐