Using Spotify API with requests
in Python. It makes life easier.
https://developer.spotify.com/documentation/web-api/reference/#/operations/get-playlist
GET https://api.spotify.com/v1/playlists/{playlist_id}

Python Code as get-playlist.py
file.
import requests
CLIENT_ID = '*************** your client ID ***************'
CLIENT_SECRET = '*********** your client secret ***********'
AUTH_URL = 'https://accounts.spotify.com/api/token'
BASE_URL = 'https://api.spotify.com/v1/'
playlist_id = '37i9dQZF1DX2Zjg8kBuEsQ'
# POST
auth_response = requests.post(AUTH_URL, {
'grant_type': 'client_credentials',
'client_id': CLIENT_ID,
'client_secret': CLIENT_SECRET,
})
# convert the response to JSON
auth_response_data = auth_response.json()
# save the access token
access_token = auth_response_data['access_token']
# print(access_token)
headers = {
'Authorization': 'Bearer {token}'.format(token=access_token)
}
# actual GET request with proper header
r = requests.get(BASE_URL + 'playlists/' + playlist_id, headers=headers)
r = r.json()
tracks = []
for item in r['tracks']['items']:
tracks.append({
'title' : item['track']['album']['name'],
'artists' : item['track']['album']['artists'][0]['name'],
'duration_ms' : item['track']['duration_ms'],
'track URL' : item['track']['album']['external_urls']['spotify'],
})
for track in tracks:
print(track)
Result
$ python get-playlist.py
{'title': 'Special', 'artists': 'Lizzo', 'duration_ms': 191937, 'track URL': 'https://open.spotify.com/album/1KtDsGsSRGbnmH07v5hB1I'}
{'title': 'Doja', 'artists': 'Central Cee', 'duration_ms': 97392, 'track URL': 'https://open.spotify.com/album/6oECjagksATHu2UaclXrq1'}
{'title': 'B.O.T.A. (Baddest Of Them All)', 'artists': 'Eliza Rose', 'duration_ms': 226626, 'track URL': 'https://open.spotify.com/album/2lQgd3Svp1ZWAzZPLobAPK'}
{'title': 'Come Home The Kids Miss You', 'artists': 'Jack Harlow', 'duration_ms': 173947, 'track URL': 'https://open.spotify.com/album/2eE8BVirX9VF8Di9hD90iw'}
{'title': 'Hounds of Love (2018 Remaster)', 'artists': 'Kate Bush', 'duration_ms': 300840, 'track URL': 'https://open.spotify.com/album/3OYnManu1Nlxnw9OMng7BH'}
{'title': 'Afraid To Feel', 'artists': 'LF SYSTEM', 'duration_ms': 177524, 'track URL': 'https://open.spotify.com/album/528LrHfHcB7PMAvyp8Obhp'}
{'title': 'Calm Down (with Selena Gomez)', 'artists': 'Rema', 'duration_ms': 239317, 'track URL': 'https://open.spotify.com/album/2b2GHWESCWEuHiCZ2Skedp'}
{'title': 'Go (Remixes)', 'artists': 'Cat Burns', 'duration_ms': 192514, 'track URL': 'https://open.spotify.com/album/1kGbwOHke3PrRnZAmlkoOu'}
{'title': 'Finesse', 'artists': 'Pheelz', 'duration_ms': 155293, 'track URL': 'https://open.spotify.com/album/4QEC4uzBJJfLVv2bD337g1'}
{'title': "Takin' It Back", 'artists': 'Meghan Trainor', 'duration_ms': 134256, 'track URL': 'https://open.spotify.com/album/4LVa9bljQRvLYpWr8qyaXs'}
{'title': 'No Excuses', 'artists': 'Bru-C', 'duration_ms': 222453, 'track URL': 'https://open.spotify.com/album/1r6fzHMhZcd35ZkpKPRRWE'}
{'title': 'RENAISSANCE', 'artists': 'Beyonc▒', 'duration_ms': 225388, 'track URL': 'https://open.spotify.com/album/6FJxoadUE4JNVwWHghBwnb'}
{'title': 'Gemini Rights', 'artists': 'Steve Lacy', 'duration_ms': 232066, 'track URL': 'https://open.spotify.com/album/3Ks0eeH0GWpY4AU20D5HPD'}
{'title': 'Packs and Potions', 'artists': 'HAZEY', 'duration_ms': 160551, 'track URL': 'https://open.spotify.com/album/1VkcWtwCE0ktxyiZqjkCzu'}
{'title': 'messy in heaven', 'artists': 'venbee', 'duration_ms': 170437, 'track URL': 'https://open.spotify.com/album/0a9uNlopPXGg37OC20qDk6'}
{'title': 'KU LO SA - A COLORS SHOW', 'artists': 'Oxlade', 'duration_ms': 147580, 'track URL': 'https://open.spotify.com/album/36bNKiiUjxUCaAO7QtUVfi'}
{'title': 'BMW', 'artists': 'Bad Boy Chiller Crew', 'duration_ms': 213880, 'track URL': 'https://open.spotify.com/album/3U3FSuGS2CXktQD4fU78gM'}
{'title': 'Victoria▒s Secret', 'artists': 'Jax', 'duration_ms': 176124, 'track URL': 'https://open.spotify.com/album/47eeG5ewIbO53gXZOkvHhg'}
{'title': 'Euphoria (Original Score from the HBO Series)', 'artists': 'Labrinth', 'duration_ms': 91869, 'track URL': 'https://open.spotify.com/album/788r22Bhr3IHLTNPy8pZea'}
{'title': "Je M'appelle", 'artists': 'Benzz', 'duration_ms': 139574, 'track URL': 'https://open.spotify.com/album/6T6uWGOSAUhvoZfLlIyDuJ'}
{'title': 'I Ain▒t Worried (Music From The Motion Picture "Top Gun: Maverick")', 'artists': 'OneRepublic', 'duration_ms': 148485, 'track URL': 'https://open.spotify.com/album/04PEOM6kIEeq9lRp1asNP2'}
{'title': 'Boyfriend', 'artists': 'Dove Cameron', 'duration_ms': 153000, 'track URL': 'https://open.spotify.com/album/4jUfPmvZGiRRJwULbfk1dc'}
{'title': 'Glad U Came (feat. ZieZie)', 'artists': 'Liilz', 'duration_ms': 125142, 'track URL': 'https://open.spotify.com/album/52JvEc5EDwgA854xVckQL9'}
{'title': 'Jiggle Jiggle', 'artists': 'Duke & Jones', 'duration_ms': 97208, 'track URL': 'https://open.spotify.com/album/3WcZOQwZQ5gLyNrA0aXUeT'}
{'title': 'Fat Funny Friend', 'artists': 'Maddie Zahm', 'duration_ms': 199764, 'track URL': 'https://open.spotify.com/album/5NMKRqxbne8L5LerDnpGjk'}
{'title': 'Big City Life', 'artists': 'Luude', 'duration_ms': 146711, 'track URL': 'https://open.spotify.com/album/5Z9UyG6AYfNphE7UJGXifC'}
{'title': 'Sunroof', 'artists': 'Nicky Youre', 'duration_ms': 163025, 'track URL': 'https://open.spotify.com/album/0VaHnwzDug4AcDkejYDUl5'}
{'title': 'Friendly Sex', 'artists': 'Caity Baser', 'duration_ms': 159193, 'track URL': 'https://open.spotify.com/album/3KgVXRJUmbAS28XfHrJOs2'}
{'title': 'Safe Haven', 'artists': 'Ruth B.', 'duration_ms': 233720, 'track URL': 'https://open.spotify.com/album/6FgtuX3PtiB5civjHYhc52'}
{'title': 'channel ORANGE', 'artists': 'Frank Ocean', 'duration_ms': 234093, 'track URL': 'https://open.spotify.com/album/392p3shh2jkxUxY2VHvlH8'}
{'title': 'Miss You', 'artists': 'Oliver Tree', 'duration_ms': 206000, 'track URL': 'https://open.spotify.com/album/32G4vFNwLJQjpzkOoGEUUo'}
{'title': 'Super Freaky Girl', 'artists': 'Nicki Minaj', 'duration_ms': 170977, 'track URL': 'https://open.spotify.com/album/0h5MuD9O9o1VoN07mQmwMQ'}
{'title': 'BILLIE EILISH.', 'artists': 'Armani White', 'duration_ms': 99282, 'track URL': 'https://open.spotify.com/album/4MajX5vPeY4cX5pv6rf0sA'}
{'title': '10 Things I Hate About You', 'artists': 'Leah Kate', 'duration_ms': 157138, 'track URL': 'https://open.spotify.com/album/26hYEKjKZU1sTC4mUPpVI6'}
{'title': 'LA Leakers Freestyle', 'artists': 'Central Cee', 'duration_ms': 178000, 'track URL': 'https://open.spotify.com/album/5BdTpc5WQMyt0qBuJ5mDCH'}
{'title': 'Betty (Get Money)', 'artists': 'Yung Gravy', 'duration_ms': 146470, 'track URL': 'https://open.spotify.com/album/3FEQZNn34v3EdxrVKi9pZF'}
{'title': 'Rainfall (Praise You)', 'artists': 'Tom Santa', 'duration_ms': 166569, 'track URL': 'https://open.spotify.com/album/4VanY5i4E59Mhz52qznJ95'}
{'title': 'froge.mp3', 'artists': 'piri', 'duration_ms': 135386, 'track URL': 'https://open.spotify.com/album/4AueWk2dGXqbMFx7ogEAs7'}
{'title': 'IFTK', 'artists': 'Tion Wayne', 'duration_ms': 190684, 'track URL': 'https://open.spotify.com/album/6IZ5CNSuwvcAnmnxfmVvl5'}
{'title': 'Ivory', 'artists': 'Omar Apollo', 'duration_ms': 216680, 'track URL': 'https://open.spotify.com/album/5z7TD11Qh81Gbf52hd5zAv'}
{'title': 'Drunk', 'artists': 'Thundercat', 'duration_ms': 188453, 'track URL': 'https://open.spotify.com/album/7vHBQDqwzB7uDvoE5bncMM'}
{'title': 'Special', 'artists': 'Lizzo', 'duration_ms': 187107, 'track URL': 'https://open.spotify.com/album/1NgFBv1PxMG1zhFDW1OrRr'}
{'title': "Harry's House", 'artists': 'Harry Styles', 'duration_ms': 193813, 'track URL': 'https://open.spotify.com/album/5r36AJ6VOJtp00oxSkBZ5h'}
{'title': 'reputation', 'artists': 'Taylor Swift', 'duration_ms': 236413, 'track URL': 'https://open.spotify.com/album/6DEjYFkNZh67HP7R9PSZvv'}
{'title': 'SPACE MAN', 'artists': 'Sam Ryder', 'duration_ms': 217320, 'track URL': 'https://open.spotify.com/album/7uJgYMaJcQ25PhywdJfrJF'}
{'title': 'Vegas (From the Original Motion Picture Soundtrack ELVIS)', 'artists': 'Doja Cat', 'duration_ms': 182906, 'track URL': 'https://open.spotify.com/album/2Q5DPv9uliinOBSdNooIe3'}
{'title': 'Ronaldo (SEWY)', 'artists': 'IShowSpeed', 'duration_ms': 102019, 'track URL': 'https://open.spotify.com/album/1HGtrBJ0dCm0FmgLxBBo8P'}
{'title': "Tell Me It's Real (Expanded Edition)", 'artists': 'Seafret', 'duration_ms': 229173, 'track URL': 'https://open.spotify.com/album/4m8XN9CKqve1ExYBnNu5kt'}
{'title': 'low down', 'artists': 'venbee', 'duration_ms': 182923, 'track URL': 'https://open.spotify.com/album/48KWh33PB86WJeyFhQOkVl'}
{'title': 'Chit Chat', 'artists': 'Beach Weather', 'duration_ms': 196784, 'track URL': 'https://open.spotify.com/album/1xz1n7gyY02veDxH50SQHQ'}
{'title': 'Starboy', 'artists': 'The Weeknd', 'duration_ms': 260253, 'track URL': 'https://open.spotify.com/album/2ODvWsOgouMbaA5xf0RkJe'}
{'title': 'Next Up - S4-E2', 'artists': 'Mixtape Madness', 'duration_ms': 120839, 'track URL': 'https://open.spotify.com/album/0j070iW9OV0gL2IbSWIHcE'}
{'title': 'RENAISSANCE', 'artists': 'Beyonc▒', 'duration_ms': 278281, 'track URL': 'https://open.spotify.com/album/6FJxoadUE4JNVwWHghBwnb'}
{'title': 'Pretty One', 'artists': 'Koomz', 'duration_ms': 182169, 'track URL': 'https://open.spotify.com/album/1xn5iNKJUkVV5qC6y6GEXS'}
{'title': "It's Not Me, It's You", 'artists': 'Lily Allen', 'duration_ms': 219893, 'track URL': 'https://open.spotify.com/album/39zjAZD6nlscx5DafH8GI8'}
{'title': 'Glimpse of Us', 'artists': 'Joji', 'duration_ms': 233456, 'track URL': 'https://open.spotify.com/album/6ZZvx0aefZV3LKa053fn71'}
{'title': 'Down Under (feat. Colin Hay)', 'artists': 'Luude', 'duration_ms': 158774, 'track URL': 'https://open.spotify.com/album/64xmSwJJbFQMf63AFYZXpW'}
{'title': 'Anthology', 'artists': 'Musical Youth', 'duration_ms': 205426, 'track URL': 'https://open.spotify.com/album/7EaFCudrleGdoYFl2srjhi'}
{'title': 'Wet Dream', 'artists': 'Wet Leg', 'duration_ms': 140083, 'track URL': 'https://open.spotify.com/album/2EVCxBosAlVsC0d7DAJXCA'}
{'title': 'Turn On The Lights again.. (feat. Future)', 'artists': 'Fred again..', 'duration_ms': 267946, 'track URL': 'https://open.spotify.com/album/2jiMeC1iUWVYmvXSIiO8ks'}
{'title': 'Night Away (Dance) (feat. Tion Wayne)', 'artists': 'A1 x J1', 'duration_ms': 175774, 'track URL': 'https://open.spotify.com/album/6WQPgcOtZCoXGBZQOxDJQ0'}
{'title': 'Favourite Worst Nightmare (Standard Version)', 'artists': 'Arctic Monkeys', 'duration_ms': 253586, 'track URL': 'https://open.spotify.com/album/6rsQnwaoJHxXJRCDBPkBRw'}
{'title': 'Birthday Cake', 'artists': 'Dylan Conrique', 'duration_ms': 205280, 'track URL': 'https://open.spotify.com/album/6Z2I7RVroN2B24d7mms0tT'}
{'title': 'Offering', 'artists': 'Cults', 'duration_ms': 212736, 'track URL': 'https://open.spotify.com/album/1FUaSRyu5LIj1QpMXRncxt'}
{'title': 'Playboy', 'artists': 'Fireboy DML', 'duration_ms': 187111, 'track URL': 'https://open.spotify.com/album/1pUJnA3OSbvVr5afqxNARZ'}
{'title': 'SNAP', 'artists': 'Rosa Linn', 'duration_ms': 179551, 'track URL': 'https://open.spotify.com/album/4fb1QzgTJpTk9TBjFzjmlR'}
{'title': 'Until I Found You', 'artists': 'Stephen Sanchez', 'duration_ms': 177720, 'track URL': 'https://open.spotify.com/album/18CtLoAMTr7F8ngtuM6D8i'}
{'title': 'abcdefu', 'artists': 'GAYLE', 'duration_ms': 168601, 'track URL': 'https://open.spotify.com/album/6tUQPKlpR4x1gjrXTtOImI'}
{'title': 'Balling (feat. Songer, Mr Traumatik, Devilman & OneDa) [Edit]', 'artists': 'Vibe Chemistry', 'duration_ms': 266206, 'track URL': 'https://open.spotify.com/album/775Vio9oXjiTAbh7Ad15ay'}
{'title': 'Shen Yeng Anthem', 'artists': 'Shenseea', 'duration_ms': 141226, 'track URL': 'https://open.spotify.com/album/5ITcrOlnxhUg2uvlGNPJhT'}
{'title': 'Deep Down (feat. Never Dull)', 'artists': 'Alok', 'duration_ms': 165752, 'track URL': 'https://open.spotify.com/album/3KpxpdySrMR2S7noneu1bI'}
{'title': 'Beautiful Lies', 'artists': 'Birdy', 'duration_ms': 208395, 'track URL': 'https://open.spotify.com/album/5wNnopxjgSKVvHTIcBpV8Q'}
{'title': 'Ferrari', 'artists': 'James Hype', 'duration_ms': 186661, 'track URL': 'https://open.spotify.com/album/6moZ4sNThthUAwCklyuPY8'}
{'title': 'Mockingbird (Remix)', 'artists': 'E1 (3x3)', 'duration_ms': 192362, 'track URL': 'https://open.spotify.com/album/7BXb7pTQSCicF0duwhWd9E'}
{'title': 'Flowers', 'artists': 'Lauren Spencer Smith', 'duration_ms': 157373, 'track URL': 'https://open.spotify.com/album/03sQVTmOjeFVi8GsSaJSNV'}
{'title': "Harry's House", 'artists': 'Harry Styles', 'duration_ms': 245964, 'track URL': 'https://open.spotify.com/album/5r36AJ6VOJtp00oxSkBZ5h'}
{'title': 'Temptation (feat. Poppy Baskcomb)', 'artists': 'Jess Bays', 'duration_ms': 172197, 'track URL': 'https://open.spotify.com/album/30K7gYPhkqCeQJ25C0GvXE'}
{'title': 'Voulez-Vous', 'artists': 'ABBA', 'duration_ms': 260893, 'track URL': 'https://open.spotify.com/album/7iLuHJkrb9KHPkMgddYigh'}
{'title': 'Stan Smith', 'artists': 'Alcemist', 'duration_ms': 235000, 'track URL': 'https://open.spotify.com/album/2s7f4Qihh5MV7D6lKLPBjB'}
{'title': 'Poland', 'artists': 'Lil Yachty', 'duration_ms': 83200, 'track URL': 'https://open.spotify.com/album/5LZiWbqOpj6g8uxSHch12S'}
{'title': 'Diplo', 'artists': 'Diplo', 'duration_ms': 199090, 'track URL': 'https://open.spotify.com/album/5pdR4YX2zWMXotdF034UYY'}
{'title': 'Visions', 'artists': 'Grimes', 'duration_ms': 255320, 'track URL': 'https://open.spotify.com/album/3HED2IUaNSnbOe88a7ZdwM'}
{'title': 'SUPERMODEL', 'artists': 'M▒neskin', 'duration_ms': 148064, 'track URL': 'https://open.spotify.com/album/5jhbLeXH1a3SRSOg84GSUn'}
{'title': 'Satin Panthers', 'artists': 'Hudson Mohawke', 'duration_ms': 171720, 'track URL': 'https://open.spotify.com/album/0d99LxnQpiPLgSGDRuU9HT'}
{'title': 'Hold This', 'artists': 'HStikkytokky', 'duration_ms': 145920, 'track URL': 'https://open.spotify.com/album/242dSF54Qx1DKFwmHLGUGA'}
{'title': 'Oslo', 'artists': 'Lil Macks', 'duration_ms': 154906, 'track URL': 'https://open.spotify.com/album/03ti301zMUad9uUP7CGgF1'}
{'title': 'MEAN!', 'artists': 'Madeline The Person', 'duration_ms': 143960, 'track URL': 'https://open.spotify.com/album/1JX43p0bIGOMiHjdgoyAQD'}
{'title': 'Being Funny In A Foreign Language', 'artists': 'The 1975', 'duration_ms': 326490, 'track URL': 'https://open.spotify.com/album/6dVCpQ7oGJD1oYs2fv1t5M'}
{'title': 'IGOR', 'artists': 'Tyler, The Creator', 'duration_ms': 195320, 'track URL': 'https://open.spotify.com/album/5zi7WsKlIiUXv09tbGLKsE'}
{'title': 'Eye Tell (!)', 'artists': 'Jim Legxacy', 'duration_ms': 130692, 'track URL': 'https://open.spotify.com/album/1piNdNQfDq2mW2BiHC30by'}
{'title': 'SloMo', 'artists': 'Chanel', 'duration_ms': 176285, 'track URL': 'https://open.spotify.com/album/0UMDRKVU0rz2nALo2bms9d'}
{'title': 'Just Wanna Rock', 'artists': 'Lil Uzi Vert', 'duration_ms': 123890, 'track URL': 'https://open.spotify.com/album/2FD6g8bXEn2uQMYbeqqoCg'}
{'title': 'Essex Girls (feat. Jaykae, Silky & Janice Robinson)', 'artists': 'Rude Kid', 'duration_ms': 160000, 'track URL': 'https://open.spotify.com/album/1XTzHBeO7yYUnkkYN6i0V1'}
{'title': 'Mr Worldwide', 'artists': 'Pete & Bas', 'duration_ms': 181254, 'track URL': 'https://open.spotify.com/album/0UGJ7jvU1Dn14J7BCMCP1f'}
{'title': 'Forbidden Feelingz', 'artists': 'Nia Archives', 'duration_ms': 166356, 'track URL': 'https://open.spotify.com/album/5OoEG2axfMGY44nUNMayoW'}
{'title': 'tonight belongs to you', 'artists': 'Bissett', 'duration_ms': 147075, 'track URL': 'https://open.spotify.com/album/5oEWotsYKfSnkN6Hp11yMS'}
{'title': 'FOUND LOVE (feat. Carrie Baxter)', 'artists': 'NOTION', 'duration_ms': 136615, 'track URL': 'https://open.spotify.com/album/7ut4PfRhGtazFoK7UOMxbs'}
{'title': 'DJ Party (Monster)', 'artists': 'wewantwraiths', 'duration_ms': 194032, 'track URL': 'https://open.spotify.com/album/2UVZtK15LKQBJkl54Z8wvg'}
{'title': 'Public Displays Of Affection: The Album', 'artists': 'Muni Long', 'duration_ms': 204316, 'track URL': 'https://open.spotify.com/album/7fe4Mem3wWgY6zkTFuKUI9'}
{'title': 'I Love You.', 'artists': 'The Neighbourhood', 'duration_ms': 240400, 'track URL': 'https://open.spotify.com/album/4xkM0BwLM9H2IUcbYzpcBI'}
How to file a title, artist, duration and track URL
Using Postman - it is cool tool for REST API debugging.
#1 Get Access Token - POST request
POST https://accounts.spotify.com/api/token
In Body, select x-www-form-urlencoded
and KEY with VALUE with those value.
grant_type client_credentials

In Tests, copy/paste it.
It will assign varaible into access-token
var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("access-token", jsonData.access_token);
console.log(jsonData.access_token);

In Authorization, Select Basic Auth
type and copy paste your credential.
CLIENT_ID -> Username
CLIENT_SECRET -> Password
Finally, press Send
Button

#2 Get Access Playlist - GET request
GET https://api.spotify.com/v1/playlists/37i9dQZF1DX2Zjg8kBuEsQ
In Authorization, select Bearer Token
and enter {{access-token}}
in Token input.
Press Send
button, you can get the JSON response.

#3 Access data
by dictionary access variable['key'] (if start {
)OR
by list access variable[index] (if start [
)
r['tracks']['items'] - access items
child start with [
It will access list - we are for loop - item
item started {
- dictionary
all of child(tract)/grand child(album)/grand grand child(artists) are dictionary and it started {
it is list so [0] - first item of list then back to final name
as dictionary.
So it can access by this.
'artists' : item['track']['album']['artists'][0]['name']
Other properties same idea.
I hope to you can leverage Postman for debugging.