Setup¶
Installation¶
pip install ytmusicapi
Authenticated requests¶
Copy authentication headers¶
To run authenticated requests you need to set up you need to copy your request headers from a POST request in your browser. To do so, follow these steps:
- Open a new tab
- Open the developer tools (Ctrl-Shift-I) and select the “Network” tab
- Go to https://music.youtube.com and ensure you are logged in
- Find an authenticated POST request. The simplest way is to filter by
/browse
using the search bar
Firefox
- Verify that the request looks like this: Status 200, Method POST, Domain music.youtube.com, File
browse?...
- Copy the request headers (right click > copy > copy request headers)
Chromium (Chrome/Edge)
- Verify that the request looks like this: Status 200, Type xhr, Name
browse?...
- Click on the Name of any matching request. In the “Headers” tab, scroll to the section “Request headers” and copy everything starting from “accept: */*” to the end of the section
Using the headers in your project¶
To set up your project, open a Python console and call YTMusic.setup()
with the parameter filepath=headers_auth.json
and follow the instructions and paste the request headers to the terminal input:
from ytmusicapi import YTMusic
YTMusic.setup(filepath=headers_auth.json)
If you don’t want terminal interaction in your project, you can pass the request headers with the headers_raw
parameter:
from ytmusicapi import YTMusic
YTMusic.setup(filepath=headers_auth.json, headers_raw="<headers copied above>")
The function returns a JSON string with the credentials needed for Usage. Alternatively, if you passed the filepath parameter as described above,
a file called headers_auth.json
will be created in the current directory, which you can pass to YTMusic()
for authentication.
These credentials remain valid as long as your YTMusic browser session is valid (about 2 years unless you log out).
Manual file creation¶
Alternatively, you can paste the cookie to headers_auth.json
below and create your own file:
{
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0",
"Accept": "*/*",
"Accept-Language": "en-US,en;q=0.5",
"Content-Type": "application/json",
"X-Goog-AuthUser": "0",
"x-origin": "https://music.youtube.com",
"Cookie" : "PASTE_COOKIE"
}