Invisible link to canonical for Microformats

API

Grab some functions for us, mate


API [Application Programming Interface]. You must’ve heard this word whispered in your ears a couple of times and are wondering what the hell is it.

API is a set of rules [or protocols] that enable software applications to communicate with each other to exchange data, features, etc.

So if I am using…let’s say Spotify’s API to get the top 5 songs, which looks like this :

// Authorization token that must have been created previously. See : https://developer.spotify.com/documentation/web-api/concepts/authorization
const token = 'undefined';
async function fetchWebApi(endpoint, method, body) {
  const res = await fetch(`https://api.spotify.com/${endpoint}`, {
    headers: {
      Authorization: `Bearer ${token}`,
    },
    method,
    body:JSON.stringify(body)
  });
  return await res.json();
}

async function getTopTracks(){
  // Endpoint reference : https://developer.spotify.com/documentation/web-api/reference/get-users-top-artists-and-tracks
  return (await fetchWebApi(
    'v1/me/top/tracks?time_range=long_term&limit=5', 'GET'
  )).items;
}

const topTracks = await getTopTracks();
console.log(
  topTracks?.map(
    ({name, artists}) =>
      `${name} by ${artists.map(artist => artist.name).join(', ')}`
  )
);

Check out Spotify’s developer site for more info.


Related