ytmusicapi package

Subpackages

Submodules

ytmusicapi.constants module

ytmusicapi.continuations module

ytmusicapi.continuations.get_continuation_contents(continuation, parse_func)
ytmusicapi.continuations.get_continuation_params(results, ctoken_path='')
ytmusicapi.continuations.get_continuation_string(ctoken)
ytmusicapi.continuations.get_continuation_token(results: list[dict[str, Any]]) str | None
Return type:

Optional[str]

ytmusicapi.continuations.get_continuations(results, continuation_type, limit, request_func, parse_func, ctoken_path='', reloadable=False)
ytmusicapi.continuations.get_continuations_2025(results, limit, request_func, parse_func)
ytmusicapi.continuations.get_parsed_continuation_items(response, parse_func, continuation_type)
ytmusicapi.continuations.get_reloadable_continuation_params(results)
ytmusicapi.continuations.get_validated_continuations(results, continuation_type, limit, per_page, request_func, parse_func, ctoken_path='')
ytmusicapi.continuations.resend_request_until_parsed_response_is_valid(request_func, request_additional_params, parse_func, validate_func, max_retries)
ytmusicapi.continuations.validate_response(response, per_page, limit, current_count)

ytmusicapi.enums module

class ytmusicapi.enums.ResponseStatus(value)

Bases: str, Enum

An enumeration.

SUCCEEDED = 'STATUS_SUCCEEDED'

ytmusicapi.exceptions module

custom exception classes for ytmusicapi

exception ytmusicapi.exceptions.YTMusicError

Bases: Exception

base error class

shall only be raised if none of the subclasses below are fitting

exception ytmusicapi.exceptions.YTMusicServerError

Bases: YTMusicError

error caused by the YouTube Music backend

exception ytmusicapi.exceptions.YTMusicUserError

Bases: YTMusicError

error caused by invalid usage of ytmusicapi

ytmusicapi.helpers module

ytmusicapi.helpers.get_authorization(auth)
ytmusicapi.helpers.get_visitor_id(request_func)
ytmusicapi.helpers.initialize_context()
ytmusicapi.helpers.initialize_headers()
ytmusicapi.helpers.sum_total_duration(item)
ytmusicapi.helpers.to_int(string)

ytmusicapi.navigation module

commonly used navigation paths

ytmusicapi.navigation.find_object_by_key(object_list, key, nested=None, is_key=False)
ytmusicapi.navigation.find_objects_by_key(object_list, key, nested=None)
ytmusicapi.navigation.nav(root: dict, items: list[Any], none_if_absent: bool = False) Any | None

Access a nested object in root by item sequence.

Return type:

Optional[Any]

ytmusicapi.setup module

ytmusicapi.setup.main() ytmusicapi.auth.oauth.token.RefreshingToken | str
Return type:

Union[RefreshingToken, str]

ytmusicapi.setup.parse_args(args)
ytmusicapi.setup.setup(filepath: str | None = None, headers_raw: str | None = None) str

Requests browser headers from the user via command line and returns a string that can be passed to YTMusic()

Parameters:
  • filepath (Optional[str]) – Optional filepath to store headers to.

  • headers_raw (Optional[str]) – Optional request headers copied from browser. Otherwise requested from terminal

Return type:

str

Returns:

configuration headers string

ytmusicapi.setup.setup_oauth(client_id: str, client_secret: str, filepath: str | None = None, session: requests.sessions.Session | None = None, proxies: dict | None = None, open_browser: bool = False) ytmusicapi.auth.oauth.token.RefreshingToken

Starts oauth flow from the terminal and returns a string that can be passed to YTMusic()

Parameters:
  • client_id (str) – Optional. Used to specify the client_id oauth should use for authentication flow. If provided, client_secret MUST also be passed or both will be ignored.

  • client_secret (str) – Optional. Same as client_id but for the oauth client secret.

  • session (Optional[Session]) – Session to use for authentication

  • proxies (Optional[dict]) – Proxies to use for authentication

  • filepath (Optional[str]) – Optional filepath to store headers to.

  • open_browser (bool) – If True, open the default browser with the setup link

Return type:

RefreshingToken

Returns:

configuration headers string

ytmusicapi.ytmusic module

class ytmusicapi.ytmusic.YTMusic(auth: str | dict | None = None, user: str | None = None, requests_session: requests.sessions.Session | None = None, proxies: dict[str, str] | None = None, language: str = 'en', location: str = '', oauth_credentials: ytmusicapi.auth.oauth.credentials.OAuthCredentials | None = None)

