This tutorial will help you to make your first Web API call by retriving an artist's metadata. It assumes you have a Spotify account (free or premium).
The first step is login to the Spotify Developer Dashboard. If necessary, read the latest Developer Terms of Service to complete your account set up.
An app provides the Client ID and Client Secret needed to request an access token by implementing any of the authorization flows. To create an app, go to your Dashboard, click on the Create an app button and enter the following 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.
Finally, check the Developer Terms of Service checkbox and tap on the Create button.
The access token is a string which contains the credentials and permissions that can be used to access a given resource (e.g artists, albums or tracks) or user's data (e.g your profile or your playlists).
In order to request the access token you need 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 ID can be found here. The Client Secret can be found behind the View client secret link.
With our credentials in hand, we are ready to request an access token. This tutorial uses the Client Credentials, so we must:
- Send a POST request to the token endpoint URI.
- Add the Content-Type header set to the application/x-www-form-urlencoded value.
- Add a HTTP body containing the Client ID and Client Secret, along with the grant_type parameter set to client_credentials.
curl -X POST "https://accounts.spotify.com/api/token" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "grant_type=client_credentials&client_id=your-client-id&client_secret=your-client-secret"
The response will return an access token valid for 1 hour:
{
"access_token": "BQDBKJ5eo5jxbtpWjVOj7ryS84khybFpP_lTqzV7uV-T_m0cTfwvdn5BnBSKPxKgEb11",
"token_type": "Bearer",
"expires_in": 3600
}
For this example, we will use the Get Artist endpoint to request information about an artist. According to the API Reference, the endpoint needs the Spotify ID of the artist.
An easy way to get the Spotify ID of an artist is using the Spotify Desktop 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 include the access token we have just generated using the Authorization header as follows:
curl "https://api.spotify.com/v1/artists/4Z8W4fKeB5YxbusRsdQVPb" \
-H "Authorization: Bearer BQDBKJ5eo5jxbtpWjVOj7ryS84khybFpP_lTqzV7uV-T_m0cTfwvdn5BnBSKPxKgEb11"
If everything goes well, the API will return the following JSON response:
{
"external_urls": {
"spotify": "https://open.spotify.com/artist/4Z8W4fKeB5YxbusRsdQVPb"
},
"followers": {
"href": null,
"total": 7625607
},
"genres": [
"alternative rock",
"art rock",
"melancholia",
"oxford indie",
"permanent wave",
"rock"
],
"href": "https://api.spotify.com/v1/artists/4Z8W4fKeB5YxbusRsdQVPb",
"id": "4Z8W4fKeB5YxbusRsdQVPb",
"images": [
{
"height": 640,
"url": "https://i.scdn.co/image/ab6761610000e5eba03696716c9ee605006047fd",
"width": 640
},
{
"height": 320,
"url": "https://i.scdn.co/image/ab67616100005174a03696716c9ee605006047fd",
"width": 320
},
{
"height": 160,
"url": "https://i.scdn.co/image/ab6761610000f178a03696716c9ee605006047fd",
"width": 160
}
],
"name": "Radiohead",
"popularity": 79,
"type": "artist",
"uri": "spotify:artist:4Z8W4fKeB5YxbusRsdQVPb"
}
You can also request Spotify catalog information for several artists based on their Spotify IDs.
This is the request sample for that:
curl --request GET \
--url 'https://api.spotify.com/v1/artists?ids=2CIMQHirSU0MQqyYHq0eOx%2C57dN52uHvrHOxijzpIgu3E%2C1vCWHaC5f2uS3yhpwWbIA6' \
--header 'Authorization: Bearer 1POdFZRZbvb...qqillRxMr2z'
Then we have the next response sample:
{
"artists": [
{
"external_urls": {
"spotify": "string"
},
"followers": {
"href": "string",
"total": 0
},
"genres": [
"Prog rock",
"Grunge"
],
"href": "string",
"id": "string",
"images": [
{
"url": "https://i.scdn.co/image/ab67616d00001e02ff9ca10b55ce82ae553c8228",
"height": 300,
"width": 300
}
],
"name": "string",
"popularity": 0,
"type": "artist",
"uri": "string"
}
]
}
Congratulations! You made your first API call to the Spotify Web API.
All the documentation in this page is taken from Spotify for Developers.