How APIs work. Learn how to use the YouTube API like @Tom Scott and @MrBeast to automatically update a video in the background. https://github.com/fireship-io/this-video-has-n-views

Tom Scott video https://youtu.be/BxV14h0kFs0
Mr Beast Video https://youtu.be/YSoJPA8-oHc

Note: The background job is on a 5 minute interval, so it won't be very accurate. 

Fireship OAuth 2.0 Video https://fireship.io/lessons/google-apis-node-tutorial/
2024-02-04T06:11:40.992Z
nova-2
2-general-nova
2024-01-09.29447
30089e05-99d1-4376-b32e-c263170674af
c9b76df9-c07b-4a21-9f98-74e19044631f
514d9788b8410c743c2f2716a05e7cc9f29c59a55bd075a960a72e975d9d82bc
67875a7f-c9c4-48a0-aa55-5bdb8a91c34a
deprecated
Today on How They Do It.
If you watch my YouTube channel, there's a good chance you've already seen Tom Scott's video.
This video has x amount of views.
If not, check it out.
But basically what he does is run a background job that hits the YouTube API to update the title with the latest view count every few minutes.
Then another YouTuber you may have heard of, MrBeast, appears Appears to be doing the same thing in his latest video, where the thumbnail updates every few minutes with the amount of money given away.
If you're wondering how they do this, you might be a little disappointed when Tom Scott says,
I'm not gonna talk about the exact details of my code here.
It is not the important part because code is just incredibly dull on camera.
But I have to disagree there because in my opinion, The code is the most exciting part.
So in today's video, we'll take a deep dive into APIs, and I'll show you exactly how these YouTubers are updating their videos automatically way in the background.
If you're new here, like and subscribe, and you can grab the full source code from GitHub.
My primary mission today is to teach you everything you need to know about API's, guys, which is an essential skill for both front end and back end developers.
Along the way, you'll learn about API authentication and OAuth 2, how to work with Google APIs and Node.
JS, and how to schedule your code to run-in the background with a serverless cloud function.
But first, let's answer the question, what is an API?
An application programming interface is simply a way for 2 pieces of software to talk to each other.
In this demo, we have our application, which will eventually live on a cloud function.
It needs to talk to YouTube to get the view count for a video, and then tell YouTube to update the video with a new title.
To facilitate this, YouTube has a standard set of functions where developers can request the data they On the web, APIs are accessed over HTTP.
Or, in other words, there's a URL on the Internet that connects that server to the rest of the Internet.
The most popular standard for implementing APIs in today's world is REST, which stands for Representational State Transfer.
APIs that implement this standard, like YouTube, are called restful APIs.
The most important concept to understand is that restful APIs are stateless, which means the client and the server Don't need to know anything about each other's internal state.
Instead, they simply pass messages back and forth.
The process starts when the client, your application, makes a request.
An HTTP request message has a very specific format.
The start line contains the URL of the API you're making a request to.
It also contains an HTTP g t p method or verb that defines what you're trying to do on the server.
A git request means you're trying to access data, but not modify it.
Whereas a post request means you're trying to create some new data on the server.
The reason this is important is because the API can design endpoints based on a specific entity.
We can see that in the YouTube API, where it has different entities for comments, videos, playlists, etcetera.
The cool thing about YouTube is that it has an API Explorer, so we can actually look at the raw HTTP message.
For example, to retrieve a list of videos, we point to this video's endpoint as a GET request.
Then, if we wanted to update that video, we'd point to the same URL, but instead make it a PUT request.
So the takeaway here is that the URL defines the resource or entity that lives there, and the verb defines what you want to do to that entity.
Now back in the HTTP message, after the start line, we have headers.
The headers contain additional metadata about the request.
For example, the accept header tells the server which type of content that the client will accept back, which more often than not will be JSON.
The headers also handle things like authorization to determine if the user making the request is authorized to do so.
Then after the headers, we have the body, which is typically in JSON format, And it contains a payload of data that the server can use to update something or create a new record on the server.
Okay.
So now that we have our request message, That message gets sent to the server or API.
It receives the request and then executes whatever code needs to be done to handle it, like reading or writing to a database, for example.
When it's done, it formats an HTTP response message to go back to the client.
It also has a very specific format with a start line that contains the status code.
There are a bunch of different status codes, and they're used to tell the client what happened to their request.
If everything worked, then you should get a 200 status code, which means okay.
Codes at the 200 level mean success.
Codes at the 400 level mean that you did something wrong.
And codes at the 500 level mean the server failed for some reason.
And then below that, we also have a body, which contains the actual content that the server is sending back down to the client, which is often JSON data or HTML to use in a front end application.
And that's really all there is to it.
It's a simple request response exchange of data between 2 pieces of software that live on the Internet.
Now that you know how APIs work, let's build a background job that runs every few minutes to grab the latest view count from YouTube and then update a YouTube video's title.
1st, we need a piece of software to make API calls from.
To handle that, I'll be using a Firebase Cloud function with the Node.
Js runtime.
If you're not a Firebase user already, it's very easy to set up.
Install the Firebase command line tools, run Firebase init, and you now have a serverless environment to start writing your code in.
Now YouTube, of course, is a Google product, which means you need to enable the API from the Google Cloud Platform console.
Most major APIs out there, Google included, provide software developer kits to work with their APIs.
In our Node.
Js project, we can install the Google APIs package with npm.
What the SDK does is simplify the process of making calls to the API.
If you wanted to, you could send raw HTTP requests to Google, and in theory that would work fine, but that would be doing things the hard way.
When using the SDK, we can import Google into our code, and now we have IntelliSense for not just YouTube, but any Google API.
Like, if we take a look at Google Drive, for example, you can see it gives us a variety of different endpoints, and then we can simply call a method, and the Google SDK will format the HTTP request for us.
So that's awesome.
But at this point, we need to schedule a background job to run our code every few minutes.
In Cloud Functions, We can do that by first setting up an export with the name of the function, and then we call functions pubsub schedule and pass in a cron schedule as the argument.
This translates to run every 3 minutes, or in Firebase, we have a more readable option of just writing every 3 minutes.
After that, we'll add a Callback function that will run every 3 minutes after we deploy this function to the cloud.
Now inside this function, we'll create a variable named YouTube.
But in order to use the YouTube API, we need to authenticate.
So let's take a second to talk about API authentication.
There are 2 main types of authentication that you should know about right now.
API keys and OAuth.
When thinking about YouTube or any API for that matter, there are 2 main types of resources, those that are public and those that are private.
For example, a get request to a public video is something that anybody can do.
But if we wanted to modify that video with a put request, We would need to have the proper authorization to do so, like being the video's owner or being an admin at YouTube.
Or as developers, we could get the user's permission using OAuth.
So here's what that boils down to.
If we only need to read public resources, then we can use our YouTube developer API key.
The API key is simply a way for YouTube to identify who is making the request, which is important because it's a free API that has a quota on usage, which means that a given API key can only make so many requests per day.
In the case of YouTube, they assign points to every request and then cap your daily usage at 10,000 points.
Now because we're updating an actual video, we need to go through OAuth 2 in this case.
I'm not going to go through the entire process in this video because it can be somewhat complicated.
But if it's something that you're interested in implementing, I have a video for that on Fireship IO.
Basically, what needs to happen is you need to generate a URL where the user can log in to their YouTube or Google account, then your app will request permission to perform actions on the user's behalf.
These permissions are known as scopes.
When the user says okay, Google will redirect them back to your site with an authorization code.
You then send that code to your server or Cloud Function and use it to create an access token and refresh token.
These values can be saved to a database, so you can make API calls for the user in the future.
When you're ready to make an API call, you can read the tokens from your database and create this OAuth 2 client object, and then you use it as the auth argument.
Now that we're authenticated, we can make an HTTP request to the API using the Google API's SDK.
We call YouTube videos list with the video ID, And then we specify the data that we want back.
In our case, we want the statistics, which will include the view count, and also the snippet, which contains the raw data about the video, like the description, title, and so on.
The API responds back with JSON, which is automatically converted into a JavaScript object for us.
And the actual data about this video is the first element in the items array.
From there, we'll grab the view count, then interpolate it into a string to format the new title.
And now we're ready to update our video.
We'll call YouTube Videos Update, and then we'll define a request body.
Remember, the body is the data that the API will use to make the update.
We'll assign it the new title, and then it also requires a category ID, which we can keep from the original snippet.
And that's all there is to it.
Now we just need to deploy our code to get it running in production.
Open up the command line and run firebase deploy.
This will deploy your code as a serverless function in the cloud.
You'll see your function in the Firebase console.
If your function doesn't seem to be working, you can check out the logs for errors.
I'm gonna go ahead and wrap things up there.
If this video helped you, please like and subscribe.
And if you want access to more advanced content, Consider becoming a pro member at FireShip IO.
Thanks for watching, and I will see you in the next one.

