31 releases
Uses new Rust 2024
| new 0.3.1 | Feb 9, 2026 |
|---|---|
| 0.2.4 | Jan 13, 2026 |
| 0.2.3 | Dec 22, 2025 |
| 0.2.1 | Nov 20, 2025 |
| 0.0.1 | Dec 3, 2023 |
#7 in #youtube-api
297 downloads per month
Used in youtui
505KB
11K
SLoC
About
Ytmapi-rs - an asynchronous API for youtube music - using Google's internal API, Tokio and Reqwest. Inspired by https://github.com/sigma67/ytmusicapi/.
This project is not supported or endorsed by Google.
Please refer to docs.rs for documentation and usage examples.
lib.rs:
ytmapi_rs
Library into YouTube Music's internal API.
Examples
For additional examples using builder, see builder module.
Unauthenticated usage - note, not all queries supported.
#[tokio::main]
pub async fn main() -> Result<(), ytmapi_rs::Error> {
let yt = ytmapi_rs::YtMusic::new_unauthenticated().await?;
yt.get_search_suggestions("Beatles").await?;
let result = yt.get_search_suggestions("Beatles").await?;
println!("{:?}", result);
Ok(())
}
Basic authenticated usage with a pre-created cookie file, demonstrating uploading a song.
#[tokio::main]
pub async fn main() -> Result<(), ytmapi_rs::Error> {
let cookie_path = std::path::Path::new("./cookie.txt");
let yt = ytmapi_rs::YtMusic::from_cookie_file(cookie_path).await?;
yt.get_search_suggestions("Beatles").await?;
let result = yt.get_search_suggestions("Beatles").await?;
println!("{:?}", result);
assert_eq!(
yt.upload_song("my_song_to_upload.mp3").await.unwrap(),
ytmapi_rs::common::ApiOutcome::Success
);
Ok(())
}
OAuth authenticated usage, using the workflow, and builder method to re-use the Client.
#[tokio::main]
pub async fn main() -> Result<(), ytmapi_rs::Error> {
let client = ytmapi_rs::Client::new().unwrap();
// A Client ID and Client Secret must be provided - see `youtui` README.md.
// In this example, I assume they were put in environment variables beforehand.
let client_id = std::env::var("YOUTUI_OAUTH_CLIENT_ID").unwrap();
let client_secret = std::env::var("YOUTUI_OAUTH_CLIENT_SECRET").unwrap();
let (code, url) = ytmapi_rs::generate_oauth_code_and_url(&client, &client_id).await?;
println!("Go to {url}, finish the login flow, and press enter when done");
let mut _buf = String::new();
let _ = std::io::stdin().read_line(&mut _buf);
let token =
ytmapi_rs::generate_oauth_token(&client, code, client_id, client_secret).await?;
// NOTE: The token can be re-used until it expires, and refreshed once it has,
// so it's recommended to save it to a file here.
let yt = ytmapi_rs::YtMusicBuilder::new_with_client(client)
.with_auth_token(token)
.build()
.unwrap();
let result = yt.get_search_suggestions("Beatles").await?;
println!("{:?}", result);
Ok(())
}
Optional Features
TLS
NOTE: reqwest will prefer to utilise default-tls if multiple features are
built when using the standard constructors. Use YtMusicBuilder to ensure
the preferred choice of TLS is used. See reqwest docs for more information https://docs.rs/reqwest/latest/reqwest/tls/index.html.
- default-tls (enabled by default): Utilises the default TLS from reqwest - at the time of writing is native-tls.
- native-tls: This feature allows use of the the native-tls crate, reliant on vendors tls.
- rustls: This feature allows use of the rustls crate, written in rust.
Other
- simplified_queries: Adds convenience methods to
YtMusic. - serde_json: Enables some interoperability functions with
serde_json. - reqwest: Enables some interoperability functions with
reqwest.
Dependencies
~9–27MB
~353K SLoC