Facebook
From DJ Blyatman, 1 Month ago, written in JavaScript.
Embed
Download Paste or View Raw
Hits: 161
  1. // Import the axios library
  2. const axios = require('axios');
  3.  
  4. // Set your access token (you can get this from your Spotify premium account)
  5. const accessToken = 'your_access_token_here';
  6.  
  7. // Initialize the Spotify Web API
  8. const spotifyAPI = axios.create({
  9.   baseURL: 'https://api.spotify.com/v1/',
  10.   headers: {
  11.     'Authorization': `Bearer ${accessToken}`
  12.   }
  13. });
  14.  
  15. // Get a user's top tracks
  16. spotifyAPI.get('me/top/tracks', {
  17.   params: {
  18.     limit: 10,
  19.     time_range: 'short_term'
  20.   }
  21. })
  22. .then(response => {
  23.   // Handle the response
  24.   console.log(response.data);
  25. })
  26. .catch(error => {
  27.   // Handle the error
  28.   console.error(error.response.data);
  29. });
  30.  
  31. // Get a track's audio features
  32. spotifyAPI.get('audio-features/track/4iV5W9uYEdYUVa79A7WQQb')
  33. .then(response => {
  34.   // Handle the response
  35.   console.log(response.data);
  36. })
  37. .catch(error => {
  38.   // Handle the error
  39.   console.error(error.response.data);
  40. });