Speaker 0: Today on How They Do It. If you watch my YouTube channel, there's a good chance you've already seen Tom Scott's video. This video has x amount of views. If not, check it out. But basically what he does is run a background job that hits the YouTube API to update the title with the latest view count every few minutes.

Then another YouTuber you may have heard of, MrBeast, appears Appears to be doing the same thing in his latest video, where the thumbnail updates every few minutes with the amount of money given away. If you're wondering how they do this, you might be a little disappointed when Tom Scott says,

Speaker 1: I'm not gonna talk about the exact details of my code here. It is not the important part because code is just incredibly dull on camera.

Speaker 0: But I have to disagree there because in my opinion, The code is the most exciting part. So in today's video, we'll take a deep dive into APIs, and I'll show you exactly how these YouTubers are updating their videos automatically way in the background. If you're new here, like and subscribe, and you can grab the full source code from GitHub. My primary mission today is to teach you everything you need to know about API's, guys, which is an essential skill for both front end and back end developers. Along the way, you'll learn about API authentication and OAuth 2, how to work with Google APIs and Node.

JS, and how to schedule your code to run-in the background with a serverless cloud function. But first, let's answer the question, what is an API? An application programming interface is simply a way for 2 pieces of software to talk to each other. In this demo, we have our application, which will eventually live on a cloud function. It needs to talk to YouTube to get the view count for a video, and then tell YouTube to update the video with a new title.

To facilitate this, YouTube has a standard set of functions where developers can request the data they On the web, APIs are accessed over HTTP. Or, in other words, there's a URL on the Internet that connects that server to the rest of the Internet. The most popular standard for implementing APIs in today's world is REST, which stands for Representational State Transfer. APIs that implement this standard, like YouTube, are called restful APIs. The most important concept to understand is that restful APIs are stateless, which means the client and the server Don't need to know anything about each other's internal state.

Instead, they simply pass messages back and forth. The process starts when the client, your application, makes a request. An HTTP request message has a very specific format. The start line contains the URL of the API you're making a request to. It also contains an HTTP g t p method or verb that defines what you're trying to do on the server.

A git request means you're trying to access data, but not modify it. Whereas a post request means you're trying to create some new data on the server. The reason this is important is because the API can design endpoints based on a specific entity. We can see that in the YouTube API, where it has different entities for comments, videos, playlists, etcetera. The cool thing about YouTube is that it has an API Explorer, so we can actually look at the raw HTTP message.

For example, to retrieve a list of videos, we point to this video's endpoint as a GET request. Then, if we wanted to update that video, we'd point to the same URL, but instead make it a PUT request. So the takeaway here is that the URL defines the resource or entity that lives there, and the verb defines what you want to do to that entity. Now back in the HTTP message, after the start line, we have headers. The headers contain additional metadata about the request.

For example, the accept header tells the server which type of content that the client will accept back, which more often than not will be JSON. The headers also handle things like authorization to determine if the user making the request is authorized to do so. Then after the headers, we have the body, which is typically in JSON format, And it contains a payload of data that the server can use to update something or create a new record on the server. Okay. So now that we have our request message, That message gets sent to the server or API.

It receives the request and then executes whatever code needs to be done to handle it, like reading or writing to a database, for example. When it's done, it formats an HTTP response message to go back to the client. It also has a very specific format with a start line that contains the status code. There are a bunch of different status codes, and they're used to tell the client what happened to their request. If everything worked, then you should get a 200 status code, which means okay.

Codes at the 200 level mean success. Codes at the 400 level mean that you did something wrong. And codes at the 500 level mean the server failed for some reason. And then below that, we also have a body, which contains the actual content that the server is sending back down to the client, which is often JSON data or HTML to use in a front end application. And that's really all there is to it.

It's a simple request response exchange of data between 2 pieces of software that live on the Internet. Now that you know how APIs work, let's build a background job that runs every few minutes to grab the latest view count from YouTube and then update a YouTube video's title. 1st, we need a piece of software to make API calls from. To handle that, I'll be using a Firebase Cloud function with the Node. Js runtime.

If you're not a Firebase user already, it's very easy to set up. Install the Firebase command line tools, run Firebase init, and you now have a serverless environment to start writing your code in. Now YouTube, of course, is a Google product, which means you need to enable the API from the Google Cloud Platform console. Most major APIs out there, Google included, provide software developer kits to work with their APIs. In our Node.

Js project, we can install the Google APIs package with npm. What the SDK does is simplify the process of making calls to the API. If you wanted to, you could send raw HTTP requests to Google, and in theory that would work fine, but that would be doing things the hard way. When using the SDK, we can import Google into our code, and now we have IntelliSense for not just YouTube, but any Google API. Like, if we take a look at Google Drive, for example, you can see it gives us a variety of different endpoints, and then we can simply call a method, and the Google SDK will format the HTTP request for us.

So that's awesome. But at this point, we need to schedule a background job to run our code every few minutes. In Cloud Functions, We can do that by first setting up an export with the name of the function, and then we call functions pubsub schedule and pass in a cron schedule as the argument. This translates to run every 3 minutes, or in Firebase, we have a more readable option of just writing every 3 minutes. After that, we'll add a Callback function that will run every 3 minutes after we deploy this function to the cloud.

Now inside this function, we'll create a variable named YouTube. But in order to use the YouTube API, we need to authenticate. So let's take a second to talk about API authentication. There are 2 main types of authentication that you should know about right now. API keys and OAuth.

When thinking about YouTube or any API for that matter, there are 2 main types of resources, those that are public and those that are private. For example, a get request to a public video is something that anybody can do. But if we wanted to modify that video with a put request, We would need to have the proper authorization to do so, like being the video's owner or being an admin at YouTube. Or as developers, we could get the user's permission using OAuth. So here's what that boils down to.

If we only need to read public resources, then we can use our YouTube developer API key. The API key is simply a way for YouTube to identify who is making the request, which is important because it's a free API that has a quota on usage, which means that a given API key can only make so many requests per day. In the case of YouTube, they assign points to every request and then cap your daily usage at 10,000 points. Now because we're updating an actual video, we need to go through OAuth 2 in this case. I'm not going to go through the entire process in this video because it can be somewhat complicated.

But if it's something that you're interested in implementing, I have a video for that on Fireship IO. Basically, what needs to happen is you need to generate a URL where the user can log in to their YouTube or Google account, then your app will request permission to perform actions on the user's behalf. These permissions are known as scopes. When the user says okay, Google will redirect them back to your site with an authorization code. You then send that code to your server or Cloud Function and use it to create an access token and refresh token.

These values can be saved to a database, so you can make API calls for the user in the future. When you're ready to make an API call, you can read the tokens from your database and create this OAuth 2 client object, and then you use it as the auth argument. Now that we're authenticated, we can make an HTTP request to the API using the Google API's SDK. We call YouTube videos list with the video ID, And then we specify the data that we want back. In our case, we want the statistics, which will include the view count, and also the snippet, which contains the raw data about the video, like the description, title, and so on.

The API responds back with JSON, which is automatically converted into a JavaScript object for us. And the actual data about this video is the first element in the items array. From there, we'll grab the view count, then interpolate it into a string to format the new title. And now we're ready to update our video. We'll call YouTube Videos Update, and then we'll define a request body.

Remember, the body is the data that the API will use to make the update. We'll assign it the new title, and then it also requires a category ID, which we can keep from the original snippet. And that's all there is to it. Now we just need to deploy our code to get it running in production. Open up the command line and run firebase deploy.

This will deploy your code as a serverless function in the cloud. You'll see your function in the Firebase console. If your function doesn't seem to be working, you can check out the logs for errors. I'm gonna go ahead and wrap things up there. If this video helped you, please like and subscribe.

And if you want access to more advanced content, Consider becoming a pro member at FireShip IO. Thanks for watching, and I will see you in the next one.
Today on How They Do It. If you watch my YouTube channel, there's a good chance you've already seen Tom Scott's video. This video has x amount of views. If not, check it out. But basically what he does is run a background job that hits the YouTube API to update the title with the latest view count every few minutes. Then another YouTuber you may have heard of, MrBeast, appears Appears to be doing the same thing in his latest video, where the thumbnail updates every few minutes with the amount of money given away. If you're wondering how they do this, you might be a little disappointed when Tom Scott says, I'm not gonna talk about the exact details of my code here. It is not the important part because code is just incredibly dull on camera. But I have to disagree there because in my opinion, The code is the most exciting part. So in today's video, we'll take a deep dive into APIs, and I'll show you exactly how these YouTubers are updating their videos automatically way in the background. If you're new here, like and subscribe, and you can grab the full source code from GitHub. My primary mission today is to teach you everything you need to know about API's, guys, which is an essential skill for both front end and back end developers.
youtube
video gaming
programming
deep learning
Along the way, you'll learn about API authentication and OAuth 2, how to work with Google APIs and Node. JS, and how to schedule your code to run-in the background with a serverless cloud function.
But first, let's answer the question, what is an API? An application programming interface is simply a way for 2 pieces of software to talk to each other. In this demo, we have our application, which will eventually live on a cloud function. It needs to talk to YouTube to get the view count for a video, and then tell YouTube to update the video with a new title. To facilitate this, YouTube has a standard set of functions where developers can request the data they On the web, APIs are accessed over HTTP. Or, in other words, there's a URL on the Internet that connects that server to the rest of the Internet. The most popular standard for implementing APIs in today's world is REST, which stands for Representational State Transfer.
youtube
software development
video gaming
software
programming
internet
streaming
APIs that implement this standard, like YouTube, are called restful APIs. The most important concept to understand is that restful APIs are stateless, which means the client and the server Don't need to know anything about each other's internal state. Instead, they simply pass messages back and forth. The process starts when the client, your application, makes a request. An HTTP request message has a very specific format. The start line contains the URL of the API you're making a request to. It also contains an HTTP g t p method or verb that defines what you're trying to do on the server. A git request means you're trying to access data, but not modify it. Whereas a post request means you're trying to create some new data on the server.
The reason this is important is because the API can design endpoints based on a specific entity. We can see that in the YouTube API, where it has different entities for comments, videos, playlists, etcetera. The cool thing about YouTube is that it has an API Explorer, so we can actually look at the raw HTTP message. For example, to retrieve a list of videos, we point to this video's endpoint as a GET request. Then, if we wanted to update that video, we'd point to the same URL, but instead make it a PUT request. So the takeaway here is that the URL defines the resource or entity that lives there, and the verb defines what you want to do to that entity. Now back in the HTTP message, after the start line, we have headers.
youtube
streaming
twitter
The headers contain additional metadata about the request. For example, the accept header tells the server which type of content that the client will accept back, which more often than not will be JSON. The headers also handle things like authorization to determine if the user making the request is authorized to do so. Then after the headers, we have the body, which is typically in JSON format, And it contains a payload of data that the server can use to update something or create a new record on the server. Okay. So now that we have our request message, That message gets sent to the server or API. It receives the request and then executes whatever code needs to be done to handle it, like reading or writing to a database, for example. When it's done, it formats an HTTP response message to go back to the client. It also has a very specific format with a start line that contains the status code. There are a bunch of different status codes, and they're used to tell the client what happened to their request. If everything worked, then you should get a 200 status code, which means okay. Codes at the 200 level mean success. Codes at the 400 level mean that you did something wrong. And codes at the 500 level mean the server failed for some reason. And then below that, we also have a body, which contains the actual content that the server is sending back down to the client, which is often JSON data or HTML to use in a front end application.
And that's really all there is to it. It's a simple request response exchange of data between 2 pieces of software that live on the Internet. Now that you know how APIs work, let's build a background job that runs every few minutes to grab the latest view count from YouTube and then update a YouTube video's title. 1st, we need a piece of software to make API calls from. To handle that, I'll be using a Firebase Cloud function with the Node.
youtube
internet
software
Js runtime. If you're not a Firebase user already, it's very easy to set up. Install the Firebase command line tools, run Firebase init, and you now have a serverless environment to start writing your code in. Now YouTube, of course, is a Google product, which means you need to enable the API from the Google Cloud Platform console. Most major APIs out there, Google included, provide software developer kits to work with their APIs. In our Node. Js project, we can install the Google APIs package with npm. What the SDK does is simplify the process of making calls to the API. If you wanted to, you could send raw HTTP requests to Google, and in theory that would work fine, but that would be doing things the hard way. When using the SDK, we can import Google into our code, and now we have IntelliSense for not just YouTube, but any Google API. Like, if we take a look at Google Drive, for example, you can see it gives us a variety of different endpoints, and then we can simply call a method, and the Google SDK will format the HTTP request for us.
google
youtube
internet
So that's awesome. But at this point, we need to schedule a background job to run our code every few minutes. In Cloud Functions, We can do that by first setting up an export with the name of the function, and then we call functions pubsub schedule and pass in a cron schedule as the argument.
This translates to run every 3 minutes, or in Firebase, we have a more readable option of just writing every 3 minutes. After that, we'll add a Callback function that will run every 3 minutes after we deploy this function to the cloud. Now inside this function, we'll create a variable named YouTube. But in order to use the YouTube API, we need to authenticate. So let's take a second to talk about API authentication. There are 2 main types of authentication that you should know about right now. API keys and OAuth.
youtube
When thinking about YouTube or any API for that matter, there are 2 main types of resources, those that are public and those that are private.
For example, a get request to a public video is something that anybody can do. But if we wanted to modify that video with a put request, We would need to have the proper authorization to do so, like being the video's owner or being an admin at YouTube. Or as developers, we could get the user's permission using OAuth. So here's what that boils down to. If we only need to read public resources, then we can use our YouTube developer API key. The API key is simply a way for YouTube to identify who is making the request, which is important because it's a free API that has a quota on usage, which means that a given API key can only make so many requests per day. In the case of YouTube, they assign points to every request and then cap your daily usage at 10,000 points. Now because we're updating an actual video, we need to go through OAuth 2 in this case. I'm not going to go through the entire process in this video because it can be somewhat complicated. But if it's something that you're interested in implementing, I have a video for that on Fireship IO. Basically, what needs to happen is you need to generate a URL where the user can log in to their YouTube or Google account, then your app will request permission to perform actions on the user's behalf. These permissions are known as scopes. When the user says okay, Google will redirect them back to your site with an authorization code. You then send that code to your server or Cloud Function and use it to create an access token and refresh token. These values can be saved to a database, so you can make API calls for the user in the future. When you're ready to make an API call, you can read the tokens from your database and create this OAuth 2 client object, and then you use it as the auth argument. Now that we're authenticated, we can make an HTTP request to the API using the Google API's SDK. We call YouTube videos list with the video ID, And then we specify the data that we want back. In our case, we want the statistics, which will include the view count, and also the snippet, which contains the raw data about the video, like the description, title, and so on. The API responds back with JSON, which is automatically converted into a JavaScript object for us. And the actual data about this video is the first element in the items array.
data
databases
youtube
video gaming
google
streaming
multimedia
statistics
visualization
From there, we'll grab the view count, then interpolate it into a string to format the new title. And now we're ready to update our video. We'll call YouTube Videos Update, and then we'll define a request body. Remember, the body is the data that the API will use to make the update. We'll assign it the new title, and then it also requires a category ID, which we can keep from the original snippet. And that's all there is to it. Now we just need to deploy our code to get it running in production. Open up the command line and run firebase deploy.
youtube
video gaming
software development
multimedia
This will deploy your code as a serverless function in the cloud. You'll see your function in the Firebase console. If your function doesn't seem to be working, you can check out the logs for errors. I'm gonna go ahead and wrap things up there.
If this video helped you, please like and subscribe. And if you want access to more advanced content, Consider becoming a pro member at FireShip IO. Thanks for watching, and I will see you in the next one.
media
social media
streaming
youtube
Today on How They Do It. If you watch my YouTube channel, there's a good chance you've already seen Tom Scott's video. This video has x amount of views. If not, check it out. But basically what he does is run a background job that hits the YouTube API to update the title with the latest view count every few minutes. Then another YouTuber you may have heard of, MrBeast, appears Appears to be doing the same thing in his latest video, where the thumbnail updates every few minutes with the amount of money given away. If you're wondering how they do this, you might be a little disappointed when Tom Scott says, I'm not gonna talk about the exact details of my code here. It is not the important part because code is just incredibly dull on camera. But I have to disagree there because in my opinion, The code is the most exciting part. So in today's video, we'll take a deep dive into APIs, and I'll show you exactly how these YouTubers are updating their videos automatically way in the background. If you're new here, like and subscribe, and you can grab the full source code from GitHub. My primary mission today is to teach you everything you need to know about API's, guys, which is an essential skill for both front end and back end developers. Along the way, you'll learn about API authentication and OAuth 2, how to work with Google APIs and Node. JS, and how to schedule your code to run-in the background with a serverless cloud function. But first, let's answer the question, what is an API? An application programming interface is simply a way for 2 pieces of software to talk to each other. In this demo, we have our application, which will eventually live on a cloud function. It needs to talk to YouTube to get the view count for a video, and then tell YouTube to update the video with a new title. To facilitate this, YouTube has a standard set of functions where developers can request the data they On the web, APIs are accessed over HTTP. Or, in other words, there's a URL on the Internet that connects that server to the rest of the Internet. The most popular standard for implementing APIs in today's world is REST, which stands for Representational State Transfer. APIs that implement this standard, like YouTube, are called restful APIs. The most important concept to understand is that restful APIs are stateless, which means the client and the server Don't need to know anything about each other's internal state. Instead, they simply pass messages back and forth. The process starts when the client, your application, makes a request. An HTTP request message has a very specific format. The start line contains the URL of the API you're making a request to. It also contains an HTTP g t p method or verb that defines what you're trying to do on the server. A git request means you're trying to access data, but not modify it. Whereas a post request means you're trying to create some new data on the server. The reason this is important is because the API can design endpoints based on a specific entity. We can see that in the YouTube API, where it has different entities for comments, videos, playlists, etcetera. The cool thing about YouTube is that it has an API Explorer, so we can actually look at the raw HTTP message. For example, to retrieve a list of videos, we point to this video's endpoint as a GET request. Then, if we wanted to update that video, we'd point to the same URL, but instead make it a PUT request. So the takeaway here is that the URL defines the resource or entity that lives there, and the verb defines what you want to do to that entity. Now back in the HTTP message, after the start line, we have headers. The headers contain additional metadata about the request. For example, the accept header tells the server which type of content that the client will accept back, which more often than not will be JSON. The headers also handle things like authorization to determine if the user making the request is authorized to do so. Then after the headers, we have the body, which is typically in JSON format, And it contains a payload of data that the server can use to update something or create a new record on the server. Okay. So now that we have our request message, That message gets sent to the server or API. It receives the request and then executes whatever code needs to be done to handle it, like reading or writing to a database, for example. When it's done, it formats an HTTP response message to go back to the client. It also has a very specific format with a start line that contains the status code. There are a bunch of different status codes, and they're used to tell the client what happened to their request. If everything worked, then you should get a 200 status code, which means okay. Codes at the 200 level mean success. Codes at the 400 level mean that you did something wrong. And codes at the 500 level mean the server failed for some reason. And then below that, we also have a body, which contains the actual content that the server is sending back down to the client, which is often JSON data or HTML to use in a front end application. And that's really all there is to it. It's a simple request response exchange of data between 2 pieces of software that live on the Internet. Now that you know how APIs work, let's build a background job that runs every few minutes to grab the latest view count from YouTube and then update a YouTube video's title. 1st, we need a piece of software to make API calls from. To handle that, I'll be using a Firebase Cloud function with the Node. Js runtime. If you're not a Firebase user already, it's very easy to set up. Install the Firebase command line tools, run Firebase init, and you now have a serverless environment to start writing your code in. Now YouTube, of course, is a Google product, which means you need to enable the API from the Google Cloud Platform console. Most major APIs out there, Google included, provide software developer kits to work with their APIs. In our Node. Js project, we can install the Google APIs package with npm. What the SDK does is simplify the process of making calls to the API. If you wanted to, you could send raw HTTP requests to Google, and in theory that would work fine, but that would be doing things the hard way. When using the SDK, we can import Google into our code, and now we have IntelliSense for not just YouTube, but any Google API. Like, if we take a look at Google Drive, for example, you can see it gives us a variety of different endpoints, and then we can simply call a method, and the Google SDK will format the HTTP request for us. So that's awesome. But at this point, we need to schedule a background job to run our code every few minutes. In Cloud Functions, We can do that by first setting up an export with the name of the function, and then we call functions pubsub schedule and pass in a cron schedule as the argument. This translates to run every 3 minutes, or in Firebase, we have a more readable option of just writing every 3 minutes. After that, we'll add a Callback function that will run every 3 minutes after we deploy this function to the cloud. Now inside this function, we'll create a variable named YouTube. But in order to use the YouTube API, we need to authenticate. So let's take a second to talk about API authentication. There are 2 main types of authentication that you should know about right now. API keys and OAuth. When thinking about YouTube or any API for that matter, there are 2 main types of resources, those that are public and those that are private. For example, a get request to a public video is something that anybody can do. But if we wanted to modify that video with a put request, We would need to have the proper authorization to do so, like being the video's owner or being an admin at YouTube. Or as developers, we could get the user's permission using OAuth. So here's what that boils down to. If we only need to read public resources, then we can use our YouTube developer API key. The API key is simply a way for YouTube to identify who is making the request, which is important because it's a free API that has a quota on usage, which means that a given API key can only make so many requests per day. In the case of YouTube, they assign points to every request and then cap your daily usage at 10,000 points. Now because we're updating an actual video, we need to go through OAuth 2 in this case. I'm not going to go through the entire process in this video because it can be somewhat complicated. But if it's something that you're interested in implementing, I have a video for that on Fireship IO. Basically, what needs to happen is you need to generate a URL where the user can log in to their YouTube or Google account, then your app will request permission to perform actions on the user's behalf. These permissions are known as scopes. When the user says okay, Google will redirect them back to your site with an authorization code. You then send that code to your server or Cloud Function and use it to create an access token and refresh token. These values can be saved to a database, so you can make API calls for the user in the future. When you're ready to make an API call, you can read the tokens from your database and create this OAuth 2 client object, and then you use it as the auth argument. Now that we're authenticated, we can make an HTTP request to the API using the Google API's SDK. We call YouTube videos list with the video ID, And then we specify the data that we want back. In our case, we want the statistics, which will include the view count, and also the snippet, which contains the raw data about the video, like the description, title, and so on. The API responds back with JSON, which is automatically converted into a JavaScript object for us. And the actual data about this video is the first element in the items array. From there, we'll grab the view count, then interpolate it into a string to format the new title. And now we're ready to update our video. We'll call YouTube Videos Update, and then we'll define a request body. Remember, the body is the data that the API will use to make the update. We'll assign it the new title, and then it also requires a category ID, which we can keep from the original snippet. And that's all there is to it. Now we just need to deploy our code to get it running in production. Open up the command line and run firebase deploy. This will deploy your code as a serverless function in the cloud. You'll see your function in the Firebase console. If your function doesn't seem to be working, you can check out the logs for errors. I'm gonna go ahead and wrap things up there. If this video helped you, please like and subscribe. And if you want access to more advanced content, Consider becoming a pro member at FireShip IO. Thanks for watching, and I will see you in the next one.
Today
today
on
on
How
how
They
they
Do
do
It.
it
If
if
you
you
watch
watch
my
my
YouTube
youtube
channel,
channel
there's
there's
a
a
good
good
chance
chance
you've
you've
already
already
seen
seen
Tom
tom
Scott's
scott's
video.
video
This
this
video
video
has
has
x
x
amount
amount
of
of
views.
views
If
if
not,
not
check
check
it
it
out.
out
But
but
basically
basically
what
what
he
he
does
does
is
is
run
run
a
a
background
background
job
job
that
that
hits
hits
the
the
YouTube
youtube
API
api
to
to
update
update
the
the
title
title
with
with
the
the
latest
latest
view
view
count
count
every
every
few
few
minutes.
minutes
Then
then
another
another
YouTuber
youtuber
you
you
may
may
have
have
heard
heard
of,
of
MrBeast,
mrbeast
appears
appears
Appears
appears
to
to
be
be
doing
doing
the
the
same
same
thing
thing
in
in
his
his
latest
latest
video,
video
where
where
the
the
thumbnail
thumbnail
updates
updates
every
every
few
few
minutes
minutes
with
with
the
the
amount
amount
of
of
money
money
given
given
away.
away
If
if
you're
you're
wondering
wondering
how
how
they
they
do
do
this,
this
you
you
might
might
be
be
a
a
little
little
disappointed
disappointed
when
when
Tom
tom
Scott
scott
says,
says
I'm
i'm
not
not
gonna
gonna
talk
talk
about
about
the
the
exact
exact
details
details
of
of
my
my
code
code
here.
here
It
it
is
is
not
not
the
the
important
important
part
part
because
because
code
code
is
is
just
just
incredibly
incredibly
dull
dull
on
on
camera.
camera
But
but
I
i
have
have
to
to
disagree
disagree
there
there
because
because
in
in
my
my
opinion,
opinion
The
the
code
code
is
is
the
the
most
most
exciting
exciting
part.
part
So
so
in
in
today's
today's
video,
video
we'll
we'll
take
take
a
a
deep
deep
dive
dive
into
into
APIs,
apis
and
and
I'll
i'll
show
show
you
you
exactly
exactly
how
how
these
these
YouTubers
youtubers
are
are
updating
updating
their
their
videos
videos
automatically
automatically
way
way
in
in
the
the
background.
background
If
if
you're
you're
new
new
here,
here
like
like
and
and
subscribe,
subscribe
and
and
you
you
can
can
grab
grab
the
the
full
full
source
source
code
code
from
from
GitHub.
github
My
my
primary
primary
mission
mission
today
today
is
is
to
to
teach
teach
you
you
everything
everything
you
you
need
need
to
to
know
know
about
about
API's,
api's
guys,
guys
which
which
is
is
an
an
essential
essential
skill
skill
for
for
both
both
front
front
end
end
and
and
back
back
end
end
developers.
developers
Along
along
the
the
way,
way
you'll
you'll
learn
learn
about
about
API
api
authentication
authentication
and
and
OAuth
oauth
2,
2
how
how
to
to
work
work
with
with
Google
google
APIs
apis
and
and
Node.
node
JS,
js
and
and
how
how
to
to
schedule
schedule
your
your
code
code
to
to
run-in
run-in
the
the
background
background
with
with
a
a
serverless
serverless
cloud
cloud
function.
function
But
but
first,
first
let's
let's
answer
answer
the
the
question,
question
what
what
is
is
an
an
API?
api
An
an
application
application
programming
programming
interface
interface
is
is
simply
simply
a
a
way
way
for
for
2
2
pieces
pieces
of
of
software
software
to
to
talk
talk
to
to
each
each
other.
other
In
in
this
this
demo,
demo
we
we
have
have
our
our
application,
application
which
which
will
will
eventually
eventually
live
live
on
on
a
a
cloud
cloud
function.
function
It
it
needs
needs
to
to
talk
talk
to
to
YouTube
youtube
to
to
get
get
the
the
view
view
count
count
for
for
a
a
video,
video
and
and
then
then
tell
tell
YouTube
youtube
to
to
update
update
the
the
video
video
with
with
a
a
new
new
title.
title
To
to
facilitate
facilitate
this,
this
YouTube
youtube
has
has
a
a
standard
standard
set
set
of
of
functions
functions
where
where
developers
developers
can
can
request
request
the
the
data
data
they
they
On
on
the
the
web,
web
APIs
apis
are
are
accessed
accessed
over
over
HTTP.
http
Or,
or
in
in
other
other
words,
words
there's
there's
a
a
URL
url
on
on
the
the
Internet
internet
that
that
connects
connects
that
that
server
server
to
to
the
the
rest
rest
of
of
the
the
Internet.
internet
The
the
most
most
popular
popular
standard
standard
for
for
implementing
implementing
APIs
apis
in
in
today's
today's
world
world
is
is
REST,
rest
which
which
stands
stands
for
for
Representational
representational
State
state
Transfer.
transfer
APIs
apis
that
that
implement
implement
this
this
standard,
standard
like
like
YouTube,
youtube
are
are
called
called
restful
restful
APIs.
apis
The
the
most
most
important
important
concept
concept
to
to
understand
understand
is
is
that
that
restful
restful
APIs
apis
are
are
stateless,
stateless
which
which
means
means
the
the
client
client
and
and
the
the
server
server
Don't
don't
need
need
to
to
know
know
anything
anything
about
about
each
each
other's
other's
internal
internal
state.
state
Instead,
instead
they
they
simply
simply
pass
pass
messages
messages
back
back
and
and
forth.
forth
The
the
process
process
starts
starts
when
when
the
the
client,
client
your
your
application,
application
makes
makes
a
a
request.
request
An
an
HTTP
http
request
request
message
message
has
has
a
a
very
very
specific
specific
format.
format
The
the
start
start
line
line
contains
contains
the
the
URL
url
of
of
the
the
API
api
you're
you're
making
making
a
a
request
request
to.
to
It
it
also
also
contains
contains
an
an
HTTP
http
g
g
t
t
p
p
method
method
or
or
verb
verb
that
that
defines
defines
what
what
you're
you're
trying
trying
to
to
do
do
on
on
the
the
server.
server
A
a
git
git
request
request
means
means
you're
you're
trying
trying
to
to
access
access
data,
data
but
but
not
not
modify
modify
it.
it
Whereas
whereas
a
a
post
post
request
request
means
means
you're
you're
trying
trying
to
to
create
create
some
some
new
new
data
data
on
on
the
the
server.
server
The
the
reason
reason
this
this
is
is
important
important
is
is
because
because
the
the
API
api
can
can
design
design
endpoints
endpoints
based
based
on
on
a
a
specific
specific
entity.
entity
We
we
can
can
see
see
that
that
in
in
the
the
YouTube
youtube
API,
api
where
where
it
it
has
has
different
different
entities
entities
for
for
comments,
comments
videos,
videos
playlists,
playlists
etcetera.
etcetera
The
the
cool
cool
thing
thing
about
about
YouTube
youtube
is
is
that
that
it
it
has
has
an
an
API
api
Explorer,
explorer
so
so
we
we
can
can
actually
actually
look
look
at
at
the
the
raw
raw
HTTP
http
message.
message
For
for
example,
example
to
to
retrieve
retrieve
a
a
list
list
of
of
videos,
videos
we
we
point
point
to
to
this
this
video's
video's
endpoint
endpoint
as
as
a
a
GET
get
request.
request
Then,
then
if
if
we
we
wanted
wanted
to
to
update
update
that
that
video,
video
we'd
we'd
point
point
to
to
the
the
same
same
URL,
url
but
but
instead
instead
make
make
it
it
a
a
PUT
put
request.
request
So
so
the
the
takeaway
takeaway
here
here
is
is
that
that
the
the
URL
url
defines
defines
the
the
resource
resource
or
or
entity
entity
that
that
lives
lives
there,
there
and
and
the
the
verb
verb
defines
defines
what
what
you
you
want
want
to
to
do
do
to
to
that
that
entity.
entity
Now
now
back
back
in
in
the
the
HTTP
http
message,
message
after
after
the
the
start
start
line,
line
we
we
have
have
headers.
headers
The
the
headers
headers
contain
contain
additional
additional
metadata
metadata
about
about
the
the
request.
request
For
for
example,
example
the
the
accept
accept
header
header
tells
tells
the
the
server
server
which
which
type
type
of
of
content
content
that
that
the
the
client
client
will
will
accept
accept
back,
back
which
which
more
more
often
often
than
than
not
not
will
will
be
be
JSON.
json
The
the
headers
headers
also
also
handle
handle
things
things
like
like
authorization
authorization
to
to
determine
determine
if
if
the
the
user
user
making
making
the
the
request
request
is
is
authorized
authorized
to
to
do
do
so.
so
Then
then
after
after
the
the
headers,
headers
we
we
have
have
the
the
body,
body
which
which
is
is
typically
typically
in
in
JSON
json
format,
format
And
and
it
it
contains
contains
a
a
payload
payload
of
of
data
data
that
that
the
the
server
server
can
can
use
use
to
to
update
update
something
something
or
or
create
create
a
a
new
new
record
record
on
on
the
the
server.
server
Okay.
okay
So
so
now
now
that
that
we
we
have
have
our
our
request
request
message,
message
That
that
message
message
gets
gets
sent
sent
to
to
the
the
server
server
or
or
API.
api
It
it
receives
receives
the
the
request
request
and
and
then
then
executes
executes
whatever
whatever
code
code
needs
needs
to
to
be
be
done
done
to
to
handle
handle
it,
it
like
like
reading
reading
or
or
writing
writing
to
to
a
a
database,
database
for
for
example.
example
When
when
it's
it's
done,
done
it
it
formats
formats
an
an
HTTP
http
response
response
message
message
to
to
go
go
back
back
to
to
the
the
client.
client
It
it
also
also
has
has
a
a
very
very
specific
specific
format
format
with
with
a
a
start
start
line
line
that
that
contains
contains
the
the
status
status
code.
code
There
there
are
are
a
a
bunch
bunch
of
of
different
different
status
status
codes,
codes
and
and
they're
they're
used
used
to
to
tell
tell
the
the
client
client
what
what
happened
happened
to
to
their
their
request.
request
If
if
everything
everything
worked,
worked
then
then
you
you
should
should
get
get
a
a
200
200
status
status
code,
code
which
which
means
means
okay.
okay
Codes
codes
at
at
the
the
200
200
level
level
mean
mean
success.
success
Codes
codes
at
at
the
the
400
400
level
level
mean
mean
that
that
you
you
did
did
something
something
wrong.
wrong
And
and
codes
codes
at
at
the
the
500
500
level
level
mean
mean
the
the
server
server
failed
failed
for
for
some
some
reason.
reason
And
and
then
then
below
below
that,
that
we
we
also
also
have
have
a
a
body,
body
which
which
contains
contains
the
the
actual
actual
content
content
that
that
the
the
server
server
is
is
sending
sending
back
back
down
down
to
to
the
the
client,
client
which
which
is
is
often
often
JSON
json
data
data
or
or
HTML
html
to
to
use
use
in
in
a
a
front
front
end
end
application.
application
And
and
that's
that's
really
really
all
all
there
there
is
is
to
to
it.
it
It's
it's
a
a
simple
simple
request
request
response
response
exchange
exchange
of
of
data
data
between
between
2
2
pieces
pieces
of
of
software
software
that
that
live
live
on
on
the
the
Internet.
internet
Now
now
that
that
you
you
know
know
how
how
APIs
apis
work,
work
let's
let's
build
build
a
a
background
background
job
job
that
that
runs
runs
every
every
few
few
minutes
minutes
to
to
grab
grab
the
the
latest
latest
view
view
count
count
from
from
YouTube
youtube
and
and
then
then
update
update
a
a
YouTube
youtube
video's
video's
title.
title
1st,
1st
we
we
need
need
a
a
piece
piece
of
of
software
software
to
to
make
make
API
api
calls
calls
from.
from
To
to
handle
handle
that,
that
I'll
i'll
be
be
using
using
a
a
Firebase
firebase
Cloud
cloud
function
function
with
with
the
the
Node.
node
Js
js
runtime.
runtime
If
if
you're
you're
not
not
a
a
Firebase
firebase
user
user
already,
already
it's
it's
very
very
easy
easy
to
to
set
set
up.
up
Install
install
the
the
Firebase
firebase
command
command
line
line
tools,
tools
run
run
Firebase
firebase
init,
init
and
and
you
you
now
now
have
have
a
a
serverless
serverless
environment
environment
to
to
start
start
writing
writing
your
your
code
code
in.
in
Now
now
YouTube,
youtube
of
of
course,
course
is
is
a
a
Google
google
product,
product
which
which
means
means
you
you
need
need
to
to
enable
enable
the
the
API
api
from
from
the
the
Google
google
Cloud
cloud
Platform
platform
console.
console
Most
most
major
major
APIs
apis
out
out
there,
there
Google
google
included,
included
provide
provide
software
software
developer
developer
kits
kits
to
to
work
work
with
with
their
their
APIs.
apis
In
in
our
our
Node.
node
Js
js
project,
project
we
we
can
can
install
install
the
the
Google
google
APIs
apis
package
package
with
with
npm.
npm
What
what
the
the
SDK
sdk
does
does
is
is
simplify
simplify
the
the
process
process
of
of
making
making
calls
calls
to
to
the
the
API.
api
If
if
you
you
wanted
wanted
to,
to
you
you
could
could
send
send
raw
raw
HTTP
http
requests
requests
to
to
Google,
google
and
and
in
in
theory
theory
that
that
would
would
work
work
fine,
fine
but
but
that
that
would
would
be
be
doing
doing
things
things
the
the
hard
hard
way.
way
When
when
using
using
the
the
SDK,
sdk
we
we
can
can
import
import
Google
google
into
into
our
our
code,
code
and
and
now
now
we
we
have
have
IntelliSense
intellisense
for
for
not
not
just
just
YouTube,
youtube
but
but
any
any
Google
google
API.
api
Like,
like
if
if
we
we
take
take
a
a
look
look
at
at
Google
google
Drive,
drive
for
for
example,
example
you
you
can
can
see
see
it
it
gives
gives
us
us
a
a
variety
variety
of
of
different
different
endpoints,
endpoints
and
and
then
then
we
we
can
can
simply
simply
call
call
a
a
method,
method
and
and
the
the
Google
google
SDK
sdk
will
will
format
format
the
the
HTTP
http
request
request
for
for
us.
us
So
so
that's
that's
awesome.
awesome
But
but
at
at
this
this
point,
point
we
we
need
need
to
to
schedule
schedule
a
a
background
background
job
job
to
to
run
run
our
our
code
code
every
every
few
few
minutes.
minutes
In
in
Cloud
cloud
Functions,
functions
We
we
can
can
do
do
that
that
by
by
first
first
setting
setting
up
up
an
an
export
export
with
with
the
the
name
name
of
of
the
the
function,
function
and
and
then
then
we
we
call
call
functions
functions
pubsub
pubsub
schedule
schedule
and
and
pass
pass
in
in
a
a
cron
cron
schedule
schedule
as
as
the
the
argument.
argument
This
this
translates
translates
to
to
run
run
every
every
3
3
minutes,
minutes
or
or
in
in
Firebase,
firebase
we
we
have
have
a
a
more
more
readable
readable
option
option
of
of
just
just
writing
writing
every
every
3
3
minutes.
minutes
After
after
that,
that
we'll
we'll
add
add
a
a
Callback
callback
function
function
that
that
will
will
run
run
every
every
3
3
minutes
minutes
after
after
we
we
deploy
deploy
this
this
function
function
to
to
the
the
cloud.
cloud
Now
now
inside
inside
this
this
function,
function
we'll
we'll
create
create
a
a
variable
variable
named
named
YouTube.
youtube
But
but
in
in
order
order
to
to
use
use
the
the
YouTube
youtube
API,
api
we
we
need
need
to
to
authenticate.
authenticate
So
so
let's
let's
take
take
a
a
second
second
to
to
talk
talk
about
about
API
api
authentication.
authentication
There
there
are
are
2
2
main
main
types
types
of
of
authentication
authentication
that
that
you
you
should
should
know
know
about
about
right
right
now.
now
API
api
keys
keys
and
and
OAuth.
oauth
When
when
thinking
thinking
about
about
YouTube
youtube
or
or
any
any
API
api
for
for
that
that
matter,
matter
there
there
are
are
2
2
main
main
types
types
of
of
resources,
resources
those
those
that
that
are
are
public
public
and
and
those
those
that
that
are
are
private.
private
For
for
example,
example
a
a
get
get
request
request
to
to
a
a
public
public
video
video
is
is
something
something
that
that
anybody
anybody
can
can
do.
do
But
but
if
if
we
we
wanted
wanted
to
to
modify
modify
that
that
video
video
with
with
a
a
put
put
request,
request
We
we
would
would
need
need
to
to
have
have
the
the
proper
proper
authorization
authorization
to
to
do
do
so,
so
like
like
being
being
the
the
video's
video's
owner
owner
or
or
being
being
an
an
admin
admin
at
at
YouTube.
youtube
Or
or
as
as
developers,
developers
we
we
could
could
get
get
the
the
user's
user's
permission
permission
using
using
OAuth.
oauth
So
so
here's
here's
what
what
that
that
boils
boils
down
down
to.
to
If
if
we
we
only
only
need
need
to
to
read
read
public
public
resources,
resources
then
then
we
we
can
can
use
use
our
our
YouTube
youtube
developer
developer
API
api
key.
key
The
the
API
api
key
key
is
is
simply
simply
a
a
way
way
for
for
YouTube
youtube
to
to
identify
identify
who
who
is
is
making
making
the
the
request,
request
which
which
is
is
important
important
because
because
it's
it's
a
a
free
free
API
api
that
that
has
has
a
a
quota
quota
on
on
usage,
usage
which
which
means
means
that
that
a
a
given
given
API
api
key
key
can
can
only
only
make
make
so
so
many
many
requests
requests
per
per
day.
day
In
in
the
the
case
case
of
of
YouTube,
youtube
they
they
assign
assign
points
points
to
to
every
every
request
request
and
and
then
then
cap
cap
your
your
daily
daily
usage
usage
at
at
10,000
10,000
points.
points
Now
now
because
because
we're
we're
updating
updating
an
an
actual
actual
video,
video
we
we
need
need
to
to
go
go
through
through
OAuth
oauth
2
2
in
in
this
this
case.
case
I'm
i'm
not
not
going
going
to
to
go
go
through
through
the
the
entire
entire
process
process
in
in
this
this
video
video
because
because
it
it
can
can
be
be
somewhat
somewhat
complicated.
complicated
But
but
if
if
it's
it's
something
something
that
that
you're
you're
interested
interested
in
in
implementing,
implementing
I
i
have
have
a
a
video
video
for
for
that
that
on
on
Fireship
fireship
IO.
io
Basically,
basically
what
what
needs
needs
to
to
happen
happen
is
is
you
you
need
need
to
to
generate
generate
a
a
URL
url
where
where
the
the
user
user
can
can
log
log
in
in
to
to
their
their
YouTube
youtube
or
or
Google
google
account,
account
then
then
your
your
app
app
will
will
request
request
permission
permission
to
to
perform
perform
actions
actions
on
on
the
the
user's
user's
behalf.
behalf
These
these
permissions
permissions
are
are
known
known
as
as
scopes.
scopes
When
when
the
the
user
user
says
says
okay,
okay
Google
google
will
will
redirect
redirect
them
them
back
back
to
to
your
your
site
site
with
with
an
an
authorization
authorization
code.
code
You
you
then
then
send
send
that
that
code
code
to
to
your
your
server
server
or
or
Cloud
cloud
Function
function
and
and
use
use
it
it
to
to
create
create
an
an
access
access
token
token
and
and
refresh
refresh
token.
token
These
these
values
values
can
can
be
be
saved
saved
to
to
a
a
database,
database
so
so
you
you
can
can
make
make
API
api
calls
calls
for
for
the
the
user
user
in
in
the
the
future.
future
When
when
you're
you're
ready
ready
to
to
make
make
an
an
API
api
call,
call
you
you
can
can
read
read
the
the
tokens
tokens
from
from
your
your
database
database
and
and
create
create
this
this
OAuth
oauth
2
2
client
client
object,
object
and
and
then
then
you
you
use
use
it
it
as
as
the
the
auth
auth
argument.
argument
Now
now
that
that
we're
we're
authenticated,
authenticated
we
we
can
can
make
make
an
an
HTTP
http
request
request
to
to
the
the
API
api
using
using
the
the
Google
google
API's
api's
SDK.
sdk
We
we
call
call
YouTube
youtube
videos
videos
list
list
with
with
the
the
video
video
ID,
id
And
and
then
then
we
we
specify
specify
the
the
data
data
that
that
we
we
want
want
back.
back
In
in
our
our
case,
case
we
we
want
want
the
the
statistics,
statistics
which
which
will
will
include
include
the
the
view
view
count,
count
and
and
also
also
the
the
snippet,
snippet
which
which
contains
contains
the
the
raw
raw
data
data
about
about
the
the
video,
video
like
like
the
the
description,
description
title,
title
and
and
so
so
on.
on
The
the
API
api
responds
responds
back
back
with
with
JSON,
json
which
which
is
is
automatically
automatically
converted
converted
into
into
a
a
JavaScript
javascript
object
object
for
for
us.
us
And
and
the
the
actual
actual
data
data
about
about
this
this
video
video
is
is
the
the
first
first
element
element
in
in
the
the
items
items
array.
array
From
from
there,
there
we'll
we'll
grab
grab
the
the
view
view
count,
count
then
then
interpolate
interpolate
it
it
into
into
a
a
string
string
to
to
format
format
the
the
new
new
title.
title
And
and
now
now
we're
we're
ready
ready
to
to
update
update
our
our
video.
video
We'll
we'll
call
call
YouTube
youtube
Videos
videos
Update,
update
and
and
then
then
we'll
we'll
define
define
a
a
request
request
body.
body
Remember,
remember
the
the
body
body
is
is
the
the
data
data
that
that
the
the
API
api
will
will
use
use
to
to
make
make
the
the
update.
update
We'll
we'll
assign
assign
it
it
the
the
new
new
title,
title
and
and
then
then
it
it
also
also
requires
requires
a
a
category
category
ID,
id
which
which
we
we
can
can
keep
keep
from
from
the
the
original
original
snippet.
snippet
And
and
that's
that's
all
all
there
there
is
is
to
to
it.
it
Now
now
we
we
just
just
need
need
to
to
deploy
deploy
our
our
code
code
to
to
get
get
it
it
running
running
in
in
production.
production
Open
open
up
up
the
the
command
command
line
line
and
and
run
run
firebase
firebase
deploy.
deploy
This
this
will
will
deploy
deploy
your
your
code
code
as
as
a
a
serverless
serverless
function
function
in
in
the
the
cloud.
cloud
You'll
you'll
see
see
your
your
function
function
in
in
the
the
Firebase
firebase
console.
console
If
if
your
your
function
function
doesn't
doesn't
seem
seem
to
to
be
be
working,
working
you
you
can
can
check
check
out
out
the
the
logs
logs
for
for
errors.
errors
I'm
i'm
gonna
gonna
go
go
ahead
ahead
and
and
wrap
wrap
things
things
up
up
there.
there
If
if
this
this
video
video
helped
helped
you,
you
please
please
like
like
and
and
subscribe.
subscribe
And
and
if
if
you
you
want
want
access
access
to
to
more
more
advanced
advanced
content,
content
Consider
consider
becoming
becoming
a
a
pro
pro
member
member
at
at
FireShip
fireship
IO.
io
Thanks
thanks
for
for
watching,
watching
and
and
I
i
will
will
see
see
you
you
in
in
the
the
next
next
one.
one
success
The speakers discuss how YouTubers update videos automatically through YouTube API and the importance of APIs for developers. They also discuss the use of restful APIs and the process for updating a YouTube video's title. The speakers explain the use of a developer kit for APIs, AWS function, and the use of a YouTube video ID. They also mention the use of a cloud function for YouTube and the need for authorization to use it. Finally, they discuss the use of a YouTube video ID and the process of updating a video to get it running in production.
How Tom Scott & Mr. Beast use APIs to update their videos programmatically
Today on How They Do It. If you watch my YouTube channel, there's a good chance you've already seen Tom Scott's video. This video has x amount of views. If not, check it out. But basically what he does is run a background job that hits the YouTube API to update the title with the latest view count every few minutes. Then another YouTuber you may have heard of, MrBeast, appears Appears to be doing the same thing in his latest video, where the thumbnail updates every few minutes with the amount of money given away. If you're wondering how they do this, you might be a little disappointed when Tom Scott says, I'm not gonna talk about the exact details of my code here. It is not the important part because code is just incredibly dull on camera. But I have to disagree there because in my opinion, The code is the most exciting part. So in today's video, we'll take a deep dive into APIs, and I'll show you exactly how these YouTubers are updating their videos automatically way in the background. If you're new here, like and subscribe, and you can grab the full source code from GitHub. My primary mission today is to teach you everything you need to know about API's, guys, which is an essential skill for both front end and back end developers. Along the way, you'll learn about API authentication and OAuth 2, how to work with Google APIs and Node. JS, and how to schedule your code to run-in the background with a serverless cloud function. But first, let's answer the question, what is an API? An application programming interface is simply a way for 2 pieces of software to talk to each other. In this demo, we have our application, which will eventually live on a cloud function. It needs to talk to YouTube to get the view count for a video, and then tell YouTube to update the video with a new title. To facilitate this, YouTube has a standard set of functions where developers can request the data they On the web, APIs are accessed over HTTP. Or, in other words, there's a URL on the Internet that connects that server to the rest of the Internet. The most popular standard for implementing APIs in today's world is REST, which stands for Representational State Transfer. APIs that implement this standard, like YouTube, are called restful APIs. The most important concept to understand is that restful APIs are stateless, which means the client and the server Don't need to know anything about each other's internal state. Instead, they simply pass messages back and forth. The process starts when the client, your application, makes a request. An HTTP request message has a very specific format. The start line contains the URL of the API you're making a request to. It also contains an HTTP g t p method or verb that defines what you're trying to do on the server. A git request means you're trying to access data, but not modify it. Whereas a post request means you're trying to create some new data on the server. The reason this is important is because the API can design endpoints based on a specific entity. We can see that in the YouTube API, where it has different entities for comments, videos, playlists, etcetera. The cool thing about YouTube is that it has an API Explorer, so we can actually look at the raw HTTP message. For example, to retrieve a list of videos, we point to this video's endpoint as a GET request. Then, if we wanted to update that video, we'd point to the same URL, but instead make it a PUT request. So the takeaway here is that the URL defines the resource or entity that lives there, and the verb defines what you want to do to that entity. Now back in the HTTP message, after the start line, we have headers. The headers contain additional metadata about the request. For example, the accept header tells the server which type of content that the client will accept back, which more often than not will be JSON. The headers also handle things like authorization to determine if the user making the request is authorized to do so. Then after the headers, we have the body, which is typically in JSON format, And it contains a payload of data that the server can use to update something or create a new record on the server. Okay. So now that we have our request message, That message gets sent to the server or API. It receives the request and then executes whatever code needs to be done to handle it, like reading or writing to a database, for example. When it's done, it formats an HTTP response message to go back to the client. It also has a very specific format with a start line that contains the status code. There are a bunch of different status codes, and they're used to tell the client what happened to their request. If everything worked, then you should get a 200 status code, which means okay. Codes at the 200 level mean success. Codes at the 400 level mean that you did something wrong. And codes at the 500 level mean the server failed for some reason. And then below that, we also have a body, which contains the actual content that the server is sending back down to the client, which is often JSON data or HTML to use in a front end application. And that's really all there is to it. It's a simple request response exchange of data between 2 pieces of software that live on the Internet. Now that you know how APIs work, let's build a background job that runs every few minutes to grab the latest view count from YouTube and then update a YouTube video's title. 1st, we need a piece of software to make API calls from. To handle that, I'll be using a Firebase Cloud function with the Node. Js runtime. If you're not a Firebase user already, it's very easy to set up. Install the Firebase command line tools, run Firebase init, and you now have a serverless environment to start writing your code in. Now YouTube, of course, is a Google product, which means you need to enable the API from the Google Cloud Platform console. Most major APIs out there, Google included, provide software developer kits to work with their APIs. In our Node. Js project, we can install the Google APIs package with npm. What the SDK does is simplify the process of making calls to the API. If you wanted to, you could send raw HTTP requests to Google, and in theory that would work fine, but that would be doing things the hard way. When using the SDK, we can import Google into our code, and now we have IntelliSense for not just YouTube, but any Google API. Like, if we take a look at Google Drive, for example, you can see it gives us a variety of different endpoints, and then we can simply call a method, and the Google SDK will format the HTTP request for us. So that's awesome. But at this point, we need to schedule a background job to run our code every few minutes. In Cloud Functions, We can do that by first setting up an export with the name of the function, and then we call functions pubsub schedule and pass in a cron schedule as the argument. This translates to run every 3 minutes, or in Firebase, we have a more readable option of just writing every 3 minutes. After that, we'll add a Callback function that will run every 3 minutes after we deploy this function to the cloud. Now inside this function, we'll create a variable named YouTube. But in order to use the YouTube API, we need to authenticate. So let's take a second to talk about API authentication. There are 2 main types of authentication that you should know about right now. API keys and OAuth. When thinking about YouTube or any API for that matter, there are 2 main types of resources, those that are public and those that are private. For example, a get request to a public video is something that anybody can do. But if we wanted to modify that video with a put request, We would need to have the proper authorization to do so, like being the video's owner or being an admin at YouTube. Or as developers, we could get the user's permission using OAuth. So here's what that boils down to. If we only need to read public resources, then we can use our YouTube developer API key. The API key is simply a way for YouTube to identify who is making the request, which is important because it's a free API that has a quota on usage, which means that a given API key can only make so many requests per day. In the case of YouTube, they assign points to every request and then cap your daily usage at 10,000 points. Now because we're updating an actual video, we need to go through OAuth 2 in this case. I'm not going to go through the entire process in this video because it can be somewhat complicated. But if it's something that you're interested in implementing, I have a video for that on Fireship IO. Basically, what needs to happen is you need to generate a URL where the user can log in to their YouTube or Google account, then your app will request permission to perform actions on the user's behalf. These permissions are known as scopes. When the user says okay, Google will redirect them back to your site with an authorization code. You then send that code to your server or Cloud Function and use it to create an access token and refresh token. These values can be saved to a database, so you can make API calls for the user in the future. When you're ready to make an API call, you can read the tokens from your database and create this OAuth 2 client object, and then you use it as the auth argument. Now that we're authenticated, we can make an HTTP request to the API using the Google API's SDK. We call YouTube videos list with the video ID, And then we specify the data that we want back. In our case, we want the statistics, which will include the view count, and also the snippet, which contains the raw data about the video, like the description, title, and so on. The API responds back with JSON, which is automatically converted into a JavaScript object for us. And the actual data about this video is the first element in the items array. From there, we'll grab the view count, then interpolate it into a string to format the new title. And now we're ready to update our video. We'll call YouTube Videos Update, and then we'll define a request body. Remember, the body is the data that the API will use to make the update. We'll assign it the new title, and then it also requires a category ID, which we can keep from the original snippet. And that's all there is to it. Now we just need to deploy our code to get it running in production. Open up the command line and run firebase deploy. This will deploy your code as a serverless function in the cloud. You'll see your function in the Firebase console. If your function doesn't seem to be working, you can check out the logs for errors. I'm gonna go ahead and wrap things up there. If this video helped you, please like and subscribe. And if you want access to more advanced content, Consider becoming a pro member at FireShip IO. Thanks for watching, and I will see you in the next one.
The speakers discuss how YouTubers update videos automatically through YouTube API and the importance of APIs for developers. They also discuss the use of restful APIs and the process for updating a YouTube video's title. The speakers explain the use of a developer kit for APIs, AWS function, and the use of a YouTube video ID. They also mention the use of a cloud function for YouTube and the need for authorization to use it. Finally, they discuss the use of a YouTube video ID and the process of updating a video to get it running in production.