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):
- Open YouTube Music.
- Open Developer Tools (F12) > Network tab.
- Copy request headers from a
browsecall and save asheaders.json:{"cookie": "YOUR_COOKIE_STRING", "x-goog-authuser": "0"}
- OAuth device flow:
- Create a YouTube Data API OAuth client (TVs and Limited Input devices).
- Use
OAuthCredentials::request_device_codeto get auser_codeandverification_url. - After completing the browser flow, call
exchange_device_codeand 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.
- Browser
Auth - Browser-based authentication using cookies from a YouTube Music session.
- Device
Code - Device-code response returned by Google’s OAuth device flow.
- Microformat
- Microformat
Data Renderer - OAuth
Credentials - OAuth client credentials.
- OAuth
Token - OAuth token representation (access + optional refresh).
- Playlist
- Full playlist with tracks.
- Playlist
Summary - Summary info for a playlist in a library listing.
- Playlist
Track - A track within a playlist.
- Song
- Thumbnail
- A thumbnail image.
- Video
Details - YTMusic
Client - The main YouTube Music API client.
- YTMusic
Client Builder - 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.