Crate ytmusicapi

Crate ytmusicapi 

Source
Expand description

§ytmusicapi: A Rust client for the YouTube Music API

This library provides a Rust interface to the YouTube Music API (YouTubei), ported from the popular Python ytmusicapi library.

§Features

  • Browsing: Fetch playlists, library tracks, and playlist details.
  • Authentication: Browser-cookie or OAuth-based authentication (required for library access).
  • Robust: Handles various API response formats (premium vs free, grid vs list).

Note: Currently focused on read-only operations (reading playlists/library).

§Installation

Add to your Cargo.toml:

[dependencies]
ytmusicapi = "0.1.0"

§Authentication

Two options are supported:

  • Browser cookies (matches the Python library default):
    1. Open YouTube Music.
    2. Open Developer Tools (F12) > Network tab.
    3. Copy request headers from a browse call and save as headers.json:
      {"cookie": "YOUR_COOKIE_STRING", "x-goog-authuser": "0"}
  • OAuth device flow:
    1. Create a YouTube Data API OAuth client (TVs and Limited Input devices).
    2. Use OAuthCredentials::request_device_code to get a user_code and verification_url.
    3. After completing the browser flow, call exchange_device_code and save the token JSON.

§Example (browser cookies)

use ytmusicapi::{YTMusicClient, BrowserAuth};

let auth = BrowserAuth::from_file("headers.json")?;
let client = YTMusicClient::builder()
    .with_browser_auth(auth)
    .build()?;

let playlists = client.get_library_playlists(None).await?;
for pl in playlists {
    println!("{} ({})", pl.title, pl.count.unwrap_or(0));
}

Macros§

path
Macro for creating a path from mixed key/index values.

Structs§

Album
An album reference.
Artist
An artist reference.
Author
Author of a playlist.
BrowserAuth
Browser-based authentication using cookies from a YouTube Music session.
DeviceCode
Device-code response returned by Google’s OAuth device flow.
Microformat
MicroformatDataRenderer
OAuthCredentials
OAuth client credentials.
OAuthToken
OAuth token representation (access + optional refresh).
Playlist
Full playlist with tracks.
PlaylistSummary
Summary info for a playlist in a library listing.
PlaylistTrack
A track within a playlist.
Song
Thumbnail
A thumbnail image.
VideoDetails
YTMusicClient
The main YouTube Music API client.
YTMusicClientBuilder
Builder for constructing a YTMusicClient.

Enums§

Auth
Authentication strategies supported by the client.
Error
The error type for YouTube Music API operations.
Privacy
Privacy status of a playlist.

Type Aliases§

Result
A specialized Result type for YouTube Music API operations.