About
This is a wrapper for the Youtube API with typings. It allows you to easily search videos, channels, playlists, and more content from Youtube!
Install
npm install youtube.ts
Getting Started
You will need to obtain a Google API Key from here: https://console.developers.google.com
Create a new project and enable Youtube Data API v3.
Searching for channels and subscriptions
import Youtube from "youtube.ts"
async function useAPI() {
const youtube = new Youtube(process.env.GOOGLE_API_KEY)
const channel = await youtube.channels.get("https://www.youtube.com/channel/UC8qU4aFe81jzG1attsyQ5wQ")
const channelByCustomURL = await youtube.channels.get("https://www.youtube.com/c/mychannel")
const channelByQuery = await youtube.channels.get("mychannel")
const channelSearch = await youtube.channels.search({q: "cool channels", maxResults: 10})
const discussionComments = await youtube.channels.comments("mychannel")
const allComments = await youtube.channels.allComments("mychannel")
const subscriptions = await youtube.channel.subscriptions("some channel")
const subscription = await youtube.subscriptions.get("g3a2JN_ndb4bKe1baMCxV1arn1DRZpHxVk1i_ZTl4uA")
}
useAPI()
Searching for videos and comments
async function useAPI() {
const video = await youtube.videos.get("https://www.youtube.com/watch?v=yexu92rKpjs")
const videoSearch = await youtube.videos.search({q: "anime", maxResults: 10})
const comments = await youtube.videos.comments("https://www.youtube.com/watch?v=mLJQ0HO5Alc")
const comment = await youtube.comments.thread("UgzNgGMYuY6qJl0RV7d4AaABAg")
const reply = await youtube.comments.get("UgzNgGMYuY6qJl0RV7d4AaABAg")
}
Searching for playlists
async function useAPI() {
const playlist = await youtube.playlists.get("https://www.youtube.com/playlist?list=PL1BC175_2xP8ifSS9CM92G5eIOPRG1g7t")
const playlistItems = await youtube.playlists.items("https://www.youtube.com/playlist?list=PL1BC175_2xP8ifSS9CM92G5eIOPRG1g7t")
const item = await youtube.playlists.item("PL1BC175_2xP8ifSS9CM92G5eIOPRG1g7t", "tenpi - moonlight (chill)")
const itemByID = await youtube.playlists.itemByID("UEwxQkMxNzVfMnhQOGlmU1M5Q005Mkc1ZUlPUFJHMWc3dC41MzJCQjBCNDIyRkJDN0VD")
const playlistSearch = await youtube.playlists.search({q: "cool playlist"})
}
Downloading Videos and Audio Tracks
While downloading videos is not apart of the API, it's possible to download your own videos through youtube studio.
You are going to need a key and your browser cookie in order for this to work.
- Visit Youtube studio and go to any video page.
- Open up dev tools (right click -> inspect) and open the network tab.
- Download your video (hamburger menu -> download) and observe the network tab.
- Look for an entry named
download_my_video?v=${video_id}&t={key}
- You want to grab the t parameter (this is your key). Also grab your browser cookie under
Request Headers
, this is required to authenticate the request.
The other method, which works for any video, is to download it using yt-dlp. You will have to install this separately and make it available from invoking yt-dlp in the terminal.
async function useAPI() {
const key = "your key"
const cookie = "your long browser cookie"
await youtube.util.downloadMyVideo("https://www.youtube.com/watch?v=-BW7kUAPZiA", key, cookie, "./videos")
await youtube.util.downloadMyVideos("https://www.youtube.com/channel/UC8qU4aFe81jzG1attsyQ5wQ", key, cookie, "./videos", 100)
await youtube.util.downloadVideo("https://www.youtube.com/watch?v=mLJQ0HO5Alc", "./videos", {format: "mp4", quality: "720p60"})
await youtube.util.downloadMP3("https://www.youtube.com/watch?v=mLJQ0HO5Alc", "./videos/mp3")
const videos = await youtube.videos.search({q: "cool vid"})
await youtube.util.downloadVideos(videos, "./videos")
await youtube.util.downloadMP3s(videos, "./videos/mp3")
await youtube.util.downloadChannelVideos("tenpi", "./videos")
await youtube.util.downloadChannelMP3s("tenpi", "./videos/mp3")
const readableStream = await youtube.util.streamMP3("https://www.youtube.com/watch?v=mLJQ0HO5Alc")
}
Common Types
YoutubeChannel
export interface YoutubeChannel {
kind: string
etag: string
id: string
snippet: YoutubeChannelSnippet
contentDetails: YoutubeChannelContentDetails
statistics: YoutubeChannelStatistics
brandingSettings: YoutubeBrandingSettings
country: string
}
YoutubeVideo
export interface YoutubeVideo {
kind: string
etag: string
id: string
snippet: YoutubeVideoSnippet
contentDetails: YoutubeVideoContentDetails
status: YoutubeVideoStatus
statistics: YoutubeVideoStatistics
player: {
embedHtml: string
}
}
YoutubePlaylist
export interface YoutubePlaylist {
kind: string
etag: string
id: string
snippet: YoutubePlaylistSnippet
status: {
privacyStatus: string
},
contentDetails: {
itemCount: number
},
player: {
embedHtml: string
}
}
YoutubeComment
export interface YoutubeComment {
kind: string
etag: string
id: string
snippet: {
authorDisplayName: string
authorProfileImageUrl: string
authorChannelUrl: string
authorChannelId: {
value: string
},
channelId?: string
videoId: string
textDisplay: string
textOriginal: string
canRate: boolean
viewerRating: string
likeCount: number
publishedAt: string
updatedAt: string
}
}