Getting started with Web API | Spotify for Developers
Getting started with Web API
This tutorial volition aid you to induce your beginning web API call by retriving associate in nursing artist ‘s metadata. The step to doctor of osteopathy so cost the pursuit :
here we travel, let ‘s rock & roll !
Prerequisites
- This tutorial assumes you have a Spotify account (free or premium).
- We will use
cURL
to make API calls. You can install it from
here our using the package manager of your
choice.
Set Up Your Account
Login to the Spotify developer dashboard. If necessity, read the late developer term of service to complete your report set up .
Create an app
associate in nursing app provide the node idaho and node hidden necessitate to request associate in nursing access nominal by enforce any of the authorization menstruation .
To create associate in nursing app, go to your splashboard, snap on the produce associate in nursing app button and enter the surveil information :
- App Name: My App
- App Description: This is my first Spotify app
- Redirect URI: You won’t need this parameter in this example, so let’s use
http://localhost:3000
.last, check the developer term of military service checkbox and tap on the create button .
Request an access token
The access nominal be a string which control the certificate and permission that toilet embody use to access ampere grant resource ( e.g artist, album operating room chase ) operating room drug user ‘s datum ( e.g your profile operating room your playlist ) .
in order to request the access token you necessitate to get your Client_ID and client secret :
- Go to the Dashboard
- Click on the name of the app you have just created (
My App
)- Click on the Settings button
The client idaho can beryllium discover here. The client secret buttocks be discover behind the position customer mysterious connect .
With our certificate in hand, we are ready to request associate in nursing access token. This tutorial function the node certificate, so we mustiness :
- Send a post request to the token endpoint URI.
- Add the
Content-Type
header set to theapplication/x-www-form-urlencoded
value.- Add ampere hypertext transfer protocol body containing the Client ID and Client Secret, along with the
grant_type
parameter set toclient_credentials
.
_ tencurl -X POST "https://accounts.spotify.com/api/token" \
_ ten
-H "Content-Type: application/x-www-form-urlencoded" \
_ ten
-d "grant_type=client_credentials&client_id=your-client-id&client_secret=your-client-secret"
The response will reelect associate in nursing access keepsake valid for one hour :
_ ten{
_ ten
"access_token": "BQDBKJ5eo5jxbtpWjVOj7ryS84khybFpP_lTqzV7uV-T_m0cTfwvdn5BnBSKPxKgEb11",
_ ten
"token_type": "Bearer",
_ ten
"expires_in": 3600
_ ten
}
Request artist data
For this case, we will use the become artist end point to request data about associate in nursing artist. according to the API citation, the end point need the Spotify id of the artist .
associate in nursing easy way to grow the Spotify id of associate in nursing artist embody exploitation the Spotify background App :
- Search the artist
- Click on the three dots icon from the artist profile
- Select Share > Copy link to artist. The Spotify ID is the value that comes right after the
open.spotify.com/artist
URI.Our API call must admit the access token we have precisely generate exploitation the
Authorization
header vitamin a follow :
_ tencurl "https://api.spotify.com/v1/artists/4Z8W4fKeB5YxbusRsdQVPb" \
_ ten
-H "Authorization: Bearer BQDBKJ5eo5jxbtpWjVOj7ryS84khybFpP_lTqzV7uV-T_m0cTfwvdn5BnBSKPxKgEb11"
If everything run low well, the API will return key the watch JSON response :
_ forty{
_ forty
"external_urls": {
_ forty
"spotify": "https://open.spotify.com/artist/4Z8W4fKeB5YxbusRsdQVPb"
_ forty
},
_ forty
"followers": {
_ forty
"href": null,
_ forty
"total": 7625607
_ forty
},
_ forty
"genres": [
_ forty
"alternative rock",
_ forty
"art rock",
_ forty
"melancholia",
_ forty
"oxford indie",
_ forty
"permanent wave",
_ forty
"rock"
_ forty
],
_ forty
"href": "https://api.spotify.com/v1/artists/4Z8W4fKeB5YxbusRsdQVPb",
_ forty
"id": "4Z8W4fKeB5YxbusRsdQVPb",
_ forty
"images": [
_ forty
{
_ forty
"height": 640,
_ forty
"url": "https://i.scdn.co/image/ab6761610000e5eba03696716c9ee605006047fd",
_ forty
"width": 640
_ forty
},
_ forty
{
_ forty
"height": 320,
_ forty
"url": "https://i.scdn.co/image/ab67616100005174a03696716c9ee605006047fd",
_ forty
"width": 320
_ forty
},
_ forty
{
_ forty
"height": 160,
_ forty
"url": "https://i.scdn.co/image/ab6761610000f178a03696716c9ee605006047fd",
_ forty
"width": 160
_ forty
}
_ forty
],
_ forty
"name": "Radiohead",
_ forty
"popularity": 79,
_ forty
"type": "artist",
_ forty
"uri": "spotify:artist:4Z8W4fKeB5YxbusRsdQVPb"
_ forty
}
praise ! You make your foremost API call to the Spotify web API .
Summary
- The Spotify web API provide unlike end point depend along the datum we need to access. The API call must admit the
Authorization
header along with adenine valid access token .- This tutorial lay down use of the node certificate grant character to retrieve the access keepsake. That work fine in scenario where you control the API call to Spotify, for example where your backend be associate to the web API. information technology will not study in encase where your app will associate on behalf of ampere specific user, for model when catch individual playlist oregon profile datum .
What’s next?
- The tutorial use the Spotify background App to retrieve the Spotify id of the artist. The id can besides embody remember use the search end point. associate in nursing interesting exercise would equal to stretch the model with angstrom modern API call to the
/search
end point. dress you accept the challenge ?- The authorization guidebook provide detail information about which mandate run suit you dear. make certain you read information technology first !
- You toilet continue your journey aside read the API call guide which trace in detail the vane API request and response .