The Complete Guide to the YouTube API with TypeScript
A practical, end-to-end reference for building against the YouTube Data API v3 using Google's official, fully-typed Node.js client. Covers setup, authentication, every major resource, quota management, pagination, uploads, error handling, and production patterns — all in TypeScript.
Versions referenced:
googleapisv173+,@googleapis/youtubev33+. The YouTube Data API itself is v3 (stable since 2013). Both client packages ship TypeScript types out of the box — no@types/*package needed.
Table of Contents
- Which package should I use?
- Prerequisites & Google Cloud setup
- Installation & TypeScript config
- Core concepts: resources, parts, and quota
- Authentication
- A typed client wrapper
- Search
- Videos
- Channels
- Playlists & playlist items
- Subscriptions
- Comments & comment threads
- Captions
- Uploading a video (resumable media)
- Thumbnails
- Pagination done right
- Quota management
- Error handling & retries
- The YouTube Analytics API
- Real-time push notifications (PubSubHubbub)
- Testing & mocking
- Production best practices
- Common pitfalls
- Appendix A: Quota cost reference
- Appendix B: OAuth scopes reference
1. Which package should I use?
There is no separate "YouTube SDK." Google publishes auto-generated client libraries, and for TypeScript/Node you have two options:
| Package | What it is | When to use |
|---|---|---|
googleapis | The mega-package with every Google API (Drive, Gmail, YouTube, …). ~170MB+ installed. | You use multiple Google APIs, or you want one import surface. |
@googleapis/youtube | A standalone package containing only YouTube + the shared auth library. Much smaller. | You only need YouTube. Recommended for most projects. |
Both expose the same API surface and the same youtube_v3 type namespace. Examples in this guide use googleapis for familiarity, but switching is trivial:
// Option A — the mega-package
import { google } from 'googleapis';
const youtube = google.youtube('v3');
// Option B — the standalone package (same types, smaller install)
import { youtube, auth } from '@googleapis/youtube';
const yt = youtube('v3');
Avoid unofficial wrappers like
youtube-api(an old OO wrapper). They lag behind the API and lack first-class types. Stick with the official Google packages.
2. Prerequisites & Google Cloud setup
Before writing code you need credentials. The API is never anonymous.
Step 1 — Create a Google Cloud project
- Go to the Google Cloud Console.
- Create a new project (or select an existing one).
Step 2 — Enable the YouTube Data API v3
Navigate to APIs & Services → Library, search "YouTube Data API v3", and click Enable. If you'll use analytics or reporting, enable YouTube Analytics API and YouTube Reporting API too.
Step 3 — Create credentials
You need one or both of:
- API key — for reading public data only (search, public videos/channels/playlists). Cannot access private data or perform writes. Create via Credentials → Create credentials → API key. Restrict it to the YouTube Data API.
- OAuth 2.0 client ID — required for anything user-specific or any write (upload, rate, comment, subscribe, manage playlists). Create via Credentials → Create credentials → OAuth client ID. You'll also configure the OAuth consent screen (add scopes and, while in "testing" mode, add test users).
Service accounts do not work with YouTube for normal channels. A service account has no YouTube channel of its own, and YouTube rejects it. The only exception is Content Owner (CMS/partner) setups via
onBehalfOfContentOwner. For a regular channel you must use the interactive OAuth flow at least once to mint a refresh token.