YouTubeMusicAPI 2.2.2

dotnet add package YouTubeMusicAPI --version 2.2.2                
NuGet\Install-Package YouTubeMusicAPI -Version 2.2.2                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="YouTubeMusicAPI" Version="2.2.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add YouTubeMusicAPI --version 2.2.2                
#r "nuget: YouTubeMusicAPI, 2.2.2"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install YouTubeMusicAPI as a Cake Addin
#addin nuget:?package=YouTubeMusicAPI&version=2.2.2

// Install YouTubeMusicAPI as a Cake Tool
#tool nuget:?package=YouTubeMusicAPI&version=2.2.2                

YouTubeMusicAPI is a simple and efficient C# wrapper for the YouTube Music Web API, enabling easy search and retrieval of songs, videos, albums, artists, podcasts, and more. It also provides streaming data and access to an authenticated user's library, all with minimal effort.


Getting Started

To install YouTubeMusicAPI, add the following package to your project:

dotnet add package YouTubeMusicAPI

To start using YouTube Music in your project, just create a new YouTubeMusicClient.

YouTubeMusicClient client = new(logger, geographicalLocation, visitorData, poToken, cookies);

Usage

Using this wrapper, you can search in two ways: directly for items or via "shelves." A shelf is a container for search results like songs or videos. Each shelf has a type (e.g., "Songs") and a "Next Continuation Token" to load more results dynamically (e.g., for infinite scrolling on a page).

If this seems complicated, no worries! You can simply use the search method with a generic type SearchAsync<IYouTubeMusicItem>() instead, which also contains a property to limit the search result so you dont have to deal with any of the shelf or token stuff.

// Search for songs directly
IEnumerable<SongSearchResult> searchResults = await client.SearchAsync<SongSearchResult>(query);

foreach (SongSearchResult song in searchResults)
  Console.WriteLine($"{song.Name}, {string.Join(", ", song.Artists.Select(artist => artist.Name))} - {song.Album.Name}");
// Search for the songs using shelves
IEnumerable<Shelf> shelves = await client.SearchAsync(query, null, YouTubeMusicItemKind.Songs);

foreach (Shelf shelf in shelves)
{
  Console.WriteLine($"{shelf.Kind}: Next Continuation Token-{shelf.NextContinuationToken}");

  foreach (IYouTubeMusicItem item in shelf.Items)
  {
    SongSearchResult song = (SongSearchResult)item;
    Console.WriteLine($"{song.Name}, {string.Join(", ", song.Artists.Select(artist => artist.Name))} - {song.Album.Name}");
  }
}

// Search for all kinds directly
IEnumerable<IYouTubeMusicItem> searchResults = await client.SearchAsync<IYouTubeMusicItem>(query, limit);

foreach (IYouTubeMusicItem item in searchResults)
  Console.WriteLine($"{item.Kind}: {item.Name} - {item.Id}");
// Search for shelves including all kinds
IEnumerable<Shelf> shelves = await client.SearchAsync(query, null, null);

foreach (Shelf shelf in shelves)
{
  Console.WriteLine($"{shelf.Kind}: Next Continuation Token-{shelf.NextContinuationToken}");

  foreach (IYouTubeMusicItem item in shelf.Items)
    Console.WriteLine($"{item.Kind}: {item.Name} - {item.Id}");
}

Getting more information

// Song/Video
SongVideoInfo info = await client.GetSongVideoInfoAsync(id);

Console.WriteLine($"{info.Name}, {string.Join(", ", info.Artists.Select(artist => artist.Name))} - {info.Description}");
// Album
string browseId = await client.GetAlbumBrowseIdAsync(id);
AlbumInfo info = await client.GetAlbumInfoAsync(browseId);

foreach (AlbumSongInfo song in info.Songs)
  Console.WriteLine($"{song.Name}, {song.SongNumber}/{info.SongCount}");
// Community Playlist
string browseId = client.GetCommunityPlaylistBrowseId(id);
CommunityPlaylistInfo info = await client.GetCommunityPlaylistInfoAsync(browseId);

foreach (CommunityPlaylistSongInfo song in info.Songs)
  Console.WriteLine($"{song.Name}, {string.Join(", ", song.Artists.Select(artist => artist.Name))} - {song.Album?.Name}");
// Artist
ArtistInfo info = await client.GetArtistInfoAsync(id);

foreach (ArtistSongInfo song in info.Songs)
  Console.WriteLine($"{song.Name}, {string.Join(", ", song.Artists.Select(artist => artist.Name))} - {song.Album?.Name}");

Streaming & Downloading

