Guides in this series:
Welcome to my series on using the YouTube API with PHP.
In this first part of the series, we’ll cover the following setup steps:
In subsequent videos, we’ll put this setup to work and show examples of working with the YouTube API covering topics such as:
To get started, visit the Google Cloud console at https://console.cloud.google.com/welcome. Click the dropdown on the top to see your list of projects.
From the screen that appears, click NEW PROJECT.

Give your project a name and click CREATE.

Next, under API & Services, locate your Credentials (https://console.cloud.google.com/apis/credentials). Once here, click CREATE CREDENTIALS and choose API KEY.
Copy the key it generates for you.

Because in our first examples we’ll just be reading data (no updates) from the API, we only need an API Key. Later, when we look at updating data, we’ll need to set up OAuth credentials.
Also under API & Services, locate Enabled APIs & Services, then click + ENABLE APIS AND SERVICES.
On the page that appears, search YouTube, choose YouTube Data API v3 and then click Enable.
Switching gears to the code, we need to pull in the package google-api-php-client which will give us the utilities we need to connect to the YouTube API. This can be done via the following Composer command run in the root of your project:
> composer require google/apiclient
In my example, I’ll also pull in the package symfony/var-dumper so I can easily output data we’ll get back from YouTube during development using a dump function. If your code base already has a dump/debugging output utility you can skip this step.
> composer require symfony/var-dumper
Finally, I’ll set up a file with the following code which will utilize the google-api-php-client package to initialize an API connection with YouTube. Be sure to update this code replacing YOUR_API_KEY with the API key you retrieved in the above steps.
<?php
# You can omit requiring Composer’s autoload file if you're working in a framework (e.g. Laravel) that sets up autoloading for you
require_once 'vendor/autoload.php';
use Google\Client;
use Google\Service\YouTube;
# Configs
$apiKey = 'YOUR_API_KEY';
# Initialize YouTube API client
$client = new Client();
$client->setDeveloperKey($apiKey);
$service = new YouTube($client);
# Example query just to make sure we can connect to the API
$response = $service->videos->listVideos('snippet', ['id' => 'fG08dcJ8xFE']);
# Output the response to confirm it worked
dump($response);
# If you don’t have access to a dump function, replace the above `dump` with PHP’s built-in `var_dump` function
If your connection to the YouTube API is successful, you should see output like this:
If the connection fails, you’ll see an error message indicating what the problem is.
Now that we've set up and demonstrated we can connect to the YouTube API, we can start to put it to work. In the next video in this series we'll take a closer look at the code we wrote above to retrieve the details on a single video and learn how to utilize the YouTube API docs.
No subscriptions, no auto-renewals.
Just a simple one-time payment that helps support my free, to-the-point videos without sponsered ads.
Unlocking gets you access to the notes for this video plus all 200+ guides on this site.
Your support is appreciated. Thank you!