Bases: YTMusicBase, BrowsingMixin, SearchMixin, WatchMixin, ExploreMixin, LibraryMixin, PlaylistsMixin, PodcastsMixin, UploadsMixin

Allows automated interactions with YouTube Music by emulating the YouTube web client’s requests. Permits both authenticated and non-authenticated requests. Authentication header data must be provided on initialization.

class ytmusicapi.ytmusic.YTMusicBase(auth: str | dict | None = None, user: str | None = None, requests_session: requests.sessions.Session | None = None, proxies: dict[str, str] | None = None, language: str = 'en', location: str = '', oauth_credentials: ytmusicapi.auth.oauth.credentials.OAuthCredentials | None = None)

Bases: object

as_mobile() collections.abc.Iterator[None]
Return type:

Iterator[None]

Not thread-safe!

Temporarily changes the context to enable different results from the API, meant for the Android mobile-app. All calls inside the with-statement with emulate mobile behavior.

This context-manager has no enter_result, as it operates in-place and only temporarily alters the underlying YTMusic-object.

Example:

with yt.as_mobile():
    yt._send_request(...)  # results as mobile-app

yt._send_request(...)  # back to normal, like web-app
property base_headers: CaseInsensitiveDict
property headers: CaseInsensitiveDict
proxies: Optional[dict[str, str]]

params for session modification

Module contents

class ytmusicapi.OAuthCredentials(client_id: str, client_secret: str, session: requests.sessions.Session | None = None, proxies: dict | None = None)

Bases: Credentials

Class for handling OAuth credential retrieval and refreshing.

client_id: str
client_secret: str
get_code() ytmusicapi.auth.oauth.models.AuthCodeDict

Method for obtaining a new user auth code. First step of token creation.

Return type:

AuthCodeDict

refresh_token(refresh_token: str) ytmusicapi.auth.oauth.models.BaseTokenDict

Method for requesting a new access token for a given refresh_token. Token must have been created by the same OAuth client.

Parameters:

refresh_token (str) – Corresponding refresh_token for a matching access_token. Obtained via

Return type:

BaseTokenDict

token_from_code(device_code: str) ytmusicapi.auth.oauth.models.RefreshableTokenDict

Method for verifying user auth code and conversion into a FullTokenDict.

Return type:

RefreshableTokenDict

class ytmusicapi.YTMusic(auth: str | dict | None = None, user: str | None = None, requests_session: requests.sessions.Session | None = None, proxies: dict[str, str] | None = None, language: str = 'en', location: str = '', oauth_credentials: ytmusicapi.auth.oauth.credentials.OAuthCredentials | None = None)

Bases: YTMusicBase, BrowsingMixin, SearchMixin, WatchMixin, ExploreMixin, LibraryMixin, PlaylistsMixin, PodcastsMixin, UploadsMixin

Allows automated interactions with YouTube Music by emulating the YouTube web client’s requests. Permits both authenticated and non-authenticated requests. Authentication header data must be provided on initialization.

ytmusicapi.setup(filepath: str | None = None, headers_raw: str | None = None) str

Requests browser headers from the user via command line and returns a string that can be passed to YTMusic()

Parameters:
  • filepath (Optional[str]) – Optional filepath to store headers to.

  • headers_raw (Optional[str]) – Optional request headers copied from browser. Otherwise requested from terminal

Return type:

str

Returns:

configuration headers string

ytmusicapi.setup_oauth(client_id: str, client_secret: str, filepath: str | None = None, session: requests.sessions.Session | None = None, proxies: dict | None = None, open_browser: bool = False) ytmusicapi.auth.oauth.token.RefreshingToken

Starts oauth flow from the terminal and returns a string that can be passed to YTMusic()

Parameters:
  • client_id (str) – Optional. Used to specify the client_id oauth should use for authentication flow. If provided, client_secret MUST also be passed or both will be ignored.

  • client_secret (str) – Optional. Same as client_id but for the oauth client secret.

  • session (Optional[Session]) – Session to use for authentication

  • proxies (Optional[dict]) – Proxies to use for authentication

  • filepath (Optional[str]) – Optional filepath to store headers to.

  • open_browser (bool) – If True, open the default browser with the setup link

Return type:

RefreshingToken

Returns:

configuration headers string