Skip to content

Instantly share code, notes, and snippets.

@koji
Last active March 22, 2019 01:48
Show Gist options
  • Save koji/16808fb6473491d580fdf1add3039d53 to your computer and use it in GitHub Desktop.
Save koji/16808fb6473491d580fdf1add3039d53 to your computer and use it in GitHub Desktop.
spotify_api

preparation

$ mkdir spotifyapi (whatever you want)
$ cd spotifyapi
$ npm init -y
$ npm install spotify-web-api-node --save

app.js

const SpotifyWebApi = require('spotify-web-api-node');

// credentials are optional
const spotifyApi = new SpotifyWebApi({
    clientId: 'your client id',
    clientSecret: 'your client secret id'
});
spotifyApi.setAccessToken('');


// get current play song
spotifyApi.getMyCurrentPlaybackState({})
    .then((data) => {
        // Output items
        // console.log(data.body);
        const songId = data.body.item.id;
        const songTitle = data.body.item.name;

        // song info
        console.log(`song id: ${songId}`);
        console.log(`song title: ${songTitle}`);

        // get audio feature
        spotifyApi.getAudioFeaturesForTrack(songId)
            .then(function (data) {
                console.log(data.body);
            }, function (err) {
                done(err);
            });
    }, (err) => {
        console.log(`Something went wrong! ${err}`);
    });

run code

$ node app.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment