Home

Awesome

<div align="center">

youtube-deno : A Deno client for the YouTube Data API

Lets you incorporate core YouTube features, such as uploading videos, creating and managing playlists, searching for content, and almost any interaction with YouTube in your Deno application. <br><br> This library is a part of <br> <a href="https://deno.land/x/"> <img src="https://cdn4.iconfinder.com/data/icons/logos-brands-5/24/deno-64.png"> </a>

nest badge

<hr>

Maintained Stars Telegram

</div>
<br>

Why?

Installing

Deno allows you to directly import modules from URLs! To import and use the client in your file, add the following import statement:

import { YouTube } from 'https://deno.land/x/youtube@v0.3.0/mod.ts';
<br>

Usage

Configuring your App

Firstly, you'll have to register your app in the Google Developers Console.<br> Browse the console and select "New Project".<br>

After registering your app, enable the YouTube API for your app by clicking on the "ENABLE APIS AND SERVICES" button and selecting "YouTube Data API v3".<br>

After this, you'll need to create some credentials through the console depending on what you want to do, to be able to make API requests. <br> There are two types of objects you can make according to your requirement:

Objects that do not require user interactions

If you only need to fetch public data from YouTube, then all you need is an API key. <br><br> Click on the "CREATE CREDENTIALS" button in the console and select "API key" from the options. <br> (Note that an API key is required for making each and every request to this API, so this step is mandatory) <br><br> To create an object which uses this API key only for authentication:

let yt = new YouTube("Your-api-key-here", false); // The false is for access token

(Remember: This object is not allowed to perform any account related operation, so you won’t be able to like a video, subscribe to a channel or delete a playlist etc. from a specific account. You will only be able to retrieve read-only public data. For these operations, create an OAuth authorized object)

Now, to use this object, just call any function that only lists read-only public data. For example:

yt1.search_list({part: "snippet"}).then(function(response){
 console.log(response);
});

Objects that require user interactions (user consent by OAuth 2.0 authorization)

(Note: Creating the API key from the previous section is mandatory.)<br><br> If you need to make requests that involve access to a YouTube account, you need the owner of each account to authorize your app. For that, you need an access token to be passed to the object you create. This can have the following cases:

Case 1: You already have an access token<br> If you already have an access token, you simply have to pass it while creating the object:

let yt = new YouTube("your-api-key-here", "access-token-here");

Now, using this object you can call functions (by passing the apt parameters) which require YouTube account interactions. <br>

Case 2: You do not have an access token<br> If you do not have an access token already, you can generate it using our authenticator class. For that, follow these steps:

import { authenticator, authParams } from 'https://deno.land/x/youtube@v0.3.0/mod.ts';

let auth = new authenticator();
let creds: authParams = {
	client_id: "your-client-id",
	redirect_uri: "your-redirect-uri",
	scope: "your-decided-scopes"
};

let auth_url: string = auth.authenticate(creds);

Here, copy the client ID that you just created from the console under client-id field. The redirect_uri field must contain the same one as you filled in the form at the Developer console. The scope field tells how much access you want the user to grant to your app, decide the scopes from here and add space delimited values in the string for multiple scopes (Exapmle: scope: "https://www.googleapis.com/auth/youtube.readonly https://www.googleapis.com/auth/youtube.upload").

let yt = new YouTube("your-api-key-here", "access-token-here");

Now, using this object you can call functions (by passing the apt parameters) which require YouTube account interactions.

(Note: For CLI applications for personal use, you can open the auth_url manually in a browser and after giving access, you'll be redirected to the redirect_uri with an extra #access_token parameter which you can copy into the object created above)

<br><br>

Calling available functions

For the detailed API docs of this client library, look here.<br> For a better understanding of a particular function and parameters information, refer the YouTube Data API docs for that function.<br><br>

The following is a list of functions that you can call using one of the objects created above:<br>

The params parameter for each function must be an object type, following its schema.

All the functions return the response JSON after making the request. <br>

Examples

// A simple example to call the search_list() function and log the response json.
import { YouTube } from 'https://deno.land/x/youtube@v0.3.0/mod.ts';

let obj = new YouTube("your-api-key-here", false);

obj.search_list({part: "snippet", q: "coldplay"}).then(function(response){
 console.log(response);
});

// An example to call the activities_list() function using an authorized access token and log the response json.
import { YouTube } from 'https://deno.land/x/youtube@v0.3.0/mod.ts';

let obj = new YouTube("your-api-key-here", "access-token-here");

obj.activities_list({part: "snippet", mine: true}).then(function(response){
 console.log(response);
});


<br>

Contributing

youtube-deno needs your support! The goal of youtube-deno is to ease the usage of the YouTube API with Deno, which is a great piece of software!

If you find that a method is missing, find a change in YouTube Data API v3, notice a bug or issue, open an issue for it and once it's confirmed, just fork the project, make the relevant change, write the appropriate tests, then submit a pull request, and it will gladly be merged!<br>

For any sort of queries or discussions regarding the project, join our Telegram channel.