Track Audio Features and Analysis

Learn how to get audio features and analysis of a track using the Spotify API.

Imagine having a list of songs to play at a party but not sure what to play. Spotify can help us sort our list. It provides two endpoints that can be used to get features and analysis of a track. In this lesson, we'll look at these endpoints.

The Get Track’s Audio Features endpoint of the Spotify API can help us analyze a track using some key metrics. We can call the endpoint https://api.spotify.com/v1/audio-features/{id} and it will fetch the required metrics in response. The {id} in this URI is replaced with the Spotify ID of the track.

Using these metrics, we can help a user select the tracks that they’ll highly likely want to play. We can also use these metrics to categorize the tracks.

Note: The value of these matrices for each song will depend upon the availability of data on Spotify. So we might see that some metrics are empty in the API response.

Request parameters

This endpoint has no query parameter.

The code below demonstrates how to get the audio features of a track using the Spotify API. Click the "Run" button to get the audio features.

Press + to interact
const endpointUrl = new URL('https://api.spotify.com/v1/audio-features/11dFghVXANMlKmJXsNCbNl');
// 11dFghVXANMlKmJXsNCbNl is the Spotify ID of the song Cut To The Feeling
headerParameters = {
'Content-Type': 'application/json',
'Authorization': 'Bearer {{CLIENT_CREDENTIALS_ACCESS_TOKEN}}'
}
const options = {
method: 'GET',
headers: headerParameters,
};
async function fetchTrackFeatures() {
try {
const response = await fetch(endpointUrl, options);
printResponse(response);
} catch (error) {
printError(error);
}
}
fetchTrackFeatures();

Let's look at the explanation for the code given above:

  • Line 1: We set the URL to https://api.spotify.com/v1/audio-features/11dFghVXANMlKmJXsNCbNl. We can change the ID in line 1 to get information about a different track.

  • Lines 14–21: We define a function, fetchTrackFeatures(). This function calls the endpoint and prints the response.

Response Fields

We'll get the index for energy, liveness, tempo, speechiness, acousticness, instrumentalness, danceability, loudness and valence of the track.

We can replace the value of endpointUrl in line 1 with the base URI https://api.spotify.com/v1/audio-features combined with the Spotify IDs of multiple tracks as query parameters to get audio features for multiple tracks. The code below demonstrates how we can use this endpoint.

Press + to interact
const endpointUrl = new URL('https://api.spotify.com/v1/audio-features');
const queryParameters = new URLSearchParams({
ids: '11dFghVXANMlKmJXsNCbNl,7ouMYWpwJ422jRcDASZB7P,4VqPOruhp5EdPBeR92t6lQ'
});
// 11dFghVXANMlKmJXsNCbNl is the Spotify ID of the song Cut To The Feeling
// 7ouMYWpwJ422jRcDASZB7P is the Spotify ID of the song Knights of Cydonia
// 4VqPOruhp5EdPBeR92t6lQ is the Spotify ID of the song Uprising
headerParameters = {
'Content-Type': 'application/json',
'Authorization': 'Bearer {{CLIENT_CREDENTIALS_ACCESS_TOKEN}}'
}
const options = {
method: 'GET',
headers: headerParameters,
};
async function fetchTracksFeatures() {
try {
endpointUrl.search = queryParameters;
const response = await fetch(endpointUrl, options);
printResponse(response);
} catch (error) {
printError(error);
}
}
fetchTracksFeatures();

Let's look at how we modified the code:

  • Line 1: We change the URL to https://api.spotify.com/v1/audio-features.

  • Lines 3–5: We add query parameters.

  • Line 20: We change the function's name to fetchTracksFeatures().

We'll get the same information as we got in the first case but for multiple tracks.

Track audio analysis

The Get Track's Audio Analysis endpoint is used to get the track’s audio analysis information. The base URI of this endpoint is https://api.spotify.com/v1/audio-analysis/{id}. The {id} in the URI is replaced with the ID of the track when making the API call.

It can be used in applications where we provide the details of tracks to a user. It provides technical details of the tracks and that too for different intervals of the track, unlike the Get Track’s Audio Features endpoint.

Request parameters

This endpoint has no query parameters.

The code below calls the Audio Analysis endpoint and prints the response.

Press + to interact
const endpointUrl = new URL('https://api.spotify.com/v1/audio-analysis/11dFghVXANMlKmJXsNCbNl');
// 11dFghVXANMlKmJXsNCbNl is the Spotify ID of the song Cut To The Feeling
headerParameters = {
'Content-Type': 'application/json',
'Authorization': 'Bearer {{CLIENT_CREDENTIALS_ACCESS_TOKEN}}'
}
const options = {
method: 'GET',
headers: headerParameters,
};
async function fetchTrackAnalysis() {
try {
const response = await fetch(endpointUrl, options);
printResponse(response);
} catch (error) {
printError(error);
}
}
fetchTrackAnalysis();

Let's look at how we modified the code:

  • Line 1: We change the URL to https://api.spotify.com/v1/audio-analysis/11dFghVXANMlKmJXsNCbNl. We can change the ID in line 1 to get information about a different track.

  • Line 14: We change the function's name to fetchTracksAnalysis().

We can use the Search for Items endpoint to get the track ID using the track's name.

Response fields

The response of this endpoint contains the following information:

Response field

Type

Description

analyzer_version

String

This is the version of the analyzer used.

analysis_time

Integer

This is the amount of time, in milliseconds, taken to analyze the track.

num_samples

Integer

This is the number of samples of the track that were analyzed.

loudness

Float

This is the loudness of the sound in decibells.

tempo

Float

This is the speed or pace of a sample.

sync_string

String

It is a string that demonstrates how to accurately synchronize the track to a corresponding waveform.

start

Float

This is the start point of a sample (in seconds).

duration

Float

This is the duration of a sample (in seconds).

tatums

Array

This contains information about the smallest time interval between successive notes in a rhythmic phrase.