In some cases (e.g. when using cookie authentication), getting streaming URLs requires deciphering. Sometimes, YouTube also enforces a "Proof of Origin Token" (PoToken), which is generated by an Anti-Bot-System.

PoToken generation requires JavaScript execution and a full DOM, which isn't feasible in a pure .NET environment. While JavaScript execution can be handled using interpreters like Jint, a full browser engine (e.g. Puppeteer, CefSharp) is required to generate the token.

To avoid this, PoTokens are not automatically generated by this library. However, you can manually provide a valid token using the YouTubeMusicClient constructor when necessary. For more details on PoTokens and how to generate them, check out this guide.

// Getting streaming data of a song
StreamingData streamingData = await client.GetStreamingDataAsync(id);

Console.WriteLine($"Expires in: {streamingData.ExpiresIn}");
Console.WriteLine($"Hls Manifest URL: {streamingData.HlsManifestUrl}");

foreach(MediaStreamInfo streamInfo in streamingData.StreamInfo)
{
  if (streamInfo is VideoStreamInfo videoStreamInfo)
    Console.WriteLine($"Video ({videoStreamInfo.Quality}): {videoStreamInfo.Url}");
  else if (streamInfo is AudioStreamInfo audioStreamInfo)
    Console.WriteLine($"Audio ({audioStreamInfo.SampleRate}): {audioStreamInfo.Url}");
}
// Download highest quality audio stream
StreamingData streamingData = await client.GetStreamingDataAsync(id);

MediaStreamInfo highestAudioStreamInfo = streamingData.StreamInfo
  .OfType<AudioStreamInfo>()
  .OrderByDescending(info => info.Bitrate)
  .First();
Stream stream = await highestAudioStreamInfo.GetStreamAsync();

using FileStream fileStream = new("audio.m4a", FileMode.Create, FileAccess.Write);
await stream.CopyToAsync(fileStream);

Access to a user's library

In order to access a user's library you first need to authenticate the user by using pre-authenticated cookies.

To obtain the necessary cookies, you'll need to present the official YouTube Music login page to your users. You could use an embedded browser like WebView2 in your application and then extract the cookies for the site ".youtube.com".

// Get saved/created community playlists
IEnumerable<LibraryCommunityPlaylist> communityPlaylists = await client.GetLibraryCommunityPlaylistsAsync();

foreach (LibraryCommunityPlaylist playlist in communityPlaylists)
  Console.WriteLine($"{playlist.Name}, {playlist.SongCount} songs");
// Get saved songs
IEnumerable<LibrarySong> songs = await client.GetLibrarySongsAsync();

foreach (LibrarySong song in songs)
  Console.WriteLine($"{song.Name}, {string.Join(", ", song.Artists.Select(artist => artist.Name))} - {song.Album.Name}");
// Get saved albums
IEnumerable<LibraryAlbum> albums = await client.GetLibraryAlbumsAsync();

foreach (LibraryAlbum album in albums)
  Console.WriteLine($"{album.Name}, {album.ReleaseYear}");
// Get artists with saved songs
IEnumerable<LibraryArtist> artists = await client.GetLibraryArtistsAsync();

foreach (LibraryArtist artist in artists)
  Console.WriteLine($"{artist.Name}, {artist.SongCount} saved songs");
// Get subscribed artists
IEnumerable<LibrarySubscription> subscriptions = await client.GetLibrarySubscriptionsAsync();

foreach (LibrarySubscription subscription in subscriptions)
  Console.WriteLine($"{subscription.Name}, {subscription.SubscribersInfo}");
// Get saved podcasts
IEnumerable<LibraryPodcast> podcasts = await client.GetLibraryPodcastsAsync();

foreach (LibraryPodcast podcast in podcasts)
  Console.WriteLine($"{podcast.Name}, {podcast.Host.Name}");

License

This project is licensed under the GNU General Public License v3.0. See the LICENSE file for details.


Contact

For questions, suggestions or problems, please open an issue with a detailed description.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.2.2 80 2/14/2025
2.2.1 76 2/7/2025
2.2.0 170 1/11/2025
2.1.1 177 1/5/2025
2.1.0 127 1/1/2025
2.0.0 144 12/21/2024
1.3.1 144 9/29/2024
1.3.0 153 8/13/2024
1.2.0 121 8/11/2024
1.1.4 128 7/20/2024
1.1.3 114 7/14/2024
1.1.2 126 7/4/2024
1.1.1 121 7/4/2024
1.1.0 126 7/4/2024
1.0.1 125 6/5/2024
1.0.0 115 6/5/2024

- Fixed Streaming URL's n-signature deciphering
- Fixed parsing issues for Library Community Playlists when authenticated has YT Premium