Skip to main content
A tiny, dependency-free helper for working with YouTube links. It parses the video ID out of standard, short, embed, and Shorts URLs, then lets you generate clean embed, watch, and thumbnail URLs. Every function accepts either a full URL or a bare video ID and returns null when the input can’t be resolved.

Installation

Add the package to your project with Bun.

Usage

Import the function you need and pass it a URL or an existing video ID. Extract the video ID from any supported YouTube URL:
Build an embed URL for an iframe player. You can pass a full URL or just the ID:
Generate a canonical watch URL:
Get a thumbnail image URL for a video:
Each builder returns null when the input is neither a recognizable YouTube URL nor a valid-looking video ID, so you can guard before rendering:

Where it comes in handy

The common thread is taking a messy YouTube link and getting something usable out of it. You can resolve a video ID from mixed formats (watch, youtu.be, embed, /v/, Shorts), render an iframe embed from a user-submitted link, or normalize any URL into a canonical watch?v= form. It also builds thumbnail URLs without a call to the YouTube Data API, and since every function returns null on input it can’t resolve, it doubles as a way to check that a string is a usable URL or ID before you store or render it.