Skip to content

Everything for WordPress, web development — and beyond

🔍 Searching for videos on YouTube using Data API v3: a complete guide

🔍 Searching for videos on YouTube using Data API v3: a complete guide

Your site needs a YouTube video feed, but there is no ready-made plugin for the task, and you are already imagining a week of wrestling with OAuth, tokens, and clunky libraries. YouTube Data API v3 handles this job in about an hour. No OAuth, no user tokens, no external dependencies. Just an API key and a properly assembled URL.

The search endpoint returns videos, channels, and playlists by keywords. It filters by date, duration, and channel. It sorts by relevance or view count. In practice, this covers most scenarios, from a specific channel's video feed to a "wordpress tutorial" compilation from the past month.

Below is a step-by-step walkthrough with live request examples and PHP code. By the end, you will have a working search module: structured JSON in, video gallery out, zero third-party libraries.

💡 Quick overview:

  • Create a project in the Google Cloud console, enable YouTube Data API v3, and get an API key.
  • Build the search request URL: endpoint /youtube/v3/search, required parameters part=snippet, key, and q (since June 2025 q is required; without it the API returns an empty array).
  • Add filters: type (video/channel/playlist), channelId, publishedAfter/publishedBefore, maxResults, order, videoDuration, videoEmbeddable.
  • Execute the request via cURL in PHP, parse the JSON response, display results, and set up caching through WordPress transients.

Step 1: Getting your API key

No key, no API request goes out. The key is tied to a project in the Google Cloud Console and identifies your application; nothing more complicated than signing into a Google account is required.

Here is what you do:

  • Open the Google Cloud Console and create a new project or select an existing one.
  • Go to APIs & Services → Library, search for "YouTube Data API v3," and click Enable.
  • Open APIs & Services → Credentials, click Create Credentials → API Key.
  • Copy the key. Immediately set a restriction: Restrict Key → YouTube Data API v3, so the key cannot be used for other Google services.

The key looks like a string such as AIzaSyD-... and is passed in every request via the key parameter. For public video search, this is enough; OAuth is not needed. However, if you plan requests on behalf of a user (video upload, playlist management), you will need OAuth 2.0.

Quick test: send a GET request to the search endpoint with any search term. If you receive JSON with an items array, the key works.

Step 2: Search request URL and required parameters

Endpoint for search:

1GET https://www.googleapis.com/youtube/v3/search

Two parameters are always required:

Parameter

Purpose

part

Which resource properties to include in the response. For search you need snippet. The snippet contains title, description, thumbnails, and channelTitle.

key

Your API key.

q

The search query. Accepts free text, just like the search bar on youtube.com.

The q parameter accepts free text, just like the search bar on youtube.com. You can pass multiple words separated by spaces or plus signs: q=wordpress+speed+optimization. Case does not matter.

Minimal working request:

1https://www.googleapis.com/youtube/v3/search?part=snippet&q=wordpress&key=YOUR_KEY

It will return 5 results (the default), which is what the API returns when maxResults is not overridden.

The type parameter narrows the resource type being searched:

  • video, videos only;
  • channel, channels only;
  • playlist, playlists only.

You can list multiple values separated by commas: type=video,channel. If omitted, the API searches everything. The remaining parameters are optional, but they turn a raw query into a precise tool.

Step 3: Searching for videos by keywords

A classic scenario: a user enters a search phrase on your site, you send it to the YouTube API, and display a selection of videos.

Example request for searching videos with the word "swimming":

1https://www.googleapis.com/youtube/v3/search?part=snippet&q=swimming&type=video&key=YOUR_KEY

The response comes in JSON. Inside items[] is an array of found resources. For each video you have:

  • id.videoId, the unique identifier (plugged into https://www.youtube.com/watch?v=...);
  • snippet.title, the video title;
  • snippet.description, the description;
  • snippet.thumbnails, previews in several resolutions (default, medium, high);
  • snippet.channelTitle, the channel name.

In PHP the easiest way to fetch the response is via file_get_contents. Here is minimal processing code:

1$apiKey = 'YOUR_KEY';
2$query = 'swimming';
3$url = "https://www.googleapis.com/youtube/v3/search?part=snippet&q={$query}&type=video&key={$apiKey}";
4
5$response = file_get_contents($url);
6$data = json_decode($response, true);
7
8foreach ($data['items'] as $item) {
9 echo '<h3>' . htmlspecialchars($item['snippet']['title']) . '</h3>';
10 echo '<p>' . htmlspecialchars($item['snippet']['description']) . '</p>';
11 echo '<img src="' . $item['snippet']['thumbnails']['medium']['url'] . '" alt="">';
12 echo '<a href="https://www.youtube.com/watch?v=' . $item['id']['videoId'] . '">Watch</a>';
13}

file_get_contents is fine for tests and small projects. In production, use cURL: it gives you control over timeouts, headers, and error handling. And yes, if allow_url_fopen is disabled on your hosting, file_get_contents will not work for external URLs. In that case, cURL is your only option.

Step 4: Fetching videos from a specific channel

To collect all videos from a channel, pass the channelId parameter. Finding the channel identifier is easy: open the channel page on YouTube and copy the value after /channel/ from the address bar (for example UC3VyA8KN_VgCF93EurnAQXw).

Request:

1https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=UC3VyA8KN_VgCF93EurnAQXw&type=video&order=date&key=YOUR_KEY

The parameter order=date sorts by publication date, newest first. Without it, the API returns the most relevant results, but for a channel feed chronological order makes more sense.

The same request in PHP with cURL and basic error handling:

1$apiKey = 'YOUR_KEY';
2$channelId = 'UC3VyA8KN_VgCF93EurnAQXw';
3$url = "https://www.googleapis.com/youtube/v3/search?part=snippet&channelId={$channelId}&type=video&order=date&key={$apiKey}";
4
5$ch = curl_init();
6curl_setopt_array($ch, [
7 CURLOPT_URL => $url,
8 CURLOPT_RETURNTRANSFER => true,
9 CURLOPT_TIMEOUT => 15,
10 CURLOPT_SSL_VERIFYPEER => true,
11]);
12
13$response = curl_exec($ch);
14$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
15curl_close($ch);
16
17if ($httpCode !== 200) {
18 echo 'Request error. Code: ' . $httpCode;
19 exit;
20}
21
22$data = json_decode($response, true);
23
24foreach ($data['items'] as $item) {
25 printf(
26 '<div><a href="https://www.youtube.com/watch?v=%s">%s</a></div>',
27 $item['id']['videoId'],
28 htmlspecialchars($item['snippet']['title'])
29 );
30}

A channel can have hundreds of videos, but a single request returns no more than 50. To fetch everything, use the pageToken field from the API response: pass its value in the next request as pageToken=<token>. Repeat until nextPageToken is empty.

Step 5: Limits, sorting, and quotas

On a real site you rarely need "all videos for a query." More often you want the top 10 from the past month or a selection that excludes Shorts. For this, the API provides five fine-tuning parameters.

maxResults: how many items to return. Valid values: 1 to 50. Default is 5. For a gallery or feed, set 20-30, but keep quotas in mind.

order: sort field:

Value

What it does

relevance

By relevance to the query (default)

date

Newest first

rating

By rating (likes/dislikes)

viewCount

By number of views

title

Alphabetically by title

publishedAfter** / **publishedBefore: filter by publication date. Format: ISO 8601 (RFC 3339). Example: publishedAfter=2026-01-01T00:00:00Z. Cuts off archived videos when you only need fresh content.

videoDuration: filter by length:

  • short, under 4 minutes;
  • medium, from 4 to 20 minutes;
  • long, over 20 minutes;
  • not specified, all.

videoEmbeddable with a value of true selects only videos allowed for embedding on third-party sites. For a site that embeds the YouTube player, this is a required parameter.

Combined request: 10 embeddable videos about WordPress, no older than 2026, longer than 4 minutes, sorted by view count:

1https://www.googleapis.com/youtube/v3/search?part=snippet&q=wordpress+tutorial&type=video&videoEmbeddable=true&videoDuration=medium&publishedAfter=2026-01-01T00:00:00Z&maxResults=10&order=viewCount&key=YOUR_KEY

Quotas: how to avoid a blank screen

Each search.list call costs 100 units of your daily quota. By default, a Google Cloud project gets 10,000 units per day for free, roughly 100 search calls. For a site with more than a hundred visitors per day, that is not enough.

The solution is to cache API responses. In WordPress, transients work perfectly for this:

1$cache_key = 'yt_search_' . md5($query);
2$videos = get_transient($cache_key);
3
4if ($videos === false) {
5 $response = wp_remote_get($url);
6 $body = json_decode(wp_remote_retrieve_body($response), true);
7 $videos = $body['items'] ?? [];
8
9 set_transient($cache_key, $videos, 6 * HOUR_IN_SECONDS);
10}
11
12// Display $videos in a gallery...

A six-hour transient means a maximum of 4 API requests per day for a single search phrase. Even with ten different queries across site pages, you stay under 40 calls, less than half the daily quota. If you need more, request a quota increase through Google Cloud Console: Quotas → YouTube Data API v3 → Edit.

The live example above shows how to build a channel video gallery via Data API v3 in PHP in 15 minutes.

⁉️🤔 Frequently asked questions

Is OAuth needed to search for videos through the YouTube API?

No. For search requests to /youtube/v3/search, an API key is enough. OAuth is required only for user-level operations: uploading videos, managing playlists, subscriptions. An API key is obtained in the Google Cloud Console in a couple of minutes and used immediately.

How many requests per day can be made for free?

Each Google Cloud project gets 10,000 quota units per day for free. One search.list call costs 100 units, so roughly 100 search requests per day. With caching via WordPress transients, this is enough for an average site. If you need more, request a quota increase through Google Cloud Console (Quotas section).

How do I get more than 50 results from a single query?

Through pagination. In the JSON response, the API returns a nextPageToken field. Pass its value as the pageToken parameter in the next request to get the next page. Repeat until nextPageToken is empty. A full pass through a channel with 500 videos takes 10 requests and 1,000 quota units.

Can I search for videos in languages other than English?

Yes. The q parameter accepts any language, including Russian. YouTube's search algorithm determines language relevance automatically. To force narrowing, add the relevanceLanguage=ru parameter; results will be ranked in favor of Russian-language content.

Why does the API return an empty array even though the request is correct?

The most common cause: publishedAfter is set to a future date or to today's date with an exact time. Use the start of the day: T00:00:00Z. The second cause: a combination of filters that excludes all results. For example, videoDuration=long and videoEmbeddable=true together with the channelId of a small channel may yield zero matches. Simplify the request down to part=snippet&q=...&key=... and add filters one at a time, checking results at each step.

Building search in an hour: which tools fit which task

Your choice of tools depends on what exactly you are building. If you need a simple video gallery for a channel on a small business site, grab a ready-made PHP wrapper like madcoda/php-youtube-api: a Composer package, three lines of code, result in five minutes. If you are building custom search with filters, caching, and pagination, write your own handler using cURL plus WordPress transients. For a high-traffic site with tens of thousands of visitors, add a Redis cache layer on top of transients and set up background synchronization via WP-Cron; then pages load in milliseconds, and API quotas are spent only on cache invalidation.

You already have your API key. Caching is configured. All that remains is to assemble the URL for your task and write the output loop. The result is a live video feed that does not depend on third-party services and does not hit quota limits.